Jump to content


left handed system to right handed system


7 replies to this topic

#1 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 06 August 2007 - 06:03 PM

Can some one help me convert the following code from the left handed coordinate system to right handed coordinate system (the one that OpenGL uses).

I am kindda stuck into it for quite some time now:

below is the working code in the left handed coordinate system:
        glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CW);

        projection = matrix4x4:: perspectiveFovLHGL(fov, (float)w / h, 
		zNear, zFar);
        glLoadMatrixf(projection);

        glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
        glTranslatef(0,0, 300);    // should be (0,0,-300) in RightHanded system
	glScalef(256, 256, 128);

        glViewport(0, 0, fbWidth, fbHeight);
	glUseProgram(0);

	
	// render back depth into framebuffer 0
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer0);
	glDepthFunc(GL_GREATER);
	glClearDepth(0.0f);
	glCullFace(GL_BACK);
	
	glClear(GL_DEPTH_BUFFER_BIT);

	if (volume.cubeQuads > 0) {
		glBindBuffer(GL_ARRAY_BUFFER, volume.vbCubes);
		glVertexPointer(3, GL_FLOAT, 0, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, volume.ibCubes);
		glEnableClientState(GL_VERTEX_ARRAY);

		glDrawElements(GL_QUADS, volume.cubeQuads * 4, GL_UNSIGNED_INT, 0);

		glDisableClientState(GL_VERTEX_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	}




I have to use glTranslate(0,0,-300) rather then glTranslate(0,0,300) in line 9.

I tried doing it by changing the following lines:
glFrontFace(GL_CW); TO glFrontFace(GL_CCW);

projection = matrix4x4:: perspectiveFovLHGL(fov, (float)w / h,
zNear, zFar);
TO
projection = matrix4x4:: perspectiveFovRHGL(fov, (float)w / h,
zNear, zFar); (is a correct library call)

glTranslatef(0,0, 300); TO glTranslatef(0,0, -300);


AND

case 0: // render back depth into framebuffer 0
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer0);
	glDepthFunc(GL_GREATER);
	glClearDepth(0.0f);
	glCullFace(GL_BACK);
	break;

TO

case 0: // render back depth into framebuffer 0
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer0);
	glDepthFunc(GL_LESS);
	glClearDepth(0.0f);
	glCullFace(GL_FRONT);
	break;


it does not work :(

I posted this on another forum but could not get any help :(

#2 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 06 August 2007 - 09:43 PM

What are your znear and zfar values?
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#3 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 07 August 2007 - 09:01 AM

znear and zfar are 1 and 1024 respectively.

#4 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 07 August 2007 - 09:48 AM

It's been a long time since I've done anything with opengl, but shouldn't those values be -1 and -1024 for a righthanded system?
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#5 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 07 August 2007 - 10:05 AM

actually znear and zfar have to be always positive in openGL.

they are used in the projection matrix later on and thus setup correctly by the openGL itself.

It seems to me that the problem exists somewhere in the following lines

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer0);

	glDepthFunc(GL_LESS);

	glClearDepth(0.0f);

	glCullFace(GL_FRONT);


I cannot figure out how can I set them up. glClearDepth only gets valuess betwee [0, 1].

thanks for the reply

#6 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 907 posts

Posted 07 August 2007 - 11:36 AM

With that setup, you are telling the GPU to only accept pixels with a depth value less than zero, ie. behind the near plane :)

Using glClearDepth(1.0f) and glDepthFunc(GL_LEQUAL) should do the trick.
"Stupid bug! You go squish now!!" - Homer Simpson

#7 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 07 August 2007 - 12:55 PM

it does not work either :(

in the RHS my objects are on the negative side of the z-axis. So this means that negative values are expected to be stored. openGL system seems to look between znear and zfar, which are both positive.

i think I have to live with the LHS :(

thanks for the help

#8 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 07 August 2007 - 04:06 PM

In OpenGL it's expected that visible objects will have negative Z in eye space, since the Z axis points out of the screen. (The znear and zfar values should be positive nonetheless.)

In any case, I believe you'll need to scale everything by (1, 1, -1), won't you?

Also, don't change CullFace to front, keep it as back. The culling mode is already switched when you change FrontFace from CW to CCW.
reedbeta.com - developer blog, OpenGL demos, and other projects





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users