Hi,
I’m trying to draw an array of pixel colors to the screen. It seems that the best way to do this is to draw a screen sized (covers the entire screen) triangle and apply to it a texture that stores the color values (if there is a better way please tell me). Drawing the triangle with a single color works, but when I try to use the texture I just get a black screen. I don’t know whether the problem is with the shader or some other part of the code.
Here is the fragment shader where I try to use the texture:
#version 330
out vec3 outputColor;
uniform sampler2D myTextureSampler;
void main(){
vec2 texCoord;
texCoord.x = gl_FragCoord.x/1280;
texCoord.y = gl_FragCoord.y/960;
outputColor = texture(myTextureSampler, texCoord).rgb;
}
How I think this should work: Because texture coordinates have range [0,1] I have to divide the gl_FragCoord which is in window space by window size (1280960 in this case) to get the right texture coordinates (the texture is also 1280960).
This is where I copy the pixel color values to the texture:
textures.push_back(0);
glGenTextures(1, &textures[0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, &pixels[0]);
glBindTexture(GL_TEXTURE_2D, 0);
And here is the rendering part:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderPrograms[0].program);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glBindVertexArray(vaos[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
window.display();
Pixels is std::vector<Vector3> (Vector3 has members float x,y,z) and its size is 1280*960, which is also the size of the window. I know pixels contains the right values because when I save them as .bmp I get the image I want. I’m using SFML for creating the OpenGL window. OpenGL version is 4.2.