Hi everyone, I’m new on this forum. I’ve started an opengl class and the teacher gave us an assignment.
Basically a square has to start rotating to the left by itself on left mouse button click and stop rotating (freeze) on right mouse button click. All I got was just some skeleton code on which to work.
I’ve finished the program but it’s not working properly. I would be grateful if someone could take a look and tell me what my mistakes are.
Ok then you did not get my hint. Should work with glRectf(0.3, 0.3, 0.7, 0.7);
To be more specific, coordinates you send to opengl with glVertex(), glRect, etc are not in pixels, but are transformed by modelview matrix (here, identity = no change) and projection matrix (here, glOrtho keep x between 0 and 1, same for y, and between -1 and 1 for z), then scaled to fit glViewport values.
If you feel more comfortable working with pixels, replace your glOrtho with glOrtho(0,w,0,h,-1,1);
All you got was some skeleton code? Not the complete solution? Sheesh, how do they expect you to learn?
You’ll find soon that glRotate ‘sets’ the position, doesn’t ‘animate’ the position. You’re going to need a variable or two. [edit] Oh, you do, sorry. You just need to finish integrating that.
Yes, you were right zbuffer, I had a misconception about how that worked.
thanks
It sort of works now except the square is centered in the bottom left corner and won’t stop rotating on right click. Anywaym I’ll read some docu to finish eveything up.
Ontopic:
I’m kind of confused about what you said. as far as I know glRotatef doesn;t animate it, but it does rotate it and actually the display function in the loop makes the animation happen.
Putting spinDisplay as idle func means it is indeed called repeatedly by glut when there is no other event to process. It is not ideal as the rotation speed depends of the good will of glut, the cpu speed, other processes, etc but it is still something.
About centering the rotation : probably the easier is to center the glOrtho so that 0,0 is on the center of the screen, ie glOrtho(-1,1,-1,1,-1,1); and same for the rect coords.
I am surprised about the “won’t stop rotating on right click”.
Try some basic tracing with printf("was here 1
"); to check what paths are really used in your code (ie. does GLUT_RIGHT_BUTTON actually hapens, etc).
Hi, I’m learning Open GL and searched to find why my code from the book
OpenGL Programming Guide 3RD ED page 24 (same project as yours) wouldn’t rotate.
I compared my code to yours and noticed that the book omitted a ‘break’ after the first mouse function switch-case-if statement! !! The rest of the books code is correct and it now works. Thanks for helping! Correction : the book did have the break in it, but I didn’t notice until I saw your code here. Apologies, and thanks again! (Seems to be a good book by the way!)