opengl glulookat() to glrotate + gltranslate

Viewed 114
import glfw 
import numpy as np 
from OpenGL.GL import * 
from OpenGL.GLU import * 
gCamAng = 0. 
gCamHeight = 1. 
def drawUnitCube(): 
  glBegin(GL_QUADS) 
  glVertex3f( 0.5, 0.5,-0.5) 
  glVertex3f(-0.5, 0.5,-0.5) 
  glVertex3f(-0.5, 0.5, 0.5) 
  glVertex3f( 0.5, 0.5, 0.5) 
   
  glVertex3f( 0.5,-0.5, 0.5) 
  glVertex3f(-0.5,-0.5, 0.5) 
  glVertex3f(-0.5,-0.5,-0.5) 
  glVertex3f( 0.5,-0.5,-0.5) 
   
  glVertex3f( 0.5, 0.5, 0.5) 
  glVertex3f(-0.5, 0.5, 0.5) 
  glVertex3f(-0.5,-0.5, 0.5) 
  glVertex3f( 0.5,-0.5, 0.5) 
   
  glVertex3f( 0.5,-0.5,-0.5) 
  glVertex3f(-0.5,-0.5,-0.5) 
  glVertex3f(-0.5, 0.5,-0.5) 
  glVertex3f( 0.5, 0.5,-0.5) 
  glVertex3f(-0.5, 0.5, 0.5) 
  glVertex3f(-0.5, 0.5,-0.5) 
  glVertex3f(-0.5,-0.5,-0.5) 
  glVertex3f(-0.5,-0.5, 0.5) 
   
  glVertex3f( 0.5, 0.5,-0.5) 
  glVertex3f( 0.5, 0.5, 0.5) 
  glVertex3f( 0.5,-0.5, 0.5) 
  glVertex3f( 0.5,-0.5,-0.5) 
  glEnd() 
   
def drawCubeArray(): 
  for i in range(5): 
      for j in range(5): 
          for k in range(5): 
              glPushMatrix() 
              glTranslatef(i,j,-k-1) 
              glScalef(.5,.5,.5) 
              drawUnitCube() 
              glPopMatrix() 
def drawFrame(): 
  glBegin(GL_LINES) 
  glColor3ub(255, 0, 0) 
  glVertex3fv(np.array([0.,0.,0.])) 
  glVertex3fv(np.array([1.,0.,0.])) 
  glColor3ub(0, 255, 0) 
  glVertex3fv(np.array([0.,0.,0.])) 
  glVertex3fv(np.array([0.,1.,0.])) 
  glColor3ub(0, 0, 255) 
  glVertex3fv(np.array([0.,0.,0])) 
  glVertex3fv(np.array([0.,0.,1.])) 
  glEnd() 
def render(): 
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 
  glEnable(GL_DEPTH_TEST) 
  glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ) 
  glLoadIdentity() 
  gluPerspective(45, 1, 1,10) 
   
  #gluLookAt(3,3,3, 0,0,0, 0,1,0) 
   
  glRotatef(36.264,0,-1,-1) 
  glRotatef(45,1,0,1) 
  glTranslatef(-3,-3,-3) 
  drawFrame() 
  glColor3ub(255, 255, 255) 
  drawCubeArray() 
   
def main(): 
  if not glfw.init(): 
      return 
  window=glfw.create_window(480,480,"1",None,None) 
  if not window: 
      glfw.terminate() 
      return 
  glfw.make_context_current(window) 
   
  while not glfw.window_should_close(window): 
      glfw.poll_events() 
      render() 
      glfw.swap_buffers(window) 
  glfw.terminate() 
  return 
if __name__=="__main__": 
  main() 

i need this result but It will only be similar and this result will not come out. enter image description here

this is my result. I made a few changes over there, but the desired result doesn't come out.

enter image description here

i have information that this. so i use rotate x,y 36.264 and rotate xz 45 degree. but i can't solve this problem

enter image description here

What should I fix?

1 Answers

Translate the model relative to the camera position by (-3, -3, -3), rotate it by -45° around the y-axis and finally by 35.264° (atan(1/sqrt(2))) around the x-axis:

glRotatef(35.264, 1, 0, 0) 
glRotatef(-45, 0, 1, 0) 
glTranslatef(-3, -3, -3) 

When the up vector is (0, 1, 0):

e = [3, 3, 3]
c = [0, 0, 0]
gluLookAt(3,3,3, 0,0,0, 0,1,0) 

the general approach is:

los = c[0]-e[0], c[1]-e[1], c[2]-e[2] 
rot_y = math.degrees(math.atan2(los[0], -los[2]))
len_xz = math.hypot(los[0], los[2])
rot_x = math.degrees(math.atan2(los[1], len_xz))

glRotatef(rot_x, -1, 0, 0) 
glRotatef(rot_y, 0, 1, 0) 
glTranslatef(los[0], los[1], los[2]) 
Related