纹理在OpenGL ES 2.0中没有显示
Texture not showing up in OpenGL ES 2.0
目前在iOS原生OpenGL ES 2.0中工作。由于某些原因,我的纹理四边形不会渲染,它只是不会出现。我尝试为纹理加载。bmp文件,并且我知道它通过,因为我放入PolygonRenderer.addTexture
的日志函数打印出"Image size: 256, 256"
,这是我的纹理的大小。
编辑:我的.bmp
纹理保存在R8, G8, B8形式。
VertexShader:
std::string textureVertex =
"attribute vec3 vertexloc; n"
"attribute vec3 vertexcol; n"
"attribute vec2 vertexuv; n"
"varying vec2 TexCoords; n"
"varying vec3 textColor; n"
"uniform mat4 projection; n"
"uniform mat4 view; n"
"uniform mat4 offset; n"
"void main() n"
"{ n"
" gl_Position = projection * view * offset * vec4(vertexloc, 1.0); n"
" TexCoords = vertexuv; n"
" textColor = vertexcol; n"
"}";
片段着色器:
std::string textureFragment =
"precision mediump float; n"
"varying vec2 TexCoords; n"
"varying vec3 textColor; n"
"uniform sampler2D text; n"
"void main() n"
"{ n"
" vec4 sampled = vec4(1.0, 1.0, 1.0, texture2D(text, TexCoords).r); n"
" gl_FragColor = vec4(textColor, 1.0) * sampled; n"
"}";
顶点:
x = sWindowWidth / 2 - 1000 / 2;
y = - sWindowHeight / 2 - 172 / 2;
w = 1000;
h = 172;
temp = {
x, y + h, 0.3,
textureColor.x, textureColor.y, textureColor.z,
0, 1,
x, y, 0.3,
textureColor.x, textureColor.y, textureColor.z,
0, 0,
x + w, y, 0.3,
textureColor.x, textureColor.y, textureColor.z,
1, 0,
x, y + h, 0.3,
textureColor.x, textureColor.y, textureColor.z,
0, 1,
x + w, y, 0.3,
textureColor.x, textureColor.y, textureColor.z,
1, 0,
x + w, y + h, 0.3,
textureColor.x, textureColor.y, textureColor.z,
1, 1
};
polygonRenderer.addLoadingPolygon(temp);
PolygonRenderer摘录:
AddLoadingPolygon:
void PolygonRenderer::addLoadingPolygon(std::vector<GLfloat> vertices) {
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat),
vertices.data(), GL_STATIC_DRAW);
if(vertexBuffer == 0){
log("gl vertexBuffer not generated");
}
loadingPolygons.push_back(PolygonRenderObject{
vertexBuffer,
(long)vertices.size() / 8,
0
});
}
AddTexture:
void PolygonRenderer::addTexture(const char* imagePath) {
// Data read from the header of the BMP file
unsigned char header[54]; // Each BMP file begins by a 54-bytes header
unsigned int dataPos; // Position in the file where the actual data begins
unsigned int width, height;
unsigned int imageSize; // = width*height*3
// Actual RGB data
unsigned char * data;
// Open the file
FILE * file = fopen(imagePath,"rb");
if (!file) {
log("Image could not be openedn");
return;
}
if (fread(header, 1, 54, file) != 54) {
log("Not a correct BMP file");
return;
}
if (header[0] != 'B' || header[1] != 'M') {
log("Not a correct BMP file");
return;
}
// Read ints from the byte array
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (imageSize == 0) {
imageSize = width * height;
}
log("Image size: %d, %d", width, height);
if (dataPos == 0) {
dataPos = 54;
}
data = new unsigned char[imageSize];
fread(data, 1, imageSize, file);
fclose(file);
glActiveTexture(GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
checkForGLError("Add texture:");
log("Texture created: %d", textureId);
textures.push_back(textureId);
}
渲染部分:
glUseProgram(shader.get(1));
glBindAttribLocation(shader.get(1), 2, "vertexloc");
glBindAttribLocation(shader.get(1), 3, "vertexcol");
glBindAttribLocation(shader.get(1), 4, "vertexuv");
glBindTexture(GL_TEXTURE_2D, textures[0]);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glUniformMatrix4fv(viewLocation, 1, GL_FALSE,
&frame.combinedMatrix[16 * objects[1].ViewGroup]);
glBindBuffer(GL_ARRAY_BUFFER, objects[1].Buffer);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
0);
glBindBuffer(GL_ARRAY_BUFFER, objects[1].Buffer);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
(GLvoid*)(3 * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER, objects[1].Buffer);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat),
(GLvoid*)(6 * sizeof(GLfloat)));
checkForGLError("In Renderer");
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);
不幸的是,这只是我没有给一个值分配一个统一的值,这导致所有的顶点都指向(0,0)(或类似的东西)。
相关文章:
- OpenGL Glut显示功能在Mojave上显示黑屏
- 我在 OpenGL 中显示图像时遇到问题
- 无法获取要在 OpenGL ES2 中显示的 RGB 纹理
- OpenGL程序不显示三角形
- OpenGL 不显示 2D 线
- 如何显示具有 opengl 纹理的灰度图像
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- 如何使用 openGL 在屏幕上显示加载的纹理
- 在Opengl中绘制一条不显示C++的线
- 写入渲染缓冲区并使用单个渲染调用使用 OpenGL 显示
- OpenGL -- 多个 glDrawArrays() 调用仅显示第一个的结果
- OpenGL显示空白的白屏
- OpenGL符号突出显示visual studio Mac代码中的错误
- 未显示 OpenGL 三角形
- 仅显示 OpenGL 最近的顶点绘制
- 无法在 Qwidget 中显示 OpenGL
- 每次移动(速度)时刷新显示(openGL)
- 如何显示opengl的结果
- 在显示openGL后删除文本
- 更新窗口显示OpenGL/GLUT c++