load identity after gluLookAt?

Hi guys, i’m trying to using the function gluLookAt and apply some transformations to my object. The question is: I need to call glLoadIdentity between them or moving the camera doesn’t affect the coordinate sistem of the trasformations?

I am not sure to understand what you have said here…

If you call glLoadIdentity on modelview matrix it will fill this one with the identity matrix.
gluLookAt affects the modelview matrix, so the scene objects.

I mean, use the glulookat, influences the trasformations of the object (and so if I want to apply trasformations on the object I need to clear the matrix stack loading the identity matrix)?

Sorry for the delay,

It depends where you place the gluLookAt function. Commonly, it is placed after the modelview matrix reset. Remember that each time you set a transformation, this one affects the matrix. So when you compute transformations on vertices, these ones are computed in the opposite order compared with the order of the transformation that you set.
For example, if you a do glTranslate then glRotate and finally you draw an object, the rotation will be first applied to the object and then the translation.

To answer your question, if you load identity in the modelview matrix after calling gluLookAt, you will simply cancel the gluLookAt operation because you erase the matrix. All objects drawn after a gluLookAt call will be transformed to conform to the new camera view.
If you have done many transformations on the scene and you want to reset, you can call glPushMatrix to save the current matrix in a stack and then reset it. With glPopMatrix you can then pop the saved matrix if you want.

To make it clear:

this piece of code may can help you:

glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0, 5, 5, 0, 0, 0, 0, 1, 0); //for example

glPushMatrix(); you save the current matrix

    glTranslatef(-1, 0, 0); // then you do a transformation

// draw here something

glPopMatrix(); // get back the saved matrix

    glTranslatef(1, 0, 0); // an other transformation


// draw another thing

glutSwapBuffers();

I’ve tried to draw Line Loop, and then Points, on hitting ESC but nothing happens. Here is the code


from OpenGL.GL import *  
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys 
rot = 0 
def InitGL(Width, Height):
    glClearColor(0.0, 0.0, 0.0, 0.0) 
    glClearDepth(1.0)     glDepthFunc(GL_LESS) 
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)= 0
    glEnable(GL_LIGHT0)
    glEnable(GL_COLOR_MATERIAL)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(25.0, float(Width)/float(Height), 0.1, 1000.0)
    glMatrixMode(GL_MODELVIEW)
    
def ReSizeGLScene(Width, Height): 
    if Height == 0: Height = 1
    glViewport(0, 0, Width, Height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(25.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)

def DrawGLScene():
    global rot
    rot = (rot + 1) % 360 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 
    glLoadIdentity() 
    glTranslatef(0.2, 0.3, -10.0) 
    glRotatef(rot, 1.0, 0.0, 0.0) 
    glRotatef(rot, 0.0, 1.0, 0.0) 
    glRotatef(rot, 0.0, 0.0, 1.0) 
    glColor4f(0.0, 0.7, 0.1, 0.5) 
    glPointSize(2)
    glBegin(GL_LINE_LOOP)
    glVertex3f(0.4,0.5,0.1)
    glVertex3f(-0.5,-0.6,0.3)
    glVertex3f(0.3,-0.4,0.3)
    glVertex3f(0.0,0.0,0.0)
    glEnd()
    glutSwapBuffers()


def KeyPressed(*args): 
    if args[0] == "\033": 
        glClearColor(0.4, 0.4, 0.4, 0.4) 
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(0.1, 0.2, 0.3, 0.2, 0.1, 0.5, 0.5, 0.3, 0.3)
        glPushMatrix()
        glTranslatef(-0.7, 0.0, 0.0)
        glBegin(GL_LINE_LOOP)
        glVertex3f(0.7,0.2,0.6)
        glVertex3f(-0.1,-0.3,0.5)
        glVertex3f(0.2,-0.4,0.7)
        glVertex3f(0.1,0.4,0.8)
        glEnd()
        glPopMatrix()
        glTranslatef(0.5, 0.0, 0.0)
        glEnable(GL_POINT_SMOOTH)
        glBegin(GL_POINTS)
        glVertex3f(0.5, 0.7, -0.6)
        glVertex3f(0.2, 0.4, 0.2)
        glEnd()
        glutSwapBuffers()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
    glutInitWindowSize(800, 600)
    glutInitWindowPosition(0, 0)
    glutCreateWindow("Hello Worlds")
    glutDisplayFunc(DrawGLScene)
    glutIdleFunc(DrawGLScene)
    glutReshapeFunc(ReSizeGLScene)
    glutKeyboardFunc(KeyPressed)
    InitGL(800, 600)
    glutMainLoop()

main()

Ages, I missed something, but what? Can you help me to fing out?

ehm… you are drawing the scene without camera, you setup the camera only when you press esc. All the other frame are drawn without camera setup.

OpenGL don’t have any concept of camera, it only work with matrix transformation, to draw the scene correctly you have to setup these matrix every frame (gluLookAt make the work for you). The code that dletozeum suggest you is correct, you must do something similar in your DrawGLScene.

I shall add gluLookAt() in DrawGLScene() and/or in InitGL()
?

I made code more simple, but it gives me just black screen


from OpenGL.GL import *   
from OpenGL.GLU import *
from OpenGL.GLUT import *

def InitGL(Width, Height):
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0) 
    glDepthFunc(GL_LESS) 
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_COLOR_MATERIAL)
    
    
def ReSizeGLScene(Width, Height): 
    if Height == 0: Height = 1
    glViewport(0, 0, Width, Height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    
def DrawGLScene():
    glClear(GL_COLOR_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(-0.5, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    glPushMatrix()
    glTranslatef(-1.0, 0.0, 0.0)
    glEnable(GL_POINT_SMOOTH)
    glPointSize(3)
    glBegin(GL_POINTS)
    glVertex3f(0.0, 0.0, 0.0)
    glEnd()
    glPopMatrix() 
    glTranslatef(1.0, 0.0, 0.0)
    glBegin(GL_POINTS)
    glVertex3f(0.4, 0.5, 0.6)
    glEnd()
    glutSwapBuffers()

def KeyPressed(*args):
    if args[0] == "\033": sys.exit()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
    glutInitWindowSize(800, 600)
    glutInitWindowPosition(0, 0)
    glutCreateWindow("Will it ever works properly at last?")
    glutDisplayFunc(DrawGLScene)
    glutIdleFunc(DrawGLScene)
    glutReshapeFunc(ReSizeGLScene)
    glutKeyboardFunc(KeyPressed)
    InitGL(800, 600)
    glutMainLoop()

main()

Yes, with the gluLookAt in the DrawGLScene should work.

The problem here are that you have enabled the lighting and color material, but you didn’t specified them.
Don’t enable lighting and set the glColor3f to something very bright and you should two points.
Also be careful, setting the projection matrix to identity you are setting the far and near plane to 1 and -1 respectively, and this will cull the point away. Restore the gluPerspective as the first code you post.

I’ve found out that I made a stupid mistake in setting camera position
gluLookAt(-0.5, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
Of course I haven’t seen points, because camera was set up wrong.
It couldn’t be at position -0.5 on Axis X, 1.0 on Axis Y, and 1.0 at Axis Z simultaneously.
Now I changed code to
gluLookAt(-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
and it works. but steel I see only one point. may be I should make some delay because it drawing too fast? But how?

Yes, it draw only first point, but can’t draw line_loop then


from OpenGL.GL import *   
from OpenGL.GLU import *
from OpenGL.GLUT import *

def InitGL(Width, Height):
    glClearColor(0.0, 0.0, 0.0, 0.0)
    #gluLookAt(-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    #glClearDepth(1.0) 
    #glDepthFunc(GL_LESS) 
    #glEnable(GL_DEPTH_TEST)
    #glEnable(GL_LIGHTING)
    #glEnable(GL_LIGHT0)
    #glEnable(GL_COLOR_MATERIAL)
    
    
def ReSizeGLScene(Width, Height): 
    if Height == 0: Height = 1
    glViewport(0, 0, Width, Height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    
def DrawGLScene():
    glClear(GL_COLOR_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    glPushMatrix()
    glTranslatef(-1.0, 0.0, 0.0)
    glEnable(GL_POINT_SMOOTH)
    glPointSize(3)
    glBegin(GL_POINTS)
    glVertex3f(0.0, 0.0, 0.0)
    glEnd()
    glPopMatrix() 
    glTranslatef(1.0, 0.0, 0.0)
    glBegin(GL_LINE_LOOP)
    glVertex3f(0.4, 0.5, 0.6)
    glVertex3f(0.2, 0.4, 0.1)
    glEnd()
    glutSwapBuffers()

def KeyPressed(*args):
    if args[0] == "\033": sys.exit()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
    glutInitWindowSize(800, 600)
    glutInitWindowPosition(0, 0)
    glutCreateWindow("Will it ever works properly at last?")
    glutDisplayFunc(DrawGLScene)
    glutIdleFunc(DrawGLScene)
    glutReshapeFunc(ReSizeGLScene)
    glutKeyboardFunc(KeyPressed)
    InitGL(800, 600)
    glutMainLoop()

main()