University OpenGL projct- PROBLEM!!

Started by
5 comments, last by kSquared 18 years, 8 months ago
Dear all! I have one problem regarding my university project. I have to program in opengl a table with max. 4 drawers. So far so good and this is my code (question on the end): #include <math.h> #include <stdlib.h> #include <stdio.h> #include <GL/glut.h> #include <GL/gl.h> /*------------------------------------------------------------------------*/ float fi_x = 0.0, fi_y=0.0; int old_x = 0, old_y=0; static float xrx = 0.0, xry = -1.0, xrz = 0.2; float t; int stpredalov = 0; /*------------------------------------------------------------------------*/ void pomoc(void) { printf("\n"); printf("Tipke:\n\n"); printf("\n"); printf("v - povecaj stevilo predalov (max. 4) \n"); printf("m - zmanjsaj stevilo predalov (min. 0) \n"); printf("\n"); printf("a - zapelji predale ven \n"); printf("s - zapelji predale notri \n"); printf("\n"); printf("x - zavrti mizo okoli x osi \n"); printf("y - zavrti mizo okoli y osi \n"); printf("\n"); printf("Miska:\n\n"); printf("leva tipka - vrtenje mize po x, y in z-osi \n"); printf("srednja tipka - prepoznavanje objektov \n"); printf("\n"); } /*------------------------------------------------------------------------*/ void init(void) { GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = {1.0, 1.0, -0.5, 0.0}; GLfloat model_ambient[] = { 0.7, 0.7, 0.7, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0,1.0, -1.0,1.0, -10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /*----------------------------------------------------------------------*/ void nosilec (void) { glPushMatrix(); glBegin (GL_QUADS); //XY1 glNormal3f (0.0, 0.0, -1.0); glVertex3f (0.0, 0.0, 0.0); glVertex3f (1.0, 0.0, 0.0); glVertex3f (1.0, 1.0, 0.0); glVertex3f (0.0, 1.0, 0.0); glEnd(); glBegin (GL_QUADS); //XY2 glNormal3f (0.0, 0.0, 1.0); glVertex3f (0.0, 1.0, 1.0); glVertex3f (1.0, 1.0, 1.0); glVertex3f (1.0, 0.0, 1.0); glVertex3f (0.0, 0.0, 1.0); glEnd(); glBegin (GL_QUADS); //XZ1 glNormal3f (0.0, -1.0, 0.0); glVertex3f (0.0, 0.0, 0.0); glVertex3f (0.0, 0.0, 1.0); glVertex3f (1.0, 0.0, 1.0); glVertex3f (1.0, 0.0, 0.0); glEnd(); glBegin (GL_QUADS); //XZ2 glNormal3f (0.0, 1.0, 0.0); glVertex3f (0.0, 1.0, 0.0); glVertex3f (1.0, 1.0, 0.0); glVertex3f (1.0, 1.0, 1.0); glVertex3f (0.0, 1.0, 1.0); glEnd(); glBegin (GL_QUADS); //ZY1 glNormal3f (-1.0, 0.0, 0.0); glVertex3f (0.0, 0.0, 0.0); glVertex3f (0.0, 1.0, 0.0); glVertex3f (0.0, 1.0, 1.0); glVertex3f (0.0, 0.0, 1.0); glEnd(); glBegin (GL_QUADS); //ZY2 glNormal3f (1.0, 0.0, 0.0); glVertex3f (1.0, 0.0, 0.0); glVertex3f (1.0, 1.0, 0.0); glVertex3f (1.0, 1.0, 1.0); glVertex3f (1.0, 0.0, 1.0); glEnd(); glPopMatrix(); } /*----------------------------------------------------------------------*/ void risi_mizo() { glInitNames(); glPushName(-1); glPushMatrix(); glColor3f (0.8f, 0.8f, 0.2f); glScalef (0.5, 0.7, 0.018); glTranslatef (0.0, 0.0, 0.0); nosilec(); glLoadName(1); glPopMatrix(); glPushMatrix(); glColor3f (0.0f, 1.0f, 1.0f); glScalef (0.5, 0.018, -1.230); glTranslatef (0.0, 38.6, -0.018); nosilec(); glLoadName(2); glPopMatrix(); glPushMatrix(); glColor3f (0.8f, 1.0f, 0.0f); glScalef (0.5, 0.7, -0.018); glTranslatef (0.0, 0.0, 66.0); nosilec(); glLoadName(3); glPopMatrix(); glPushMatrix(); glColor3f (1.0f, 1.0f, 0.0f); glScalef (0.018, 0.5, -1.2); glTranslatef (0.0, 0.4, 0.0); nosilec(); glLoadName(4); glPopMatrix(); glPushMatrix(); glColor3f (0.5f, 0.75f, 1.0f); glScalef (0.482, 0.5, -0.018); glTranslatef (0.018, 0.4, 20.0); nosilec(); glLoadName(5); glPopMatrix(); glPushMatrix(); glColor3f (0.0f, 0.75f, 1.0f); glScalef (0.5, 0.018, -0.38); glTranslatef (0.0, 10.0, 0.0); nosilec(); glLoadName(6); glPopMatrix(); glPopName(); } /*-----------------------------------------------------------------------*/ void risi_predal() { glInitNames(); glPushName(-1); glPushMatrix(); glColor3f (0.5f, 1.0f, 1.0f); glScalef (0.5, 0.12, 0.018); glTranslatef (0.0, 1.65, -1.0); nosilec(); glLoadName(7); glPopMatrix(); glPushMatrix(); glColor3f (0.0f, 0.5f, 0.5f); glScalef (0.5, 0.12, 0.018); glTranslatef (0.0, 1.65, -20.0); nosilec(); glLoadName(8); glPopMatrix(); glPushMatrix(); glColor3f (1.0f, 1.0f, 0.0f); glScalef (0.018, 0.12, -0.35); glTranslatef (0.0, 1.65, 0.018); nosilec(); glLoadName(9); glPopMatrix(); glPushMatrix(); glColor3f (0.0f, 1.0f, 0.5f); glScalef (0.018, 0.12, -0.35); glTranslatef (27.0, 1.65, 0.018); nosilec(); glLoadName(10); glPopMatrix(); glPushMatrix(); glColor3f (1.0f, 0.25f, 1.0f); glScalef (0.5, 0.0012, -0.35); glTranslatef (0.0, 165.0, 0.0); nosilec(); glLoadName(11); glPopMatrix(); glPushMatrix(); glColor3f (1.0f, 0.0f, 1.0f); glScalef (0.030, 0.02, -0.12); glTranslatef (16.5, 12.0, 1.0); nosilec(); glLoadName(10); glPopMatrix(); glPopName(); } /*------------------------------------------------------------------*/ void odpiranje_predala(void) { glPushMatrix(); glTranslatef(t, 0.0, 0.0); risi_predal(); glPopMatrix(); } /* ----------------------------------------------------------------- */ void risi (void) { int j; glClearColor (0.0f, 0.0f, 0.0f, 1.0f); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //pobarva ozadje glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glScalef(0.8, 0.8, 0.8); glRotatef (fi_x, 1.0, 0.0, 0.0); /*rotacija okoli x */ glRotatef (fi_y, 0.0, 1.0, 0.0); /* rotacija okoli y */ glTranslatef (0.0, -0.2, 0.0); risi_mizo(); for (j = 0; j < stpredalov; j++) { glPushMatrix(); glTranslatef(0.0, j*0.125, 0.0); odpiranje_predala(); glPopMatrix(); } glFlush (); glutSwapBuffers(); } /* ------------------------------------------------------------------ */ void premikanje_miske(int x, int y) { if (old_x < x) fi_x += 3.0; if (old_x > x) fi_x -= 3.0; if (old_y > y) fi_y += 3.0; if (old_y < y) fi_y -= 3.0; glutPostRedisplay(); old_x = x; old_y = y; } /* --------------------------------------------------------------------- */ void obdelaj_zadetke(GLint nr_hits, GLuint buffer[]) { unsigned int i, j; GLuint nr_names, *ptr; printf ("==== obdelaj zadetke: ======\n"); printf ("stevilo_zadetkov = %d\n", nr_hits); ptr = (GLuint *) buffer; for (i = 0; i < nr_hits; i++) { nr_names = *ptr; printf (" stevilo imen v zadetku = %d\n", nr_names); ptr++; printf (" z1 je %g;", (float) *ptr/0x7fffffff); ptr++; printf (" z2 je %g\n", (float) *ptr/0x7fffffff); ptr++; printf (" imena so: "); for (j = 0; j < nr_names; j++) { printf ("%d ", (int) *ptr); ptr++; } printf ("\n"); } } /* --------------------------------------------------------------------- */ void pritisk_na_gumb_miske(int button_index, int state, int x, int y) { GLuint selectBuf[512]; GLint nr_hits; GLint viewport[4]; switch(button_index) { case GLUT_MIDDLE_BUTTON: if (state) { glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(512, selectBuf); (void) glRenderMode(GL_SELECT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); risi(); glFlush(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); nr_hits = glRenderMode(GL_RENDER); obdelaj_zadetke(nr_hits, selectBuf); glutPostRedisplay(); } break; } } /* -------------------------------------------------------------------- */ void keyboard(unsigned char key, int x, int y) { if (key == 'a') { printf("Pritisnil si tipko \"a\"!\n"); t += 0.1; glutPostRedisplay(); } if (t > 0.5) { t = 0.5; } if (key == 's') { printf("Pritisnil si tipko \"s\"!\n"); t -= 0.1; glutPostRedisplay(); } if (t < 0.0) { t = 0.0; } switch (key) { case 'x': case 'X': fi_x += 10.0; glutPostRedisplay(); break; case 'y': case 'Y': fi_y += 10.0; glutPostRedisplay(); break; case 'v': case 'V': if (stpredalov < 4) { stpredalov++; } printf("Stevilo predalov: %i\n", stpredalov); glutPostRedisplay(); break; case 'm': case 'M': if (stpredalov > 0) { stpredalov--; } printf("Stevilo predalov: %i\n", stpredalov); glutPostRedisplay(); break; case 27: exit (0); break; default: break; } } /*--------------------------------------------------------------------------*/ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB); glutInitWindowSize (600, 600); glutInitWindowPosition (1, 1); glutCreateWindow ("Miza"); printf("Miza\n"); printf("-------\n"); pomoc(); init(); glutDisplayFunc(risi); glutMotionFunc (premikanje_miske); glutMouseFunc(pritisk_na_gumb_miske); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } Now click letter v to increase the drawers! The problem is that I have to click on the drawer with the mouse and drawer must go out of the table (and possibly back in). Is there enyone who could help me?
Advertisement
Sounds like homework.
Yeah :-) but I'm stuck with it!
Perhaps you should read this. I assume phantom will be along shortly to tell you the same thing.
Ups...I'm sorry for that. I wasn't aware of that. The thing is that all this is new to me since I'm not even in computer sience I just have no idea what else to do!

Sorry again!
Quote:Original post by grahek
Yeah :-) but I'm stuck with it!


We don't do homework here. In fact it is not even allowed. Read the rules before you post please.

If you don't know what to do consult your teacher or whomever issues the work. There should be some relevance to a course you are running so the material must be available. Discuss with other students (discuss; don't copy). If the assignment is really outrageous report to dean of faculty or whoever concerns it.

Greetz,

Illco
GameDev's policy is not to answer
Homework questions big and small;
Learn it for yourself next time
Or else you'll learn it not at all.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}

This topic is closed to new replies.

Advertisement