• Advertisement

kokoui

Member
  • Content count

    1
  • Joined

  • Last visited

Community Reputation

122 Neutral

About kokoui

  • Rank
    Newbie
  1. In the following opengl program I am trying to make octagone column with startpoint (0,0,0). Although my program compiles, I am unable to find why my column is not visible. I would be greateful if somebody could have a look at it and let me know! :-) #include <stdio.h> #include <math.h> /* in case math.h dose not define PI */ #ifndef PI #define PI 3.141593 #endif /* include openGL, glUtility and utility toolkit headers*/ #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> /* define values for the view flag */ #define FIXED 0 #define ROBOT 1 #define PLAN 2 /* declare the function prototypes */ void display(void); void reshape(int, int); void init_ground(void); void init_lights(void); void special(int, int, int); void menu(int); /* declare the global variables holding display lists*/ GLuint ground; /* a flag for the viewing state */ GLuint look_flag=FIXED; int main(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); /******************************** CREATING THE WINDOW ********************************/ glutInitWindowSize(500, 500); glutCreateWindow("Sat Sales"); glClearColor (1.0, 1.0, 1.0, 0.0); glEnable(GL_DEPTH_TEST); glutReshapeFunc(reshape); glutDisplayFunc(display); glutSpecialFunc(special); init_ground(); init_lights(); glutCreateMenu(menu); glutAddMenuEntry("Fixed View", FIXED); // glutAddMenuEntry("Front View", FRONT); glutAddMenuEntry("Plan View", PLAN); glutAddMenuEntry("Exit", -1); glutAttachMenu(GLUT_MIDDLE_BUTTON); glutMainLoop(); return 0; } void reshape(int w, int h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /********************************************************* The plan view is an orthographic projection it must project the project squares as squares not rectangles. So when the width is greater than the height the `bottom' and `top' values are scaled. This adds white space at the top and bottom of the image but ensures the geometry is correct. The opposite applies when h > w. ***********************************************************/ if(look_flag==PLAN){ if(w <= h) glOrtho(-110, 110, -110*(GLfloat)h/(GLfloat)w, 110*(GLfloat)h/(GLfloat)w, 10, 200); else glOrtho(-110*(GLfloat)w/(GLfloat)h, 110*(GLfloat)w/(GLfloat)h, -110, 110, 10, 200); }else gluPerspective(120.0, ((GLfloat) w)/((GLfloat)h), 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); glutPostRedisplay(); return; } void display(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); if(look_flag==FIXED){ gluLookAt(-65.0, 15.0, 70.0, 70.0, 10.0, -70.0, 0.0, 1.0, 0.0); }else{ gluLookAt(0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); } init_lights(); glCallList(ground); glFlush(); glutSwapBuffers(); return; } void init_lights(){ GLfloat light0_position[]={70.0, 50.0, -90.0, 1.0}; GLfloat light1_position[]={-110.0, 60.0, 120.0, 1.0}; GLfloat light2_position[]={0.0, 40, 0.0, 1.0}; GLfloat amb_and_diff[]={0.8, 0.4, 0.5, 1.0}; GLfloat global_abm[]={0.2, 0.2, 0.2, 1.0}; GLfloat specular[] = {0.3, 0.3, 0.3, 1.0}; glEnable(GL_LIGHTING); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_abm); /********************************** Part 2: your code goes here **********************************/ return; } void init_ground(){ GLfloat amb_and_diffuse[]= {0.1, 0.7, 0.1, 1.0}; GLfloat emission[]= {0.0, 0.2, 0.0, 1.0}; ground = glGenLists(1); if(ground!=0){ glNewList(ground, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, amb_and_diffuse); glMaterialfv(GL_FRONT, GL_EMISSION, emission); glBegin(GL_QUADS); glNormal3f(0.0, 1.0, 0.0); glVertex3f(-100.0, 0.0, -100.0); glVertex3f(100.0, 0.0,-100.0); glVertex3f(100.0, 0.0, 100.0); glVertex3f(-100.0, 0.0, 100.0); glEnd(); glEndList(); }else fprintf(stderr, "Error: OpenGL could not obtain a list for the ground\n"); return; } /************************************** THIS FUNCTION UTILISES THE ARROW KEYS ***************************************/ void special(int key, int x, int y){ float X, Z; /********************************************** The robot moves forward and backward in its current attitude. This requires a bit of trig to find the X and Z increments and more complicated guards on the motion. ************************************************/ /* switch(key){ case GLUT_KEY_DOWN: X = sin(robot_angle*PI/180); Z = cos(robot_angle*PI/180); if((X>0.0 && robotX<=-95) || (X<0.0 && robotX>=95) || (Z>0.0 && robotZ<=-95) || (Z<0.0 && robotZ>=95)) break; robotX -= X; robotZ -= Z; break; case GLUT_KEY_UP: X = sin(robot_angle*PI/180); Z = cos(robot_angle*PI/180); if((X>0.0 && robotX>=95) || (X<0.0 && robotX<=-95) || (Z>0.0 && robotZ>=95) || (Z<0.0 && robotZ<=-95)) break; robotX += X; robotZ += Z; break; case GLUT_KEY_RIGHT: robot_angle-=5; break; case GLUT_KEY_LEFT: robot_angle+=5; break; default: fprintf(stdout, "No function attached to key.\n"); }*/ glutPostRedisplay(); return; } /********************************************* The menue is used to set the view it must therefor change the projection matrices when entering or leaving the PLAN state. This requires the use of `glutGet' to find the current window size. *********************************************/ void menu(int id){ float w, h; if(id==look_flag) return; if(id<0) exit(0); if(id==PLAN){ w = (float) glutGet(GLUT_WINDOW_WIDTH ); h = (float) glutGet(GLUT_WINDOW_HEIGHT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) glOrtho(-110, 110, -110*(GLfloat)h/(GLfloat)w, 110*(GLfloat)h/(GLfloat)w, 10, 200); else glOrtho(-110*(GLfloat)w/(GLfloat)h, 110*(GLfloat)w/(GLfloat)h, -110, 110, 10, 200); glMatrixMode(GL_MODELVIEW); }else if(look_flag==PLAN){ w = (float) glutGet(GLUT_WINDOW_WIDTH ); h = (float) glutGet(GLUT_WINDOW_HEIGHT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(120.0, ((GLfloat) w)/((GLfloat)h), 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); } look_flag=id; glutPostRedisplay(); return; }
  • Advertisement