问题图片:
我发现图像有些偏移。我认为贴图位置是问题所在,但我无法解决这个问题。
我的代码:
GLuint LoadTexture(const char * filename, int width, int height)
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen(filename, "r");
if (file == NULL) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
// RGB
for (int i = 0; i < width * height; ++i)
{
int index = i * 3;
unsigned char B, R;
B = data[index];
R = data[index + 2];
data[index] = R;
data[index + 2] = B;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); // GL_REPLACE에서 교체했음
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // GL_CLAMP
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
return texture;
并且
glBindTexture(GL_TEXTURE_2D, texture_image[2]);
glBlendFunc(GL_ONE, GL_ONE);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex3f(-32, -32, 0);
glTexCoord2i(0, 1); glVertex3f(-32, 32, 0);
glTexCoord2i(1, 1); glVertex3f(32, 32, 0);
glTexCoord2i(1, 0); glVertex3f(32, -32, 0);
glEnd();
这是我的第一个问题, 我不知道是否正确,但感谢你。