How to apply a fragment shader to only one object in OpenGL?

I'm just starting to learn OpenGL. With all the tutorials I've seen, they demonstrate the use of the fragment shader to set the color of all the objects in question. What I have not yet found is how to use the fragment shader on only one of the objects, giving different objects different colors. How do you do this?

To present the background, I draw a simple scene with a house and a road in 2d. I discovered how to set the colors of each of my objects (the main part of the house, windows, etc.) Using a fixed graphics pipeline, I just don’t understand how to set the colors using flash shaders.

Any clarifications would be very helpful, including a fix if I don't understand something.

+6
source share
2 answers

To present the background, I draw a simple scene with a house and a road in 2d. I discovered how to set the colors of each of my objects (the main part of the house, windows, etc.) Using a fixed graphics pipeline, I just don’t understand how to set the colors using flash shaders.

As Robert Rouhani said , make the color uniform and change it for each object.


How to apply fragment shader to only one object in OpenGL?

You can simply change the shader program with glUseProgram and draw calls after using different shaders.

See this: https://gamedev.stackexchange.com/questions/22216/using-multiple-shaders

+3
source

Before drawing an object using glDrawArrays or glDrawElements , pass the color to the shader as a variable.

http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml

GLSL fragment shader example:

 uniform vec4 u_color; void main(void) { gl_FragColor = u_color; } 

I would expand on this answer, but I'm lazy. Hope this helps. There are many tutorials on the Internet, just search for glsl, glUniform4f, etc.

+3
source

All Articles