Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: how can I have the shader handle two different objects?

  1. #1
    Newbie Newbie
    Join Date
    Jan 2017
    Posts
    2

    how can I have the shader handle two different objects?

    I have a cube which has a texture on it (picture on each face of it), which I want to retain.

    Also, I have a sphere that orbits the cube, which I just want to color.

    Now, I can display the textures, or color the items with the fragment/vertex shader, but I would like to change the color of just the sphere, but leave the cube unchanged.

    Ultimately, then, I want to apply lighting and shading to both objects, equally. But, want to start out with baby steps.

    Here are my vertex and fragment shaders

    Code :
    //fragment
    //varying vec4 color;
     
    //image texture
    uniform sampler2D myTexture;
    varying vec2 vTexCoord;
     
    void main (void) {
     
    	//gl_FragColor = vec4(1.0, 0.5, 0.0, 0.0);
    	//one or the other gives me the desired results
    	gl_FragColor = texture2D(myTexture, vTexCoord);
    }

    and here is my vertex shader

    Code :
    //vertex
    //varying vec4 color;
    varying vec2 vTexCoord;
     
    void main(void) {
      vTexCoord = vec2(gl_MultiTexCoord0);
      //vTexCoord = gl_MultiTexCoord0;
      //color = gl_Color;
      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,260
    It looks like you've just about got the vertex color plumbed into your shader already.

    Once you have that, you have options.

    You could pass in a uniform that tells you whether to apply the texture or apply the color. But it might be simpler to just apply both, modulating them together (e.g. color * texture). Consider that 1 * X = X and X * 1 = X.

  3. #3
    Newbie Newbie
    Join Date
    Jan 2017
    Posts
    2
    Thanks for the reply Dark Photon,

    I am not familiar enough with uniforms to know exactly what they are doing.

    I assume I am close, because depending on which gl_FragColor I enable, I get the desired result.

    But, how exactly would I go about implementing that?

    I will mediate on it, in the interim=)

    thanks!
    Last edited by jalisco; 01-24-2017 at 12:11 PM.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,260
    Quote Originally Posted by jalisco View Post
    I am not familiar enough with uniforms to know exactly what they are doing.
    Here's a wiki page in the OpenGL Wiki on them: Uniform (GLSL). Basically, they're values you can pass in from your C++ program that are effectively constant for all of the primitives in a draw call. You can use them to pass in state that doesn't need to change vertex-to-vertex or fragment-to-fragment.

    However, if you just use the modulate approach, you probably don't need one.

    If you don't have a good book on OpenGL programming, I'd suggest you pick one up. Alternatively, you can consult the online OpenGL Wiki, the OpenGL man pages, and various OpenGL tutorials around the net.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •