Jump to content
  • Advertisement
Sign in to follow this  
dandjimon

screen limits

This topic is 3535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 <GL/glut.h> #include <stdlib.h> #include <math.h> 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; }

Share this post


Link to post
Share on other sites
Advertisement
Haven't looked at your code and don't know how to confine the mouse pointer to the window in GLUT or if it's even possible (GLUT is not designed to be able to do everything, just to be a simple way to get something up and running). There is a list of the GLUT commands here: http://www.opengl.org/resources/libraries/glut/spec3/spec3.html. Maybe one of these will do it.

GLUT can draw text on the screen, but it's very limited which fonts it can use. See here for how to do it: http://www.opengl.org/resources/libraries/glut/spec3/node75.html. If you just want to get some text on the screen, it's fine, otherwise you should probably find a more powerful library to display text.

For example, to display an oldschool char string you could do something like this:

char* text = whatever;

glRasterPos3f(x,y,z);
for (char* c=text; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!