OpenGL multiple texture code

xiaoxiao2021-03-06  54

Multi-texture theory is not introduced, I don't know the underlying implementation, there is also an example in the OpenGL tutorial of Nehe. The use of multiple textures in the graduation topic, I just paste some implementation code, for some people, this is enough, :)

My graphics card is GF5700, query the ARB extension, support up to 4 textures, so each pass is loaded up to 4 textures, I sent the texture into the graphics card, manipulated the texture by the CG, so the following code does not have too many textures. operating,

#include #include #include "gl / glext.h" //glext.h Need to download another download

// define extensions inlet macro #define INIT_ENTRY_POINT (funcname, type) / static type funcname = (type) wglGetProcAddress (#funcname); / if (funcname!) {/ TRACE2 (_T ( "!! Fail to Get Proc address:" #funcname / "% s,% d) / n"), __ file __, __ line__); /} // mac end

,,,

// Test the support of the graphics card to multiple textures

int nUnits, nSize; glGetIntegerv (GL_MAX_TEXTURE_UNITS_ARB, & nUnits); glGetIntegerv (GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, & nSize); TRACE2 ( "GL_MAX_TEXTURE_UNITS_ARB =% d, GL_MAX_RECTANGLE_TEXTURE_SIZE_NV =% d / n", nUnits, nSize);

Define texture, I use a rectangular texture inside, so that the scope of texture coordinates is not [0, 1], but [0, W], [0, h], it is also very simple, after the time Some of it,

glGenTextures (1, & param.nVerTex); glBindTexture (GL_TEXTURE_RECTANGLE_NV, param.nVerTex); glTexImage2D (GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB, param.sizeVertexTex.cx, param.sizeVertexTex.cy, 0, GL_RGB, GL_FLOAT, param.pfVertexTex); glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); ,,,

Create objects, attach textures,

INIT_ENTRY_POINT (glActiveTextureARB, PFNGLACTIVETEXTUREARBPROC); INIT_ENTRY_POINT (glMultiTexCoord2fARB, PFNGLMULTITEXCOORD2FARBPROC); // bind the texture glActiveTextureARB (GL_TEXTURE0_ARB); glEnable (GL_TEXTURE_RECTANGLE_NV); glBindTexture (GL_TEXTURE_RECTANGLE_NV, pTexA-> nIdxTex); glActiveTextureARB (GL_TEXTURE1_ARB); glEnable (GL_TEXTURE_RECTANGLE_NV); glBindTexture (GL_Texture_Rectangle_nv, PTEXA-> NVERTEX);

GLACTIVETEXTUREARB (GL_TEXTURE2_ARB); GLENABLE (GL_TEXTURE_RECTANGLE_NV); GlbindTexture (GL_Texture_Rectangle_nv, PTEXB-> NIDXTEX);

GLACTIVETEXTUREARB (GL_TEXTURE3_ARB); GLENABLE (GL_Texture_Rectangle_nv); GlbindTexture (GL_Texture_Rectangle_nv, PTEXB-> NVERTEX); Glbegin (GL_QUADS);

// lower left glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 0,0); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 0,0); glMultiTexCoord2fARB (GL_TEXTURE2_ARB, 0,0); glMultiTexCoord2fARB (GL_TEXTURE3_ARB, 0,0); glVertex3f (0.0f, 0.0f, -10.0f ); // lower right glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 50,0); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 100.0f, 0.0f); glMultiTexCoord2fARB (GL_TEXTURE2_ARB, 10.0f, 0.0f); glMultiTexCoord2fARB (GL_TEXTURE3_ARB, 10.0f, 0.0f); glVertex3f ( 100.0f, 0.0f, -10.0f); // upper right glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 50,50); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 10.0f, 10.0f); glMultiTexCoord2fARB (GL_TEXTURE2_ARB, 10.0f, 1.0f); glMultiTexCoord2fARB (GL_TEXTURE3_ARB, 10.0 f, 1.0f); glVertex3f (100.0f, 100.0f, -10.0f); // upper left glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 0,50); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 0.0f, 10.0f); glMultiTexCoord2fARB (GL_TEXTURE2_ARB, 0.0f, 10.0 f); GLMULTITEXCOORD2FARB (GL_TEXTURE3_ARB, 0.0F, 10.0F); GlvertEX3F (0.0F, 100.0F, -10.0F);

glend;

GLFlush ();

glActiveTextureARB (GL_TEXTURE0_ARB); glDisable (GL_TEXTURE_RECTANGLE_NV); glActiveTextureARB (GL_TEXTURE1_ARB); glDisable (GL_TEXTURE_RECTANGLE_NV); glActiveTextureARB (GL_TEXTURE2_ARB); glDisable (GL_TEXTURE_RECTANGLE_NV);

GLACTIVETEXTUREARB (GL_TEXTURE3_ARB); GLDISABLE (GL_Texture_Rectangle_nv);

转载请注明原文地址:https://www.9cbs.com/read-117875.html

New Post(0)