glBindTexture 不起作用

发布于 2024-11-05 20:38:29 字数 2678 浏览 3 评论 0原文

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

我有 N 个“块”,每个块都有一个材料名称。

我循环遍历每个部分,根据其材质名称查找其纹理,调用 glBindTexture,然后绘制该部分。 看来,如果我有多个具有相同材质名称的部件,则只有第一个是用纹理绘制的,其余的都是白色的。

任何帮助将不胜感激。

下面是代码:

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

I'm trying to draw a few simple 3D rectangular prisms, but glBindTexture does not seem to be working properly.

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

I am looping through each piece, finding its texture based on its material name, calling glBindTexture, then drawing the piece.
It seems though, if i have multiple pieces 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:

OpenGL initialisation:

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

texture loading

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 the 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"'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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-11-12 20:38:29

不存在像 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”。质地。

There's no such thing like OpenGL initialization (apart from setting up a context). All what you do to in the "initialization" code either belongs in the render function or texture loading code:

All the following semantically belongs into the render path. OpenGL is a state machine, and like all state machines you put it into a proper start condition before you're doing some job.

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

In your texture loader you have this:

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

Either this is a typo, or the generated texture name doesn't make it into 't.' texture.

豆芽 2024-11-12 20:38:29

我想我已经修好了。

似乎纹理文件(.RAW 图像)存在实际问题,它显示为白色,而不是应有的颜色/图案。

我用较少量的材料(3 而不是 3663)再次测试,似乎工作正常。

I think i have fixed it.

Seems there was an actual problem with the texture file (.RAW images) and it was displaying as white instead of the colour/pattern it should have.

I tested again with a smaller amount of materials (3 instead of 3663) and it seems to work OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文