【发布时间】:2015-08-05 16:16:15
【问题描述】:
我的问题是 OpenGL 3.3 没有绘制我的纹理。我检查了它是否是我的着色器的问题(它无法很好地加载),但着色器没问题。然后,我检查了是否是UV坐标的问题,但是我看到使用Fragment Shader的这段代码是可以的:
#version 330 core
out vec3 outColor;
in DATA {
vec2 UV;
} fs_in;
void main() {
outColor = vec3(fs_in.UV.x, fs_in.UV.y, 0.0);
}
最后,我使用了 Fragment Shader 的这段代码,但它不起作用:
#version 330 core
out vec3 outColor;
uniform sampler2D mySampler;
in DATA {
vec2 UV;
} fs_in;
void main() {
outColor = texture(mySampler, fs_in.UV).rgb;
}
加载 BMP 图像并将其提供给 OpenGL 的代码如下(我在网站上找到的):
GLuint loadTexture(const char *lpszTexturePath) {
unsigned char header[54];
unsigned int dataPos;
unsigned int width, height;
unsigned int imageSize;
unsigned char * data;
FILE *file;
fopen_s(&file, lpszTexturePath, "rb");
if(!file)
printf("Image could not be opened\n"); return 0;
if(fread(header, 1, 54, file) != 54) {
printf("Not a correct BMP file\n");
return false;
}
if(header[0] != 'B' || header[1] != 'M') {
printf("Not a correct BMP file\n");
return 0;
}
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if(imageSize == 0) imageSize = width*height * 3;
if(dataPos == 0) dataPos = 54;
data = new unsigned char[imageSize];
fread(data, 1, imageSize, file);
fclose(file);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
return textureID;
}
部分主要代码: GLuint uTextureID = loadTexture("image.bmp");
while(!glfwWindowShouldClose(lpstWndID)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(uProgID);
glBindTexture(GL_TEXTURE_2D, uTextureID);
object.renderObject();
glfwSwapBuffers(lpstWndID);
}
【问题讨论】:
-
您是否在调试器中单步执行了加载代码,并确信它正在成功加载 BMP 文件?有预期的尺寸和一切?
-
图片加载正确(我检查过了)
-
另外,我尝试创建一个像素数组:'const char pixels[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 } ' 但它不起作用
-
你有没有告诉着色器使用哪个纹理(glUniform1i)?
-
它只是一个纹理。我从来没有用过你说的:S
标签: opengl textures bmp texture2d