目前在iOS上使用原生OpenGL ES 2.0。出于某种原因,我的纹理四元组不会渲染,它只是不会出现。我尝试在.bmp文件中加载纹理,我知道它会通过,因为我放入rwlock_t myrwlock = RW_LOCK_UNLOCKED;
的日志函数打印出PolygonRenderer.addTexture
,这是我纹理的大小。
编辑:我的"Image size: 256, 256"
纹理以R8,G8,B8格式保存。
VertexShader:
.bmp
Fragment Shader:
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"
"}";
PolygonRenderer摘录:
AddLoadingPolygon:
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);
AddTexture:
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
});
}
渲染此部分:
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 opened\n");
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);
}
答案 0 :(得分:0)
不幸的是,这只是我没有为一个值赋予一个统一,这导致所有顶点都转到(0,0)(或类似的东西)。