使用程序生成创建磁盘但纹理无法正常工作
using procedural generation to creat a disk but the texture does not work properly
期望的结果是整个磁盘的纹理几乎相同,但它似乎只在边缘周围的几个三角形区域正确渲染
disk.cpp
void disk::disk(int sides)
{
float interval = ( 2 * PI )/sides;
float angle = 0.0f;
groundtexture = SOIL_load_OGL_texture
(
"gfx/ground.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glBegin(GL_TRIANGLES);
for (int i = 0; i < sides; i++)
{
glBindTexture(GL_TEXTURE_2D,groundtexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexCoord2f(0.5, -0.5);
glVertex3f(0, 0, 0);
glTexCoord2f(cos(angle) / 2 + 0.5, - (sin(angle) / 2 + 0.5));
glVertex3f(cos(angle), sin(angle), 0);
glTexCoord2f(cos(angle + interval) / 2 + 0.5, -(sin(angle + interval) / 2 - 0.5));
glVertex3f(cos(angle + interval), sin(angle + interval), 0);
angle += interval;
}
glVertex3f(cos(0), sin(0), 0);
glEnd();
}
磁盘可以渲染,但纹理渲染不正确。编辑建议磁盘纹理不正确
GL_TEXTURE_MAG_FILTERcannot beGL_LINEAR_MIPMAP_LINEAR。它应该是 GL_LINEAR 而不是。所以替换这一行:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
和
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
无论如何,您都会遇到 OpenGL 错误。 glBindTexture
、glTexParameteri
分别在 glBegin
/glEnd
序列中不允许 glTexEnvf
。只允许修改属性或指定顶点的指令。
绑定贴图,设置贴图参数前glBegin
:
glBindTexture(GL_TEXTURE_2D, myTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBegin(GL_TRIANGLES);
for (int i = 0; i < sides; i++)
{
glTexCoord2f(0.5, -0.5);
glVertex3f(0, 0, 0);
glTexCoord2f(cos(angle) / 2 + 0.5, - (sin(angle) / 2 + 0.5));
glVertex3f(cos(angle), sin(angle), 0);
glTexCoord2f(cos(angle + interval) / 2 + 0.5, -(sin(angle + interval) / 2 - 0.5));
glVertex3f(cos(angle + interval), sin(angle + interval), 0);
angle += interval;
}
glEnd();
GL_MODULATE 是整数值,而不是浮点值。如果你想要正确的结果,请使用正确的方法;)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
期望的结果是整个磁盘的纹理几乎相同,但它似乎只在边缘周围的几个三角形区域正确渲染
disk.cpp
void disk::disk(int sides)
{
float interval = ( 2 * PI )/sides;
float angle = 0.0f;
groundtexture = SOIL_load_OGL_texture
(
"gfx/ground.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glBegin(GL_TRIANGLES);
for (int i = 0; i < sides; i++)
{
glBindTexture(GL_TEXTURE_2D,groundtexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexCoord2f(0.5, -0.5);
glVertex3f(0, 0, 0);
glTexCoord2f(cos(angle) / 2 + 0.5, - (sin(angle) / 2 + 0.5));
glVertex3f(cos(angle), sin(angle), 0);
glTexCoord2f(cos(angle + interval) / 2 + 0.5, -(sin(angle + interval) / 2 - 0.5));
glVertex3f(cos(angle + interval), sin(angle + interval), 0);
angle += interval;
}
glVertex3f(cos(0), sin(0), 0);
glEnd();
}
磁盘可以渲染,但纹理渲染不正确。编辑建议磁盘纹理不正确
GL_TEXTURE_MAG_FILTERcannot beGL_LINEAR_MIPMAP_LINEAR。它应该是 GL_LINEAR 而不是。所以替换这一行:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
和
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
无论如何,您都会遇到 OpenGL 错误。 glBindTexture
、glTexParameteri
分别在 glBegin
/glEnd
序列中不允许 glTexEnvf
。只允许修改属性或指定顶点的指令。
绑定贴图,设置贴图参数前glBegin
:
glBindTexture(GL_TEXTURE_2D, myTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBegin(GL_TRIANGLES);
for (int i = 0; i < sides; i++)
{
glTexCoord2f(0.5, -0.5);
glVertex3f(0, 0, 0);
glTexCoord2f(cos(angle) / 2 + 0.5, - (sin(angle) / 2 + 0.5));
glVertex3f(cos(angle), sin(angle), 0);
glTexCoord2f(cos(angle + interval) / 2 + 0.5, -(sin(angle + interval) / 2 - 0.5));
glVertex3f(cos(angle + interval), sin(angle + interval), 0);
angle += interval;
}
glEnd();
GL_MODULATE 是整数值,而不是浮点值。如果你想要正确的结果,请使用正确的方法;)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);