Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

opengl

I'm just beginning to learn OpenGL. With all of the tutorials I've seen, they demonstrate using a fragment shader to set the color of all the objects in view. What I haven't found yet is how you would use a fragment shader on just one of the objects, giving different objects different colors. How do you do that?

To provide background to the question, I'm drawing a simple scene with a house and a road in 2d. I have discovered how to set the colors of each of my objects (the main body of the house, the window, etc) using the fixed graphics pipeline, I just don't understand how to set the colors using fragment shaders.

Any clarification would be greatly appreciated, including correction if I'm misunderstanding something.

like image 797
xtraorange Avatar asked Nov 06 '12 04:11

xtraorange


People also ask

How many times does fragment shader run?

In this case, your fragment shader runs once for every fragment within that quad. For this program, that means 100 * 100 = 10000 times, once for every pixel on your screen. However, not every fragment rendered in the shader has to be displayed on the screen.

Is fragment shader same as pixel shader?

A fragment shader is the same as pixel shader. One main difference is that a vertex shader can manipulate the attributes of vertices. which are the corner points of your polygons. The fragment shader on the other hand takes care of how the pixels between the vertices look.

How does a fragment shader work?

A Fragment Shader is the Shader stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value. The fragment shader is the OpenGL pipeline stage after a primitive is rasterized. For each sample of the pixels covered by a primitive, a "fragment" is generated.

Can you send data from fragment shader to vertex shader?

All shader stages can pass data between them by using input and output variables. If in the vertex shader we create an output variable, we will be able to read it on the fragment shader as an input variable.


2 Answers

To provide background to the question, I'm drawing a simple scene with a house and a road in 2d. I have discovered how to set the colors of each of my objects (the main body of the house, the window, etc) using the fixed graphics pipeline, I just don't understand how to set the colors using fragment shaders.

As RobertRouhani said, make the color a uniform and change it for each object.


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

You can simply change the shader program with glUseProgram and rendering calls after it will use the different shader.

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

like image 107
Pubby Avatar answered Nov 14 '22 20:11

Pubby


Before you draw an object with glDrawArrays or glDrawElements, pass the color to a shader as a variable.

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

Sample GLSL fragment shader:

uniform vec4 u_color;

void main(void)
{
     gl_FragColor = u_color;
 }

I would expand on this answer but I am being lazy. Hope it helps somewhat. There are a lot of tutorials online, just search for glsl, glUniform4f, etc.

like image 20
user8709 Avatar answered Nov 14 '22 20:11

user8709