Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: GLSL shading: The entire screen is colored instead of just a quad

  1. #1
    Newbie Newbie
    Join Date
    Feb 2015
    Posts
    2

    GLSL shading: The entire screen is colored instead of just a quad

    The shaders I've done shades the entire screen in red instead of shading only the quad I'm drawing. The way I've done things in my code is described here.

    Code :
    #version 140 core
    // vertex shader
    in vec3 position;
    out vec3 color;
     
    void main(void)
    {
        gl_Position = vec4(position,1.0);
        color = vec3(1.0,0.0,0.0);
    }
     
    #version 140 core
    // fragment shader
    in vec3 color;
     
    out vec4 outColor;
     
    void main(void)
    {
        outColor = vec4(color,0.1);
    }
    then the order in which I do the vao vbo are
     
    /// vao buffer
    glGenVertexArrays(1,&vao);
    glBindVertexArray(vao);
     
    /// vbo - for vertices
    glGenBuffers(1,&vbo1);
    glBindBuffer(GL_ARRAY_BUFFER, ... );
    glBufferData(GL_ARRAY_BUFFER, ...);
    glVertexAttribPointer(0, 3, GL_FLOAT, ... );
    glEnableVertexAttribArray(0);
    glBindBuffer(0);
     
    /// vbo for indices
    glGenBuffers(1,&vbo2);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ... );
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, ...);
    glVertexAttribPointer(1, 0, GL_UNSIGNED_INT);
    glEnableVertexAttribArray(1);
    glBindBuffer(0);

    the shader programs are compiled, and then bound with the vertex location using

    Code :
    glBindAttribLocation(progID, 0, "position");

    I suppose the "position" is same as the variable name in vertex shader and it is bound to the 0th location as the vertices of the polygon are at 0th location in the VAO Buffer. What should I to resolve the problem here?

    These are the values of vertices I'm using

    Code :
    float Vertices[] = { -5.0f, 5.0f, 0.0f, -5.0f, -5.0f, 0.0f, 5.0f, -5.0f, 0.0f, 5.0f, 5.0f, 0.0f };

    I rotate the quad using
    Code :
    glRotate
    function before I render, If I dont enable shader I can see the quad rotating about Y axis. If I enabled shader, the entire screen is red

  2. #2
    Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    281
    Quote Originally Posted by indiebuffer View Post
    Code :
    glBindAttribLocation(progID, 0, "position");

    I suppose the "position" is same as the variable name in vertex shader and it is bound to the 0th location as the vertices of the polygon are at 0th location in the VAO Buffer. What should I to resolve the problem here?
    What problem? This function call binds "position" to vertex attribute array 0. Above you configured the vertex attribute 0 to be read from the specifed VBO.

    Quote Originally Posted by indiebuffer View Post
    These are the values of vertices I'm using

    Code :
    float Vertices[] = { -5.0f, 5.0f, 0.0f, -5.0f, -5.0f, 0.0f, 5.0f, -5.0f, 0.0f, 5.0f, 5.0f, 0.0f };

    I rotate the quad using
    Code :
    glRotate
    function before I render, If I dont enable shader I can see the quad rotating about Y axis. If I enabled shader, the entire screen is red
    There is your problem. You are not applying ANY transformations WHATSOEVER in your vertex shader. The input of the vertex shader are
    the vertices as you specify them right here. Your shader passes the vertices on as is. The vertex shader is expected to apply transformations
    to the vertices and emit the vertices in clip space (before perspective divide), so when those +/-5.0 values are divided by 1.0, they are still
    in the +/-5.0 range and WAY out of the unit cube, covering the entire screen. Your call to glRotate has no effect.

    Your vertex shader has to multiply the vertex position by a modelview and projection matrix. Those are usually passed as uniforms, or in uniform
    buffers. In compatibility profile (legacy OpenGL(R)) that you are using, there should be a built in function called ftransform( ) that does that for you,
    using the matrices from the GL matrix stack.

  3. #3
    Newbie Newbie
    Join Date
    Feb 2015
    Posts
    2
    Quote Originally Posted by Agent D View Post
    Your vertex shader has to multiply the vertex position by a modelview and projection matrix. Those are usually passed as uniforms, or in uniform
    buffers. In compatibility profile (legacy OpenGL(R)) that you are using, there should be a built in function called ftransform( ) that does that for you,
    using the matrices from the GL matrix stack.
    Thank you I got it now. I changed the vertices from 5.0 to 0.5 and I can see it shading the quad, wanna apply the transforms and do other stuffs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •