Zooming Issues with glutMouseWheelFunc - c++

I tired implementing a simple zoom in/out using the glutMouseWheelFunc in VC++. I am able to achieve the zooming in/out but while doing so, the axes(GLlines) tend to disappear after zooming more than a certain level.
Am using the glutMouseWheelFunc to increase/decrease z- axis value in glTranslatef.
I have defined 'z' of glTranslatef for as camera distance:
float cDist= 30.0f; // camera distance
Then used it in glTranslatef as,
glTranslatef(0.0f, 0.0f, -cDist);
in display function below.
void display(void) {
glClearColor(0.0, 0.0, 0.0, 1.0); //clear the screen to black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear the color buffer and the depth buffer
enable();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -cDist);
glRotatef(xrot, 1.0, 0.0, 0.0);
---------------
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-500, 0, 0);
glVertex3f(500, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0, -500, 0);
glVertex3f(0, 500, 0);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0, 0, -500);
glVertex3f(0, 0, 500);
glTranslated(-xpos, 0.0f, -zpos); //translate the screento the position of our camera
glColor3f(1.0f, 1.0f, 1.0f);
glutSwapBuffers();
}
Afterwards, I defined wheelfunc as,
void mouseWheel(int button, int dir, int x, int y)
{
if (dir > 0)
{
cDist = cDist++;
}
else
{
cDist= cDist--;
}
return;
}
and called it in main function with glutMouseWheelFunc(mouseWheel);
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Window");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(mouseMovement);
glutMouseWheelFunc(mouseWheel);---- here
-----
---
glutMainLoop();
return 0;
}
Is this approach of zooming in proper? if not, what could be the alternative ? Also how can I define axes(lines I drew) to full extent of the screen?

thanks for the help. Seems like I missed up setting up proper depth value for gluPerspective. Once I increased the depth value, zoom out/in was working fine

Related

Opengl/Glut keyboard input trouble

i have an assignment to draw a pyramid onto the screen and then rotate after i press the 'k' key in my keyboard.
Everything works ok, except the rotation part as the program doesn't seem to be noting me pressing the key. The code goes as follows:
void changeSize(int w, int h) {
if(h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
// Set perspective
gluPerspective(45.0f ,ratio, 1.0f ,1000.0f);
// return to the model view matrix mode
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// put the geometric transformations here
// put drawing instructions here
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-100.0f, 0.0f, 0.0f);
glVertex3f( 100.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -100.0f, 0.0f);
glVertex3f(0.0f, 100.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -100.0f);
glVertex3f(0.0f, 0.0f, 100.0f);
glEnd();
//triangulos da base
glBegin(GL_TRIANGLES);
glColor3f(255.0f,255.0f,255.0f);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
//triangulos das faces
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(1.0f,0.0f,1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,-1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(0.0f,2.0f,0.0f);
glVertex3f(-1.0f,0.0f,1.0f);
glEnd();
// End of frame
glutSwapBuffers();
}
// write function to process keyboard events
void rotate (unsigned char key, int x, int y) {
if (key == 'k')
glRotatef(45,1.0,1.0,0.0);
glutPostRedisplay();
}
int main(int argc, char **argv) {
// init GLUT and the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1200,1200);
glutCreateWindow("CG#DI-UM");
// Required callback registry
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
// put here the registration of the keyboard callbacks
glutKeyboardFunc(rotate);
// OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// enter GLUT's main cycle
glutMainLoop();
return 0;
}
It seems that my program is not noting me pressing the key even tho i am. Im tried only printing the keys that are being pressed, and that works, so i'm really lost here.
glRotatef seem to have no effect in rotate, because glLoadIdentity() is called at the begin of renderScene. Actually glRotatef changes the current matrix, but when the scene is rendered (renderScene), then glLoadIdentity() loads the Identity matrix to the current matrix.
Anyway it is a bad style to do changes to the current matrix in input event callbacks. Change states and values of variables in the input events and use the variables to set the current model view matrix before the scene is rendered in renderScene.
Add a global variable angle:
float angle = 0.0f;
Change the value of the variable in renderScene. e.g:
void rotate (unsigned char key, int x, int y) {
if (key == 'k') {
angle += 45.0f;
glutPostRedisplay();
}
}
And apply the rotation to the current matrix in renderScene:
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the camera
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,
0.0,0.0,0.0,
0.0f,2.0f,0.0f);
// apply rotation
glRotatef(angle, 1.0f, 1.0f, 0.0f);
// [...]
}

explanation of glFrustum() compared to gluLookAt()

I'm debugging a code where i want to look at a scan of a depth image, but my camera setup doesn't let me see the scan. Thus, i'm playing around with camera setups. I create a huge point at (30,120,800) and i can see it using gluLookAt. What is the according setup for glFrustum(left, right, bottom, top, near, far)? Using my setup, the point should lay exactly in the center, but i cannot see it.
void onDisplay()
{
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDisable(GL_CULL_FACE);
glPushMatrix();
glPointSize( 1111111116.0 );
glColor3f( 0.25f, 0.907, 0.731f );
glLoadIdentity();
// Set the camera
//gluLookAt( 30.f, 120.0f, 800.f, 30.f, 120.0f, 800.f, 0.0f, 1.f, 0.0f); //works
glFrustum( 28, 32, 118, 122, 798, 802);
glBegin( GL_POINTS );
glVertex3f( 30, 120, 800 ); //30,120,800
glEnd();
glPopMatrix();
//glFinish();
//glutSwapBuffers();
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char **argv)
{
// initialize GLUT
glutInitWindowSize(800, 800);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInit(&argc, argv);
glutCreateWindow("HeadPoseDemo");
glutKeyboardFunc (processSpecialKeys);
glutDisplayFunc(onDisplay);
glutMainLoop();
return 0;
}
The devil's in the details: Note how in the documentation they say
nearVal, farVal
Specify the distances to the near and far depth clipping planes. Both distances must be positive.
That's different from the wording they use for the other parameters:
left, right
Specify the coordinates for the left and right vertical clipping planes.
What you're expected to know is that you're looking along the negative z-axis. So my guess is that you're going to see your point when you change it to glVertex3f(30.f, 120.f, -800.f);.

How do I change a white rectangle into a colored rectangle using glut?

I am trying to make a rectangle of any color other than white or black, but seem to be failing miserably as it is always white no matter what I put in the code. I don't know what I'm doing wrong (if anything is wrong with my code even, as far as I can tell it is no different from some examples I have seen even). Here is the code I have that I think should be making a red rectangle but only makes a white one:
#include <gl/glut.h>
void mydisplay ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); //sets color
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
//glutSwapBuffers();
//glutSolidTeapot(1);
}
int main (int argc, char** argv)
{
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
Somehow you forgot to call some functions, for example glut initialization and setup window size. Also you did not set clear color and commented glutSwapBuffers function.
#include <gl/glut.h>
void mydisplay()
{
glClearColor(1.0, 1.0, 1.0, 1.0); // add this
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); //sets color
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv); // add this
glutInitWindowSize(640, 480); // add this
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
I marked lines that I have added. Try it.
Ok, what I have found is that I was missing one line of code. I needed glFlush(); instead of glutSwapBuffers(); Also, I could blend colors by assigning colors to each vertex. Here is what I have now, which will give a multicolored square:
#include <gl/glut.h>
void mydisplay ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//sets color
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, 0.5);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5, 0.5);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
//glutSwapBuffers();
//glutSolidTeapot(1);
}
int main (int argc, char** argv)
{
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
}
The other code given in the previous answer works as well. Hence I am voting it up, but accepting my answer (the glFlush() works for my original code as well, and is a simpler fix).

How to display a sphere correctly in openGL

I don't know very much about openGL/glut, but I've used it before successfully for some exceedingly simple things in 2D.
Now I want to be able to draw spheres in 3D. I'm trying to simulate particle collisions, so all I'm really going to need to do on the graphics end is draw spheres.
Here's my abortive attempt
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glutSwapBuffers();
}
void timerProc(int arg)
{
glutTimerFunc(50,timerProc,0);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glColor3f(0.0,0.0,0.0); //color = black
glPushMatrix();
glTranslated(0,0,0);
glutSolidSphere(.74, 500, 500);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
srand(time(NULL));
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50,30);
glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)-80,glutGet(GLUT_SCREEN_HEIGHT)-60);
mainWindow=glutCreateWindow("New Window"); //global variable
WIDTH=glutGet(GLUT_WINDOW_WIDTH); //global variable
HEIGHT=glutGet(GLUT_WINDOW_HEIGHT); //global variable
glutDisplayFunc(renderScene);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glutTimerFunc(50,timerProc,0);
glutMainLoop();
return 0;
}
Hopefully all of my problems stem from one really basic mistake...
For some reason, this creates an oval. And, though the oval is pretty big (maybe about an 1/8th of the screen wide and tall), if I lower the radius down to .73 it vanishes, I'm guessing because it's too small to see.
How would I make it so that this sphere would show up circular like you'd expect, and so that as I can see everything that's happening in a given volume, say a 10x10x10 box, the way you would if you were just standing next to a box of particles that were flying around and peering into it, or a reasonable approximation. Right now it's hard to tell what exactly I'm looking at (I know that I'm standing at 1,1,1 and looking at the origin, but it's hard to grasp exactly what I'm seeing)
Also, occasionally when I run it the whole screen is just black. Then when I clean and build and run again it's fine. Not really a huge concern, but annoying, and I'd love to understand what was going on.
Also, when I the number of slices and stacks was lower, it would look fine if the radius was large, but become extremely distorted when the radius was small, which I thought was very strange...
The main problem you are having here is Z clipping. The initial Z range for the scene is (-1, 1) so you only see a part of the actual sphere and by change in its size you go out of z range.
Image
There are several problems I see in the code.
It is good to get a grasp of how the GLUT workflow actually works.
Lets see what the code does wrong.
Main
int main(int argc, char **argv)
{
srand(time(NULL));
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50, 30);
glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH) - 80,
glutGet(GLUT_SCREEN_HEIGHT) - 60);
mainWindow = glutCreateWindow("New Window"); //global variable
WIDTH = glutGet(GLUT_WINDOW_WIDTH); //global variable
HEIGHT = glutGet(GLUT_WINDOW_HEIGHT); //global variable
glutDisplayFunc(renderScene);
Here you define the display function. It is called every time the window contents has to be invalidated. In this case it is invalidated only at start. The renderScene function does not do anything awesome, just clears the screen. So you get a black screen at the beginning.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
No need for blending at the moment. You can skip that part altogether.
glutTimerFunc(50, timerProc, 0);
Now you set up the timerProc function to be called in 50 milliseconds.
glutMainLoop();
As the documentation states: glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.
return 0;
}
Render Scene
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
This is the only place where you clear the screen. Timer Func does not do this.
glLoadIdentity();
You are reseting the matrices.
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
Setting up the matrices. (One matrix to be precise)
glutSwapBuffers();
And without drawing anything you swap buffers.
}
Scene rendering function is called each time the window frame has to be redrawn.
Timer
This function does rely on the screen being cleared at first by the renderScene.
void timerProc(int arg)
{
glutTimerFunc(50, timerProc, 0);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
Not clearing this time. Only setting the color.
glColor3f(0.0, 0.0, 0.0); //color = black
glPushMatrix();
glTranslated(0, 0, 0);
glutSolidSphere(.74, 500, 500);
glPopMatrix();
glutSwapBuffers();
}
How to fix it?
Just setup the matrices. With proper Z range.
void resetTransformations() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1000, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
}
void renderScene()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
resetTransformations();
// Just to see some triangles
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0, 0.0, 0.0); //color = black
glPushMatrix();
glTranslated(0, 0, 0);
glutSolidSphere(0.74, 500, 500);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
srand(time(NULL));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50, 30);
glutInitWindowSize(256, 256);
mainWindow = glutCreateWindow("New Window"); //global variable
WIDTH = glutGet(GLUT_WINDOW_WIDTH); //global variable
HEIGHT = glutGet(GLUT_WINDOW_HEIGHT); //global variable
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutMainLoop();
return 0;
}

Entire object rotates

The idea from this code is to let a windmill like structure to rotate, the problem is that the entire object rotates instead of the windmill fan itself (not the red triangles only). here is the code (I use the keys to control speed)
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
float angle = 0.00002f;
int refreshMills = 30;
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
void Timer(int value) {
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, Timer, 0); // next Timer call milliseconds later
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f)
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, 0.2f);
glVertex2f(-0.2f, 0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, -0.2f);
glVertex2f(0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.2f)
glVertex2f(-0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, 0.2f);
glVertex2f(0.2f, 0.4f);
glEnd();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
angle=angle+0.000002f;
glutPostRedisplay();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.6f);
glVertex2f(0.4f, -0.6f);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'a':{
angle+=1;
glutPostRedisplay();
}
case 's':
angle+=2;
glutPostRedisplay();
case 'd':
angle+=3
glutPostRedisplay();
case 'f':
angle=0;
}
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUTx
glutCreateWindow("Windmill"); // Create window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display);
glutTimerFunc(0, Timer, 0);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(keyboard);
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the event-processing loop
return 0;
}
You need to implement some sort of hierarchy(commonly a scene graph) here that uses transformation matrixes to do your transformations.
Basically, create a "Windmill" object that has its own transformation matrix. Then create a "Windmill Fan" object that has its own. Make the fan a child of the parent. The transformations essentially propagate down. Transform the Windmill, then transform the Windmill Fan.
Post on Scene Graphs
You may also want to check out the Game Development section of stackoverflow. These questions are usually not met with open arms here.
You need to clear your transformations before trying to draw a new frame. So, put a glLoadIdentity() after you glClear function.
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity(); // <-- put it here
glBegin(GL_TRIANGLES);
...
You need to call glRotate before drawing the rotating object. And before that, you need to push the current model view matrix onto the stack with glPushMatrix, then pop it with glPopMatrix before drawing the rest of the windmill.
Once you have a more complex scene you might consider using a scene graph. Note that the matrix functions like glRotate are now deprecated. You might consider switching to e.g. glm as soon as possible.
Try this:
#include <GL/glut.h> // GLUT, include glu.h and gl.h
void Timer(int value)
{
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(16, Timer, 0); // next Timer call milliseconds later
}
float rate = 1.0f;
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
rate=2;
break;
case 's':
rate=3;
break;
case 'd':
rate=4;
break;
case 'f':
rate=0;
break;
}
glutPostRedisplay();
}
float angle = 0.0f;
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// base
glPushMatrix();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.6f);
glVertex2f(0.4f, -0.6f);
glEnd();
glPopMatrix();
// prop
glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
angle=angle+rate;
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, 0.2f);
glVertex2f(-0.2f, 0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, -0.2f);
glVertex2f(0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.4f, -0.2f);
glVertex2f(-0.2f, -0.4f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, 0.2f);
glVertex2f(0.2f, 0.4f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUTx
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow("Windmill"); // Create window with the given title
glutDisplayFunc(display);
glutTimerFunc(0, Timer, 0);
glutKeyboardFunc(keyboard);
glutMainLoop(); // Enter the event-processing loop
return 0;
}