PDA

View Full Version : Simple OpenGL Texture not working



TheGLJ
03-13-2015, 09:22 AM
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(simpleShad er->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 :)

GClements
03-13-2015, 02:16 PM
glEnable(GL_TEXTURE_2D);

This is only relevant to the fixed-function pipeline. It doesn't do anything if you're using a fragment shader, and isn't valid if you're using OpenGL 3+ core profile.




glUniform1i(glGetUniformLocation(simpleShader->program, "simpleTex"), 1);

Unless you called glActiveTexture(GL_TEXTURE1) before binding the texture, this is incorrect; the value should be zero.



I already tried glGetError() after every function related to textures, but the return value is always 0.

It isn't necessary to call glGetError() after each function. Once an error occurs, the next call to glGetError() will report it. Nothing (other than calling glGetError()) will clear the error status. A single call to glGetError() at the end of initialisation will suffice to determine whether any error occurred. You only need additional calls if you need to determine exactly which function(s) generated an error.




glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_RGBA, buffer);

This should be


glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

TheGLJ
03-24-2015, 09:53 AM
Ahh, now it works! My only "real" mistake was the 1 I passed to the texture sampler. Thank you very much for your help and the clarifications about other OpenGL functions!