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.

#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

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

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

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

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=indiebuffer;1264375]
These are the values of vertices I’m using

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

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[/QUOTE]
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.

[QUOTE=Agent D;1264379]
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®) 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.[/QUOTE]

Thank you :slight_smile: 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 :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.