gpt4 book ai didi

c++ - glBindTexture 不工作

转载 作者:太空狗 更新时间:2023-10-29 23:09:14 31 4
gpt4 key购买 nike

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

我有 N 个“片段”,每个片段都有一个 Material 名称。

我遍历每一 block ,根据其 Material 名称找到它的纹理,调用 glBindTexture,然后绘制这 block 。看起来,如果我有多个具有相同 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com