openGL的问题,关于三角形旋转的。。新手入门
ztdtc 2009-03-30 10:53:47 老师要求我们进行4个三角型的旋转,请大家帮忙看看我的程序里有什么错误,为什么不会进行旋转?谢谢了
#include<GL/glut.h>
#include<math.h>
double temp;
#define DEG_TO_RAD 0.017453
GLfloat theta = 1.0;
void myidle()
{
theta += 0.9;
if (theta >= 360.0)
theta -= 360.0;
theta = theta* DEG_TO_RAD;
glutPostRedisplay();
}
class wcPt2D
{
public:
GLfloat x,y;
};
void init (void)
{
glClearColor (1.0,1.0,1.0,0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (-100.0,100.0,-100.0,100.0);
glMatrixMode (GL_MODELVIEW);
}
void triangle (wcPt2D *verts)
{
GLint k;
GLfloat i,j;
for(k=0;k<=2;k++)
{
i=verts[k].x;
j=verts[k].y;
verts[k].x=i*cos(theta)-j*sin(theta);
verts[k].y=i*sin(theta)+j*cos(theta);
}
glBegin (GL_TRIANGLES);
for (k=0;k<3;k++)
glVertex2f (verts[k].x,verts[k].y);
glEnd();
}
void displayFcn (void)
{
wcPt2D verts [3] ={{-50.0,-25.0},{50.0,-25.0},{0.0,50.0}};
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0,0.0,1.0);
glViewport (0,0,300,300);
triangle (verts);
glColor3f (1.0,1.0,0.0);
glViewport (300,0,300,300);
triangle (verts);
glColor3f (1.0,0.0,1.0);
glViewport (300,300,300,300);
triangle (verts);
glColor3f (1.0,0.0,0.0);
glViewport (0,300,300,300);
triangle (verts);
glEnd();
glFlush();
glutSwapBuffers();
}
void main (int argc, char ** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition (50,50);
glutInitWindowSize(600,600);
glutCreateWindow("Split-Screen Example");
init ();
glutDisplayFunc (displayFcn);
glutIdleFunc(myidle);
glutMainLoop();
}