Advertisement

09.15.2008 at 05:12PM PDT, ID: 23733731
[x]
Attachment Details

OpenGL (Timer Function)

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5
I'm working on an OpenGL program that spins a "square".  

Below is the code thus far.   Instead of the "void spinDisplay" function, I now want to use a "timer" function (included below the spinDisplay).    When simply commenting out the spinDisplay, I get a compile error.    

What am I missing?   Do I need to keep portions of the spinDisplay or is my timer_func incorrect (or placed incorrectly within the program)?

Thanks,
EEH
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
 
#define DEGREES_TO_RADIANS 3.14159/180.0
 
static GLfloat spin = 0.0;
GLfloat x, y;
 
void square()
{
 	glBegin(GL_QUADS);
       	glVertex2f(x, y);
       	glVertex2f(-y, x);
       	glVertex2f(-x, -y);
       	glVertex2f(y,-x);
    glEnd();
}
 
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    square();
    glutSwapBuffers ();
}
 
 
void spinDisplay ()          // ORIGINAL FUNCTION
{
    spin = spin + 2.5;
    if (spin > 360.0) spin = spin - 360.0;
    x = 25.0 * cos(DEGREES_TO_RADIANS * spin);
    y = 25.0 * sin(DEGREES_TO_RADIANS * spin);
    glutPostRedisplay();
}
 
 
void timer_func(int n)           // NEW FUNCTION
{
	// Update the object positions, etc.
	glutPostRedisplay();
         //glutTimerFunc(n, timer_func, n); 
	glutTimerFunc(5, timer_func, 5);
}
 
 
void myinit ()
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 0.0, 1.0);
    glShadeModel(GL_FLAT);
}
 
void mouse(int btn, int state, int x, int y)
{
   	if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    	glutIdleFunc(spinDisplay);
    if (btn == GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
    	glutIdleFunc(NULL);
}
	
void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
    glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,
        50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
    else
    glOrtho (-50.0*(GLfloat)w/(GLfloat)h,
        50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity ();
}
 
/*  main loop
 *  open window with initial window size, title bar,
 *  RGBA display mode, and handle input events
 */
int main(int argc, char** argv)
{
 
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("double buffered");
    myinit ();
    glutDisplayFunc(display);
    glutReshapeFunc (myReshape);
    glutIdleFunc (spinDisplay);
    glutMouseFunc (mouse);
 
    glutMainLoop();
}
Answered By: mitsujin
Expert Since: 08/25/2008
Accepted Solutions: 1
Computer Expertise: Intermediate
Education: Edith Cowan University, Bachelor's Degree
mitsujin has been an Expert for 4 months, during which he has posted 11 comments and answered 1 question. mitsujin is just one of 71 experts in the OpenGL Graphics & Game Programming Zone. 1 expert collaborated on this answer, which was graded an "A" by the asker.
 
 
20081119-EE-VQP-48 / EE_QW_2_20070628