Jump to content
  • Advertisement
Sign in to follow this  
AmzBee

[Solved] Basics, passing color through a vertex shader.

This topic is 1962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I'm having a bit of an issue with shader's in JOGL 2 for Java (JRE 1.7) and I'm hoping you can help. I have an interleaved and un-indexed VBO containing 3 floats per position axis and 3 floats per colour component, I'm able to draw 4 quads absolutely fine if I use the traditional methods for setting up the projection matrix (i.e. glMatrixMode, glLoadIdentity, glOrtho2D). 

When I switch to using my own shader however everything draws fine projection wise, but I am unable to figure out how to pass the vertex colour from vert to frag shaders. So far I have tried various combinations switching between varying and in/out statements to no avail, I am sure I am just missing some syntactical knowledge here and would appreciate if someone could point me in the right direction.

 

Here is the fragment shader I am using:

 

in vec3 diffuse;

void main() {
    gl_FragColor = vec4(diffuse, 1.0);
}

 

 

And here is the vertex shader I am using:

 

uniform mat4 projMatrix;

in vec3 position;
in vec3 color;

out vec3 diffuse;

void main() {
    gl_Position = projMatrix * vec4(position.xyz, 1.0);
    diffuse = vec3(1,1,1);
    // diffuse = color;
}

 

 

Notice the commented out line of code, if I uncomment it and remove the line above the quads I am drawing change from opaque white to transparent (I've made sure by changing the background clear colour). Again I am sure this is just a lack of knowledge on my part but I've been going crazy trying to solve this all day so thanks in advance to anyone who can help me out.

 

 

Aimee

Share this post


Link to post
Share on other sites
Advertisement

Solved it, problem was that I forgot to use gl_Color the correct way, here is my modified vertex shader:

niform mat4 projMatrix;
in vec3 position;
out vec4 diffuse;

void main() {
	diffuse = gl_Color;
    gl_Position = projMatrix * vec4(position, 1.0);
}

 

and the matching frag shader:

in vec4 diffuse;

void main() {
    gl_FragColor = diffuse;
}

 

Also I was getting the zNear and zFar variables in my orthographic projection calculation the wrong way round, it's funny what a lack of sleep can do to the brain lol.

 

Aimee

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!