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: Optimized uniform

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    13

    Optimized uniform

    I have shader
    vert:
    Code :
    uniform vec3 u_Camera;
    uniform float u_WaterLevel;
    uniform vec3 u_LightPosition;
     
    in vec3 in_Vertex;
    in vec3 in_Normal;
     
    out vec3 vectorLightSource;
    out vec3 transformedNormal;
     
    void main (void) {
     
    	vec3 out_Vertex = in_Vertex;
    	if ( (in_Vertex.y - u_WaterLevel)*(u_Camera.y - u_WaterLevel) < 0 ) {
    		float dist = distance(in_Vertex, u_Camera);
    		float lnt = distance( vec2(in_Vertex.x,in_Vertex.y), vec2(u_Camera.x,u_Camera.y));
    		float hgt = abs(u_Camera.y - in_Vertex.y);
     
    		float sinAngleA = hgt/dist;
    		float angleA = asin (sinAngleA);
    		float waterHgt = abs(u_Camera.y - u_WaterLevel);
    		float waterLnt = waterHgt/tan(angleA);
     
    		float angleB = asin ( sinAngleA/1.3330);
    		float dLnt = u_WaterLevel*tan(angleB);
     
    		float imagineLnt = abs(waterLnt+dLnt);
     
    		float dX = in_Vertex.x - u_Camera.x;
    		float dZ = in_Vertex.z - u_Camera.z;
     
    		dX = imagineLnt*dX/lnt;
    		dZ = imagineLnt*dZ/lnt;
     
    		out_Vertex = vec3 (u_Camera.x+dX,in_Vertex.y,u_Camera.z+dZ);
    	}
    	vec3 vertexPos = (gl_ModelViewMatrix * vec4(out_Vertex,1)).xyz;
    	vectorLightSource = normalize ( u_LightPosition - out_Vertex );
    	transformedNormal = normalize ( gl_NormalMatrix * in_Normal );
    	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(out_Vertex,1);
     
    }

    frag:
    Code :
     
    uniform vec3 u_Camera;
    uniform float u_WaterLevel;
    uniform vec3 u_LightPosition;
     
    in vec3 transformedNormal;
    in vec3 vectorLightSource;
     
    void main (void) {
     
    	vec3 diffColor = vec3 (1,1,0);
    	vec3 n2   = normalize ( transformedNormal );
    	vec3 l2   = normalize ( vectorLightSource );
    	gl_FragColor = vec4 ( diffColor * max ( dot ( n2, l2 ), 0.0 ), 1 ) ;
    	gl_FragColor = vec4(transformedNormal,1);
     
    }
    u_LightPosition and in_Normal used to make transformedNormal and vectorLightSource in vertex shader, which pass to fragment shader. The problem is thet u_LightPosition and in_Normal are optimized out by GLSL Compiler and glGetUniformLocation return me -1. But in fact I'm use them. What is the magic here?
    Sorry for my english.

  2. #2
    Intern Contributor
    Join Date
    Aug 2009
    Posts
    76
    The problem seems to be

    Code :
    gl_FragColor = vec4 ( diffColor * max ( dot ( n2, l2 ), 0.0 ), 1 ) ;     
    gl_FragColor = vec4(transformedNormal,1);

    The first gl_FragColor is useless, it is overwritten by the second. However the second one does not use l2 and thus vectorLightSource is not used. Also, the latter is the only thing needing u_LightPosition. So it's all optimized away. Actually quite a clever compiler

  3. #3
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    You aren't using them. Look at your final line

    gl_FragColor = vec4(transformedNormal,1);
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    13
    Oh my fail... I must to sleep more((
    Sorry for my english.

Posting Permissions

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