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 2 of 2

Thread: Rendering a 3D graphic

  1. #1
    Junior Member Newbie
    Join Date
    May 2009
    Posts
    9

    Question Rendering a 3D graphic

    Hello all,

    I'm developing a shader to render a graphic so I'm trying to make specific heigths (position.y) of specific colors.

    I've ran into a problem, the heights go from 0 to 255 and I've devided this into 4 groups of height that are a gradient between different colors. Example: values between 0 and 63.75 have colores between (0, 0, 255) and (0, 255, 255).
    I have very abrupt transformations between strips:
    Click image for larger version. 

Name:	badshader_Screenshot.jpg 
Views:	69 
Size:	8.7 KB 
ID:	758

    The Vertex shader (the part about diffuse color):
    Code :
    float interpolate (float z, float lowerBound, float maxRGB){
        float scaled = z - lowerBound;
        float interval = 63.75;
        return (scaled*maxRGB)/interval;
    }
     
    void main(){
        //Transformation of the object space coordinate to projection space
        //coordinates.
        //- gl_Position is the standard GLSL variable holding projection space
        //position. It must be filled in the vertex shader
        //- To convert position we multiply the worldViewProjectionMatrix by
        //by the position vector.
        //The multiplication must be done in this order.
        position = g_WorldMatrix * vec4(inPosition, 0.0);
        vec4 pos = vec4(inPosition, 1.0);
        gl_Position = g_WorldViewProjectionMatrix * pos;
        //________________________________________________________________________
        vec4 diffuseColor;
        float i;
        float alpha = 1;
        float interval = 255/4;
        int maxRGB_1 = 1;
        int maxRGB_255 = 255;
        if(position.y < interval){                               // blue to cyan
            i = interpolate (position.y, 0, maxRGB_1);
            diffuseColor = vec4(0, i, maxRGB_255, alpha);
        }
        else if(position.y < interval*2){                        // cyan to yellow
            i = interpolate (position.y, interval*1, maxRGB_1);
            diffuseColor = vec4(i, maxRGB_255, 1-i, alpha);
        }
        else if(position.y < interval*3){                        // yellow to red
            i = interpolate (position.y, interval*2, maxRGB_1);
            diffuseColor = vec4(maxRGB_255, 1-i, 0, alpha);
        }
        else{                                                   // red to dark red
            i = interpolate (position.y, interval*3, 0.5);
            diffuseColor = vec4(1-i, 0, 0, alpha);
        }
        gl_FrontColor = diffuseColor;
        //________________________________________________________________________

    In the Fragment Shader I just have:
    Code :
    vec4 diffuseColor = gl_Color;

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    Have you tried moving the vertex shader color calculations to fragment shader ?

Tags for this Thread

Posting Permissions

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