GLSL anyone?

This is a discussion on GLSL anyone? within the Game Programming forums, part of the General Programming Boards category; I'm starting to learn GLSL, which has some very clever math features, but since I am not so clever with ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    GLSL anyone?

    I'm starting to learn GLSL, which has some very clever math features, but since I am not so clever with math, it's a little difficult.

    You will have to understand GLSL in order to answer this, I'm hoping someone does.

    I have a vertex shader which rotates an object; it passes on a float value, "Diffuse", to the fragment shader which is multiplied with the color value to produce lighting.
    Code:
    void rotateY (inout vec4 vert, float rads);
    
    uniform float YRot;
    varying float Diffuse;
    
    void main() {
    	vec3 vnormal = normalize(gl_NormalMatrix * gl_Normal);
    	Diffuse = max(dot(vnormal, gl_LightSource[0].position.xyz), 0.0);
    	rotateY (gl_Vertex, YRot);
    	gl_Position = ftransform();
    }
    
    
    void rotateY (inout vec4 vert, float rads) {
    	vec4 old = vert;
    	mat2 rotY = mat2(cos(rads), -sin(rads), sin(rads), cos(rads));
    	vert.xz = old.xz * rotY;
    }
    The problem is, the light on the object so rotated moves with the object.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    The light is being calculated prior to rotating, but is never calculated with the rotated vertices, so naturally the light would rotate "with" the object. I'm not completely sure if there's a good way to do this entirely inside GLSL (although I am a bit rusty) - is there any reason in particular why you want to rotate the object in the vertex shader?
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by psychopath View Post
    1) Recalculate the vertices using a C version of the GLSL rotate function above, and then modify the buffer data in video memory. Major and maybe insurmountable drawback: you then have to recalculate all the normals, and modify those in the buffer too. Advantage: it could be done once, rather than every frame. However, this would mean using the CPU to perform the calculations.

    If I can just rotate the normal using the same algorithm, then it won't be so hard, but this did not work in the shader, so I haven't tried it outside of that yet.
    Like, OMG, it worked. Whew! I needed some encouragement here.

    So now I can do rotation and transformation on the video buffer data without having to call any standard functions, before the shaders are executed.

    Code:
    void rotate (glThing *obj, float rads, int a, int b) {
    	int i;
    	float x, z, cs = cos(rads), sn = sin(rads);
    	for (i=0; i<obj->len; i+=3) {
    		x = obj->data[i+a];
    		z = obj->data[i+b];
    		obj->data[i+a] = x*cs - z*sn;
    		obj->data[i+b] = x*sn + z*cs;
    	}
    	glBindBuffer(GL_ARRAY_BUFFER, obj->VBO);
    	glBufferSubData(GL_ARRAY_BUFFER,0,obj->len*sizeof(GLfloat),obj->data);
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    Last edited by MK27; 12-29-2009 at 12:22 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I fail to see how this benefits you. All the vertex shader is supposed to do is transform the vertices. In more advanced shaders it might perform some vertex texturing but all in all it is a simple transformation process. Why are you rotating objects inside of a vertex shader?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bubba View Post
    I fail to see how this benefits you. All the vertex shader is supposed to do is transform the vertices. In more advanced shaders it might perform some vertex texturing but all in all it is a simple transformation process. Why are you rotating objects inside of a vertex shader?
    Well, that's good to hear. I don't really see the point either, which is why I'm happier with my second idea.

    I was just slightly confused by the fact that GL 3.2 removes all the "fixed functionality" stuff, inc all functions that provide any kind of rotation or translation of objects -- glRotate(), glTranslate, et al. They also remove the pushing and popping of state matrixes, so the only way to duplicate those functions exactly (which operate per frame on static object data, ie, the data remains the same, you keep track of rotation relative to world) is in the vertex shader.
    Last edited by MK27; 12-29-2009 at 04:42 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    in modern games, and the like, transformations are calculated in shaders.
    This is because shaders are run by the GPU which is specifically designed to compute 3D math and the like, so it excels at jobs like calculated new coordinates from a given transformation matrix.
    In comparison to a GPU a CPU sucks at these jobs hence the move from fixed functionality to a programmable pipeline (shaders).

    Short of it: you took the best path

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shaders: directx/opengl
    By Raven Arkadon in forum Game Programming
    Replies: 5
    Last Post: 08-24-2006, 09:19 AM
  2. GLSL: polygon soup on nVidia cards.
    By psychopath in forum Game Programming
    Replies: 9
    Last Post: 04-23-2006, 07:08 AM
  3. GLSL lighting (again)
    By psychopath in forum Game Programming
    Replies: 6
    Last Post: 12-19-2005, 12:34 PM
  4. Replies: 6
    Last Post: 11-12-2005, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21