GlBindTexture does not work

I am trying to draw some simple 3D rectangular prisms, but glBindTexture does not seem to work properly.

I have N number of "pieces", each of which has a material name.

I go through each part, find its texture based on its material name, calling glBindTexture, and then draw a piece. It seems that if I have several parts with the same material name, only the first is drawn with a texture, and the rest are just white.

Any help would be greatly appreciated.

Here is the code:

Initializing OpenGL:

static PIXELFORMATDESCRIPTOR pfd =
{
    sizeof(PIXELFORMATDESCRIPTOR),
    1,
    PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,
    32, // bit depth
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    16, // z-buffer depth
    0, 0, 0, 0, 0, 0, 0,
};

GLfloat light_pos1[] = {0.0f, 0.0f, 15.0f, 1.0f};
GLfloat light_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};

// Get device context only once.
hdc = GetDC()->m_hDC;

// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, m_nPixelFormat, &pfd);

// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);

//texture
glEnable(GL_TEXTURE_2D);

//Setup:
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

//lighting
glLightfv(GL_LIGHT0, GL_POSITION, light_pos1);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

// Send draw request
OnDraw(NULL);

Loading textures

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, t.texture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, data);

Then drawing code:

glPolygonMode(GL_FRONT_AND_BACK, GL_FLAT);

for(int i = 0; i < mPieces.GetCount(); i++)
{
    C3DPiece p = mPieces.GetAt(mPieces.FindIndex(i));

    if(p.visible)
    {
        //GetTextureForMaterial finds the texture id based on the "piece" material name
        //have checked this and it is always returning a valid id (>0)

        GLuint tex = GetTextureForMaterial(p.material);

        glBindTexture(GL_TEXTURE_2D, tex);

        glBegin(GL_QUADS);
        glNormal3f(..., ..., ...);
        glTextCoord2f(..., ...);
        glVertex3F(..., ..., ...);
        glTextCoord2f(..., ...);
        glVertex3F(..., ..., ...);
        glTextCoord2f(..., ...);
        glVertex3F(..., ..., ...);
        glTextCoord2f(..., ...);
        glVertex3F(..., ..., ...);
        glEnd();
    }
}
+3
source share
2 answers

, OpenGL- ( ). , , :

. OpenGL - , , , , - .

//texture
glEnable(GL_TEXTURE_2D);

//Setup:
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

//lighting
glLightfv(GL_LIGHT0, GL_POSITION, light_pos1);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

:

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, t.texture);

, 't.' .

+1

, .

, ( .RAW), , /, .

(3 3663), , , .

0
source

All Articles