Original Post
im having trouble limiting the mouse movement to the screen limits of the main window. also when i run the program it takes up to 60% of the CPU why is that? oh, and one more thing i would like to put some tekst on the main window...something like directions for movement. can anyone give some sample code...would aprichiate that a lot! thanks!! here's the code. #include #include #include using namespace std; // Kut rotacije float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0; float cRadius = 20.0f; float lastx, lasty; // Broj objekata uključen u konstrukciju sfere. static int slices = 16; static int stacks = 16; void sfera(void) { glPushMatrix(); glTranslated(0.0, 0.0, 0.0); glutSolidSphere(1, slices, stacks); glPopMatrix(); } void teren(void) { glPushMatrix(); // Ravnina po kojoj se giba sfera glColor3d(0,1,0); glTranslated(0.0,-1.0,-60.0); glScalef (50.0, 0.1, 100.0); glutSolidCube(1.0); glPopMatrix(); for(int i = -3; i < 4; i++) // Kvadri raspoređeni po matrici koji predstavljaju for(int j=-9; j < -1; j++) { // zamisljene objekte na ravnini po kojoj se giba sfera (prepreke) glPushMatrix(); glColor3d(0,0,0); glTranslated(i*6.0, 2.1, j * 10.0); glScalef (2.0, 6.0, 2.0); glutSolidCube(1.0); glPopMatrix(); } } void enable (void) { glEnable (GL_DEPTH_TEST); // GLUT procedure za svijetlo, sjenu, boje i prozirnost. glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); glEnable (GL_COLOR_MATERIAL); glShadeModel (GL_SMOOTH); } void display (void) { glClearColor (1.0,1.0,0.0,1.0); // Postavljanje bijele pozadine glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Pražnjenje buffera enable(); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -cRadius); glRotatef(xrot,1.0,0.0,0.0); glColor3f(1.0f, 0.0f, 0.0f); sfera(); glRotatef(yrot,0.0,1.0,0.0); // Rotacija kamere po y-osi (gore i dolje) glTranslated(-xpos ,0.0f, -zpos); // Translacija zaslona na poziciju naše kamere glColor3f(1.0f, 1.0f, 1.0f); teren(); // Pozivanje funkcije koja crta teren. glutSwapBuffers(); angle++; // Povećanje kuta } void reshape (int w, int h) { glViewport (0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode (GL_PROJECTION); //set the matrix to projection glLoadIdentity (); gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0); // Namještanje perspektive (kut gledanja, širina, visina, dubina) glMatrixMode (GL_MODELVIEW); // Postavljanje matrice modela } void tipkovnica(unsigned char tipka, int x, int y) // Definicija tipki na tipkovnici. { switch (tipka) { case 27 : case 'x': exit(0); break; case '+': if (slices<300 && stacks<300) { slices++; stacks++; } break; case '-': if (slices>3 && stacks>3) { slices--; stacks--; } break; case 'w':{ float xrotrad, yrotrad; yrotrad = (yrot / 180 * 3.141592654f); xrotrad = (xrot / 180 * 3.141592654f); xpos += float(sin(yrotrad)); zpos -= float(cos(yrotrad)); ypos -= float(sin(xrotrad));} break; case 's':{ float xrotrad, yrotrad; yrotrad = (yrot / 180 * 3.141592654f); xrotrad = (xrot / 180 * 3.141592654f); xpos -= float(sin(yrotrad)); zpos += float(cos(yrotrad)); ypos += float(sin(xrotrad));} break; case 'a':{ float yrotrad; yrotrad = (yrot / 180 * 3.141592654f); xpos -= float(cos(yrotrad)) * 0.5; zpos -= float(sin(yrotrad)) * 0.5;} break; case 'd':{ float yrotrad; yrotrad = (yrot / 180 * 3.141592654f); xpos += float(cos(yrotrad)) * 0.5; zpos += float(sin(yrotrad)) * 0.5;} break; case 'r': if(cRadius > 5) {cRadius -= 1;} break; case 'f': if(cRadius < 50) {cRadius += 1;} break; } glutPostRedisplay(); } void KretnjaMisa(int x, int y) { float diffx=x-lastx; // Provjera razlike između trenutne i prošle pozicije na x osi. float diffy=y-lasty; // Provjera razlike između trenutne i prošle pozicije na y osi. lastx=x; // Postavljanje prosle koordinate x na trenutnu koordinatu. lasty=y; // Postavljanje prosle koordinate y na trenutnu koordinatu. xrot += (float) diffy*0.4; // Postavljanje faktora rotacije po x osi na novu vrijednost kojoj dodajemo razliku na y osi yrot += (float) diffx*0.4; // Postavljanje faktora rotacije po y osi na novu vrijednost kojoj dodajemo razliku na x osi } int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize (1024, 768); glutInitWindowPosition (100, 100); glutCreateWindow ("GISFE_KATRELI.ver.0.61"); glutDisplayFunc (display); glutIdleFunc (display); glutReshapeFunc (reshape); glutPassiveMotionFunc(KretnjaMisa); //provjera kretnje miša glutKeyboardFunc (tipkovnica); glutMainLoop (); return 0; }