Simple Basic Question

How can I pass a variable to the shader? How can I call/use a (different) shader?

I have this text: Hello World

And it is made from one long texture, now I want ‘hello’ to be red color and ‘world’ to be green. How can I tell the shader which part to take and which color to use? I need to pass a variable, right?

Your question may be basic and simple, but that doesn’t mean the answer is simple or short…

There are a few ways to pass values to shader variables. The method depends on the type of shader variable. There are vertex variables (aka attributes), and per draw command variables (aka uniforms). For what you describe, you should use a shader uniform variable, because you are not describing something specific to any vertex.

The way you use different shaders, is the way you use any shader: you call glUseProgram(). If you want/need more than one shader, then write and compile and link each of them once. When you are ready to draw with one of them, call glUseProgram() to specify which you want to use. You can draw parts of a scene with one shader and other parts with another shader program. You can also draw some frames with one shader and other frames with another shader program.

There are several ways you might approach what you describe. I suggest reading and studying an introductory tutorial, because your questions indicate that you need to get the overall picture yet, rather than just one or two details. Here’s an introductory tutorial that does something similar to what you describe:
http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html

Thanks for the help.

Please explain more, I can nowhere find how to do this. I only read about glGetUniformLocation which I don’t understand. I want something like this (but don’t know how to do it):


The main c++ program:

vec4 color1 = vec4( 0.0/255, 40.0/255, 68.0/255, 1.0 ); 
vec4 color2 = vec4( 0.0/255, 114.0/255, 194.0/255, 1.0 ); 
...etc...

variable coloring_place1 = gl_FragCoord.y < 50; //for color 1
variable coloring_place1 = gl_FragCoord.x < 50;

variable coloring_place2 = gl_FragCoord.y < 100; //for color 2
variable coloring_place2 = gl_FragCoord.x < 100;
...etc...

Shader fragment:
void main()
{

vec4 color = texture2D(myTexture, vTexCoord); 

 
if (coloring_place1){
	color = color1; 
        
}

if (coloring_place2){
	color = color2; 
        
}

if (coloring_place3){
	...etc....

gl_FragColor = color;
}

You said you can find nowhere how to pass values to shader variables. Did you read the tutorial that I provided the link to?

I really think you need to study several tutorials and try to understand what they are doing. I don’t consider it wise to jump into programming without first having some understanding of what you are trying to do or how things work. Probably any introductory OpenGL shader language book would also be helpful for you.

Looking at your sample code, I can see no reason why you have a texture at all, or else why you have anything other than a texture. Ultimately, it looks like you are trying to assign a color based on the location within a triangle.

The sample code is a mechanism/idea, not correct code of course.

Thanks for the amazing help…

I’m off to reading a shader book.

The sample code is a mechanism/idea, not correct code of course.

The mechanism/idea of your sample code made no sense and indicated a fundamental lack of understanding. That’s what my comment tactfully informed you of, of course.

Thanks for the amazing help…

I do hope you’re not being sarcastic…

I’m off to reading a shader book.

Great! Then you will be getting the help you need from an appropriate source. Who knows? Maybe once you’re an expert you’ll even come back here and provide amazing help for free to people just starting out.

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