我正在尝试绘制一些简单的3D矩形棱镜,但是glBindTexture似乎无法正常工作。

我有N个“零件”,每个都有一个 Material 名称。

我遍历每一块,根据其 Material 名称查找其纹理,调用glBindTexture,然后绘制该块。
但是,如果我有多块具有相同 Material 名称的零件,似乎只有第一个具有纹理绘制,其余的只是白色。

任何帮助将不胜感激。

这是代码:

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);

纹理加载
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);

然后是绘图代码:
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"'s 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();
    }
}

最佳答案

没有像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”。质地。

关于c++ - glBindTexture无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5933440/

10-15 00:22