В настоящее время работает в OpenGL ES 2.0 в native на iOS. По какой-то причине мой квадрат текстуры не будет отображаться, он просто не появится. Я пытаюсь загрузить в файл .bmp для текстуры, и я знаю, что он проходит, потому что функция журнала, которую я помещаю в PolygonRenderer.addTexture
, выдает "Image size: 256, 256"
, который является размером с моей текстурой.Текстура не отображается в OpenGL ES 2.0
Редактировать: My .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 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);
}
Рендер для этой части:
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);
Оказывает ли оно неструктурированное? Я не смотрел на весь код, но одна очевидная проблема заключается в том, что вы вызываете 'glBindAttribLocation()' слишком поздно. Это должно произойти * до * вы связываете программу. –
О, правда? Благодарю. Я попробую это. – iHowell
К сожалению, все еще есть проблемы, но если я переключусь на свой другой шейдер и закомментирую третий атрибут, появится окно. Что касается наличия пустой текстуры, я не думаю, что это происходит, поскольку она правильно загружает изображение, хотя я не совсем уверен, как проверить правильность создания текстуры. – iHowell