我试图在我的立方体上做纹理,如下面的代码所示。然而,我得到的结果是图像没有覆盖整个立方体表面,而看起来像循环。
您可以从使用的图片中看到蓝色衬里:
我检查了坐标,但似乎没有任何错误。我检查图像大小(512x512),它也与我在加载纹理时声明的相同。所以,我无法确定问题所在。
void square(void) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]); //bind our texture to our shape
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3d(500, -500, -500); //behind
glTexCoord3d(1.0, 0.0, 0.0); glVertex3d(500, 500, -500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3d(-500, 500, -500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3d(-500, -500, -500);
glEnd();
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, -500, 500); //front
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, 500, 500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, 500, 500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, -500, 500);
glEnd();
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, -500, -500); //right
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, 500, -500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(500, 500, 500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(500, -500, 500);
glEnd();
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(-500, -500, 500); //left
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(-500, 500, 500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, 500, -500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, -500, -500);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, 500, 500); //top
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, 500, -500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, 500, -500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, 500, 500);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_POLYGON);
glTexCoord3d(0.0, 0.0, 0.0); glVertex3f(500, -500, -500); //bottom
glTexCoord3d(1.0, 0.0, 0.0); glVertex3f(500, -500, 500);
glTexCoord3d(1.0, 1.0, 0.0); glVertex3f(-500, -500, 500);
glTexCoord3d(0.0, 1.0, 0.0); glVertex3f(-500, -500, -500);
glEnd();
glDisable(GL_TEXTURE_2D);
}
加载RAW文件的功能:
GLuint LoadTexture(const char * filename, int width, intbheight)
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen(filename, "rb");
if (file == NULL) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
glGenTextures(1, &texture); //generate the texture with the loaded data
glBindTexture(GL_TEXTURE_2D, texture); //bind the textureto it’s array
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE); //set texture environment parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT);
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
free(data); //free the texture
return texture; //return whether it was successful
}