У меня есть код, который из поверхности делает текстуру OpenGL: SDL_Color color = { 255, 255, 255, 0 }; SDL_Surface* surfaceText = TTF_RenderText_Solid(ourFont, "simple text", color); glGenTextures(1, &ret); glBindTexture(GL_TEXTURE_2D, ret); glTexImage2D(GL_TEXTURE_2D, 0, 3, surfaceText->w, surfaceText->h, 0, GL_RGB, GL_UNSIGNED_BYTE, surfaceText->pixels);
SDL_Color color = { 255, 255, 255, 0 };
SDL_Surface* surfaceText = TTF_RenderText_Solid(ourFont, "simple text", color);
glGenTextures(1, &ret);
glBindTexture(GL_TEXTURE_2D, ret);
glTexImage2D(GL_TEXTURE_2D, 0, 3, surfaceText->w, surfaceText->h, 0, GL_RGB, GL_UNSIGNED_BYTE, surfaceText->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
SDL_FreeSurface(surfaceText);
... и есть код, который рисует эту текстуру:
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glViewport(0, 0, width, height);
glClearColor(1.f, 1.f, 0.f, 1.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//GLint location = glGetUniformLocation(GrapicsPipelineShaderProgram, "dx");
//glUniform1f(location, dx);
glUniform1i(glGetUniformLocation(GrapicsPipelineShaderProgram, "sampTexture"), 0);
GLCheck(glUseProgram(GrapicsPipelineShaderProgram));
GLCheckErrorProgram(GrapicsPipelineShaderProgram, "first");
glBindVertexArray(VertexArrayObject);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ret);
glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, 1);
glBindVertexArray(0);
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
а также шейдеры
Вершинный:
#version 410 core
layout(location=0) in vec2 position;
layout(location=1) in vec2 texCoord;
out vec2 texCoordinates;
void main()
{
texCoordinates = texCoord;
gl_Position = vec4(position.x + gl_InstanceID / 3, position.y, 0.0f, 1.0f);
}
И Фрагментный:
#version 410 core
in vec2 texCoordinates;
uniform sampler2D sampTexture;
out vec4 color;
void main()
{
color = texture(sampTexture, texCoordinates);
}
Также я создаю VAO и EAB:
GLfloat vertexData[16] = {
0.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 1.0f,
0.5f, 0.5f, 1.0f, 1.0f
};
GLuint IndexBufferData[6] = {0, 1, 2, 1, 3, 2};
glGenVertexArrays(1, &VertexArrayObject);
glBindVertexArray(VertexArrayObject);
glGenBuffers(1, &VertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
glGenBuffers(1, &IndexBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), IndexBufferData, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, sizeof(GLfloat) * 4, (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(GLfloat) * 4, (GLvoid*)(sizeof(GLfloat) * 2));
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
На поверхности есть текст
На экране появляется только черный прямоугольник (2 треугольника) без текстуры