Results 1 to 3 of 3

Thread: Drawing a textured screen-sized triangle

  1. #1
    Newbie Newbie
    Join Date
    Mar 2014
    Posts
    4

    Drawing a textured screen-sized triangle

    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:
    Code :
    #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 (1280*960 in this case) to get the right texture coordinates (the texture is also 1280*960).

    This is where I copy the pixel color values to the texture:
    Code :
    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:
    Code :
    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.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    463
    By default OpenGL uses mipmaps for most texture types (except for rectangle textures), so it is expecting you to provide them & since you don't your texture isn't complete. One way to tell OpenGL that you don't want to use mipmaps is to include:
    Code :
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    In my opinion using mipmaps shouldn't have been the default value since it's an additional obstacle to learning OpenGL, and provides some complexity to the rules for what the initial parameters will be when the newly generated texture is first bound to a particular texture target, but it's late for it to change now.

  3. #3
    Newbie Newbie
    Join Date
    Mar 2014
    Posts
    4
    Thank you very much Dan, now it works correctly!

Similar Threads

  1. Can't get textured triangle to display.
    By nightmareci in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-16-2013, 12:25 PM
  2. Black taskbar-sized strip at top of screen
    By Nexuapex in forum OpenGL: Windows
    Replies: 0
    Last Post: 04-15-2010, 03:56 PM
  3. fullscreen vs screen-sized (undecorated) window
    By jeremy barrett in forum OpenGL: Windows
    Replies: 0
    Last Post: 11-20-2009, 09:23 AM
  4. Seamless textured VBO triangle strip
    By Stuart McDonald in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-15-2006, 09:36 AM
  5. rendering a screen sized quad
    By JanHH in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 07-02-2003, 02:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean