Hi,
I am trying to create a very simple texture with own values. In other projects, I already used textures, but always used a loader (like SOIL). But now, it just doesn’t work.
This is the important part of my main code:
vector<float> vertices = { 0.0f, 0.0f, 1280.0f, 0.0f, 1280.0f, 20.0f, 0.0f, 20.0f };
vector<float> texcoord = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f };
unsigned char textureData[] = { 255, 255, 255, 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255, 255 };
glUseProgram(simpleShader->program);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(float), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glGenBuffers(1, &tcbo);
glBindBuffer(GL_ARRAY_BUFFER, tcbo);
glBufferData(GL_ARRAY_BUFFER, texcoord.size()*sizeof(float), texcoord.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &fontTex);
glBindTexture(GL_TEXTURE_2D, fontTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(glGetUniformLocation(simpleShader->program, "simpleTex"), 1);
vMatrix = glm::ortho(0.0f, 1280.0f, 720.0f, 0.0f, 0.00001f, 1000.0f);
glUniformMatrix4fv(glGetUniformLocation(simpleShader->program, "vMatrix"), 1, GL_FALSE, &vMatrix[0][0]);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_QUADS, 0, 4);
These are my shaders:
//Vertex shader
#version 430 core
layout (location = 0) in vec2 vPosition;
layout (location = 1) in vec2 vTexCoord;
out vec2 fTexCoord;
uniform mat4 vMatrix;
void main()
{
gl_Position = vMatrix * vec4(vPosition, 0.0, 1.0);
fTexCoord = vTexCoord;
}
//Fragment shader
#version 430 core
in vec2 fTexCoord;
out vec4 outColor;
uniform sampler2D simpleTex;
void main()
{
outColor = texture(simpleTex, fTexCoord);
}
But this doesn’t work and just a big black box shows up on the top of the screen (nothing white like specified in the texture data).
I already tried glGetError() after every function related to textures, but the return value is always 0.
Also, I read back the data passed with glTexImage2D to OpenGL and it just returns 16 zeroes (so somewhere in this function has to be the issue). This is the code I used for this debugging purpose:
unsigned char *buffer = (unsigned char*)calloc(16, 1);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_RGBA, buffer);
FILE *test = fopen("out.raw", "wb");
fwrite(buffer, 1, 16, test);
fclose(test);
So, what do I have to change in my main code to make OpenGL show my texture properly? Thanks for your help