实现矩形在窗口内沿着水平线匀速移动
* double.c
* This is a simple double buffered program.
* Pressing the left mouse button rotates the rectangle.
* Pressing the right mouse button stops the rotation.
*/
3、已知某三角形的三顶点坐标为{50.0,25.0},{150.0,25.0},{100.0,100.0}。
要求:(1)创建一个长宽分别为600、600的窗口,窗口的左上角位于屏幕坐标(100,100)处。(提示:请试着修改gluOrtho2D函数参数,使得绘制的三角形尽可能居中显示)
代码如下:
#include
#include
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void draw_triangle(void)
{
glBegin (GL_LINE_LOOP);
glVertex2f(0.0, 25.0);
glVertex2f(25.0, -25.0);
glVertex2f(-25.0, -25.0);
glEnd();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
glColor3f (1.0, 1.0, 1.0);
draw_triangle ();
glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0xF0F0);
glLoadIdentity ();
glTranslatef (-20.0, 0.0, 0.0);
draw_triangle ();
glLineStipple (1, 0xF00F);
glLoadIdentity ();
glScalef (1.5, 0.5, 1.0);
draw_triangle ();
glLineStipple (1, 0x8888);
glLoadIdentity ();
glRotatef (90.0, 0.0, 0.0, 1.0);
draw_triangle ();
glDisable (GL_LINE_STIPPLE);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
//glViewport ( w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
gluOrtho2D (-200.0, 200.0, -200.0*(GLfloat)h/(GLfloat)w,
200.0*(GLfloat)h/(GLfloat)w);//决定显示世界窗口中的那一部分内容,使用世界左边表示(左,右,下,上)
else
gluOrtho2D (-200.0*(GLfloat)w/(GLfloat)h,
200.0*(GLfloat)w/(GLfloat)h, -200.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600); //创建一个长宽分别为600、600的窗口,
glutInitWindowPosition (100, 100);//窗口的左上角位于屏幕坐标(100,100)处。
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}