开发者

OpenGL ES 3d rotation causes unintentional translation?

开发者 https://www.devze.com 2023-01-20 14:00 出处:网络
I\'m working on an iphone project where I need both a horizontal and a vertical layout. I\'ve got it working in 2D perfectly well, using the following setup to my projection matrix:

I'm working on an iphone project where I need both a horizontal and a vertical layout. I've got it working in 2D perfectly well, using the following setup to my projection matrix:

 if(isHoriz()){
  glRotatef(90.0f,0.0f,0.0f,1.0f);
  SOrtho(0,  480, 0, 320, -1, 1); 
  }
  else
    SOrtho(0,  320, 0, 480, -1, 1); 

The problem lies when I move to 3D

  glViewport(0,0,320,480);   
  glScissor(0,0,320,480); 
  gluPerspective(28.8f,320/480,0.1f,1000.0f);
  if(isHoriz())
    glRotatef(90.0f,0.0f,0.0f,1.0f);

This seems to work, but the view is offset vertically wrong. What I want t开发者_JAVA百科o happen is that the top half of the vertical display becomes the center of the horizontal display. Instead, it seems like the middle of the vertical display becomes the center of the horizontal display.

Because this is an iphone, the window height (480) and width (320) remain constant regardless of rotation. I've tried building the same code into a windows program, and the code that gets the effect I want for the horizontal rotation is simple. I just change the window shape, so that I have a landscape window, and then remove the call to

  if(isHoriz())
    glRotatef(90.0f,0.0f,0.0f,1.0f);

(I also do something similar to the 2D code).

Am I doing something wrong? Why don't the windows version and the iphone version look the same?

Edit: To clarify, I've tried adding glTranslatef() calls above and below the glRotatef call, but this seems to have an effect on the angle of my camera. The camera code is applied to my modelView matrix immediately after the above code. It looks like this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(mRotate.mZ,0,0,1);
glRotatef(mRotate.mY,0,1,0);
glRotatef(mRotate.mX,1,0,0);
glTranslatef(-mPos.mX,-mPos.mY,-mPos.mZ);

The initial values of the camera are mRotate.mX = -80, mPos.Z = -8.


I'm still not certain why this happened, but I moved my rotation from the projection to the modelview and everything works fine. I think it had to do with the fact that the Windows window had a varying shape (either 320x480 or 480x320) that did not match the iPhone shape (always 320x480).

0

精彩评论

暂无评论...
验证码 换一张
取 消