【发布时间】:2020-07-23 03:25:16
【问题描述】:
我正在创建一个带有 6 个 Torus 的开放式 GL 程序。当我应用纹理时,它以一种颜色显示。即使它不是单一颜色的。
我正在从数组中获取图像。除圆环纹理外,其他纹理(天空盒、行星、HUD)正在正确应用。我也尝试应用光源,但没有运气。有人可以帮我实现这一目标吗?
下面是我的代码。
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glTranslatef(xpos, ypos, zpos);
glRotatef(fPlanetRot, 0.0f, -1.0f, 0.0f);
glColor3f(0.0, 0.0, 0.0);
// Select the texture object
glBindTexture(GL_TEXTURE_2D, textures[3]);
glutSolidTorus(0.1, 1.0, 30, 30);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
//This is where I load the texture
void SetupRC()
{
//textures
GLuint texture;
// allocate a texture name
glGenTextures( 1, &texture );
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
//there are quite a few ways of loading images
// Load textures in a for loop
glGenTextures(TEXTURE_COUNT, textures);
//this puts the texture into OpenGL texture memory
// glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - not defined so probably need to update GLEW - not needed here so ignore
for(int iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++)
{
// Bind to next texture object
glBindTexture(GL_TEXTURE_2D, textures[iLoop]);
// Load texture data, set filter and wrap modes
//note that gltLoadTGA is in the glm.cpp file and not a built-in openGL function
pBytes0 = gltLoadTGA(textureFiles[iLoop],&iWidth, &iHeight,
&iComponents, &eFormat);
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes0);
//set up texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//try these too
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
free(pBytes0);
}
//enable textures
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW);// Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside
// glCullFace(GL_FRONT_AND_BACK);
// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_POINT_SMOOTH);
// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLightLessBright);
glLightfv(GL_LIGHT0,GL_DIFFUSE,redLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
// Enable colour tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// Black blue background
glClearColor(0.0f, 0.0f, 0.03f, 1.0f );
}
【问题讨论】:
标签: c++ opengl textures opengl-compat