rocksoccer Posted March 5, 2007 Share Posted March 5, 2007 I don't know whether this is the correct section to put this. if not, sorry.I m new to OpenGL. sooooo, many stupid questions.Below is my program to draw a rotating cube, I very confused why I cannot move gluLookAt() from the display function to the initialize function! Also, it seems the program is affected significantly by the size of the displaying window. Is that caused by the place of gluLookAt? If it is in the display function, does that mean every time the cube is refreshed, the camera is repositioned?Thanks in advance! Help please! //#include <GL/glu.h>#include <GL/glut.h>//#include "glut.h"#include <math.h>#define NOMATERIAL ((Material*)0)#define DEGTORAD 57.29578#define root2 1.414213562//typedef enum/*GLfloat theta=0.0;GLfloat beta=0.0;GLfloat alpha=0.0;*///the light sourceGLfloat xl=-3.0;GLfloat yl=3.0;GLfloat zl=3.0;GLfloat position[3]={5.0,5.0,5.0};GLfloat f=0.0;GLfloat g=0.0;GLfloat rotqube=0.0;GLfloat m[16]={0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0};GLfloat vertices[][3]={{-1.0,2.0,1.0},{-1.0,4.0,1.0},{1.0,4.0,1.0},{1.0,2.0,1.0},{-1.0,2.0,-1.0},{-1.0,4.0,-1.0},{1.0,4.0,-1.0},{1.0,2.0,-1.0},{10.0,0.0,10.0},{10.0,0.0,-10.0},{-10.0,0.0,-10.0},{-10.0,0.0,10.0}};GLfloat colours[][3]={{1.0,0.0,0.0},{0.0,1.0,1.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{0.0,0.0,0.0},{0.7,0.7,0.5}};void polygon(GLfloat colours[][3], int col, GLfloat vertices[][3], int a, int b, int c, int d){ glColor3fv(colours[col]); glBegin(GL_POLYGON); glNormal3fv(vertices[a]); glVertex3fv(vertices[a]); glNormal3fv(vertices[b]); glVertex3fv(vertices[b]); glNormal3fv(vertices[c]); glVertex3fv(vertices[c]); glNormal3fv(vertices[d]); glVertex3fv(vertices[d]); glEnd(); glFlush();}void cube (void) { //glRotatef(rotqube,1.0,1.0,1.0); //glRotatef(rotqube,1.0,0.0,0.0); //glBegin(GL_QUADS); polygon(colours,0,vertices,0,3,2,1);//front red polygon(colours,3,vertices,2,3,7,6);//right green polygon(colours,5,vertices,3,0,4,7);//bottom polygon(colours,4,vertices,1,2,6,5);//top blue polygon(colours,1,vertices,4,5,6,7);//back polygon(colours,2,vertices,5,4,0,1);//left //glEnd(); //glFlush(); //rotqube +=0.1; }void display (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.0, 3.0, 10.0, 0.0, 3.0, 0.0, 0.0, 1.0, 0.0); polygon(colours,7,vertices,8,9,10,11);//ground ///rotate the cube glRotatef(rotqube,1.0,4.0,1.0); //glRotatef(rotqube,3.0,0.0,0.0); //glRotatef(rotqube,-1.0,4.0,1.0); cube(); /////////////Drawing shadow////////////////////////// /*glPushMatrix(); glPushAttrib(GL_CURRENT_BIT); glTranslatef(xl,yl,zl); glMultMatrixf(m); glTranslatef(-xl,-yl,-zl);*/ //cube_shadow(); /*glPopMatrix(); glPopAttrib();*/ //////////////////////////////////////////////////// glutSwapBuffers();}void reshape (int w, int h) { glViewport (0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); glMatrixMode (GL_MODELVIEW);}void myidle(void){ rotqube +=0.1;//increment the angle glutPostRedisplay(); /*Mark the normal plane of current window as needing to be redisplayed. The next iteration through glutMainLoop, the window's display callback will be called to redisplay the window's normal plane. Multiple calls to glutPostRedisplay before the next display callback opportunity generates only a single redisplay callback. glutPostRedisplay may be called within a window's display or overlay display callback to re-mark that window for redisplay.*/}static void initialize(void){ /* point light */ GLfloat light_position[] = { 5.0, 5.0, 5.0, 1.0 }; //GLfloat light_position[] = { 0.0, 2.0, 1.0, 0.0 }; GLfloat light_diffuse[]={0.0, 0.4, 0.0, 1.0}; GLfloat light_ambient[]={1.0, 1.0, 1.0, 1.0}; GLfloat white_light[]={1.0,0.0,0.0,1.0}; GLfloat mat_ambient[] = {0.56, 0.41, 0.37, 0.0}; GLfloat mat_diffuse[] = {0.43, 0.47, 0.54, 0.0}; GLfloat mat_specular[]= {0.33, 0.33, 0.52, 0.0}; GLfloat mat_emission[]= {0.0, 0.0, 0.31, 0.0}; GLfloat shininess=50.0;// the range:0~128 glMatrixMode(GL_MODELVIEW); /*glLoadIdentity(); gluLookAt (0.0, 3.0, 10.0, 0.0, 3.0, 0.0, 0.0, 1.0, 0.0);*/ /* in the eye (viewing) coordinates*/ glLightfv(GL_LIGHT0, GL_POSITION, light_position); /* GL_LIGHT0 is a point light */ glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);//light_diffuse); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);/* a local viewport tends to yield more realistic results. but more calculation*/ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_FALSE); /* Set the material*/ glShadeModel(GL_SMOOTH); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); /*diffuse reflectance plays the most important role in the determining what you perceive the color of an object to be*/ /*both ambient and diffuse are not affected by the position of the viewport*/ glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glMaterialf(GL_FRONT, GL_SHININESS, shininess); //glEnable(GL_CULL_FACE);//Backface culling glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST);/*enable the z or depth buffer. then there is no need for enabling GL_CULL_FACE, this is a more general way to hide the back face*/ //glLoadIdentity(); glEnable(GL_COLOR_MATERIAL); //glColorMaterial(GL_FRONT, GL_LIGHT0); glColorMaterial(GL_FRONT, GL_AMBIENT); glColorMaterial(GL_FRONT, GL_AMBIENT); /*glColorMaterial specifies two independent values: the first determines which face to update, and the second determines which material property are updated*/ /*glColorMaterial(GL_FRONT, GL_SPECULAR); glColorMaterial(GL_FRONT, GL_SPECULAR); glColorMaterial(GL_FRONT, GL_SPECULAR); glColorMaterial(GL_FRONT, GL_SPECULAR);*/ glClearColor (1.0,1.0,1.0,0.0);}int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);//double buffer /*RGBA mode is used, the lighting capability is different for the RGBA and Color-Index modes. RGBA is always the preferred mode*/ //still dont understand why we need GLUT_DEPTH glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("rotating cube"); initialize(); glutDisplayFunc (display); glutReshapeFunc (reshape); glutIdleFunc(myidle); glutMainLoop (); return 0;} Link to comment Share on other sites More sharing options...
rocksoccer Posted March 6, 2007 Author Share Posted March 6, 2007 nobody likes to help?? Link to comment Share on other sites More sharing options...
Zxian Posted March 13, 2007 Share Posted March 13, 2007 With what little experience I have with OpenGL, I can say that for a typical windowed application, the performance is VERY heavily dependent on the window size. I don't think that GLUT is the most efficient library out there.I'm not quite sure why the glLookAt() function needs to be in the display function, but it's there in all the OpenGL programs that I've got. Is there any specific reason why you need it in the init function? Link to comment Share on other sites More sharing options...
rocksoccer Posted March 15, 2007 Author Share Posted March 15, 2007 With what little experience I have with OpenGL, I can say that for a typical windowed application, the performance is VERY heavily dependent on the window size. I don't think that GLUT is the most efficient library out there.I'm not quite sure why the glLookAt() function needs to be in the display function, but it's there in all the OpenGL programs that I've got. Is there any specific reason why you need it in the init function?I think maybe gluLookAt() could be one of the reasons that affects the speed. Because if it is in the display function, then the camera will be set every time the display is refreshed. If it is in the init function, then just set the camera by once. Right?? Link to comment Share on other sites More sharing options...
Zxian Posted March 21, 2007 Share Posted March 21, 2007 The setting of the camera position isn't very computationally intensive, so I'd be surprised if that's why everything is slowing down. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now