计算机图形学 几何变换代码

实现矩形在窗口内沿着水平线匀速移动

/*

*double.c

*Thisisasimpledoublebufferedprogram.

*Pressingtheleftmousebuttonrotatestherectangle.

*Pressingtherightmousebuttonstopstherotation.

*/

/*#include<GL/glut.h>

#include<stdlib.h>

staticGLfloatspin=0.0;

voiddisplay(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();

//glRotatef(spin,0.0,0.0,1.0);

glTranslatef(spin,0.0,0.0);

glColor3f(1.0,1.0,1.0);

glRectf(-10.0,-10.0,10.0,10.0);

glPopMatrix();

glutSwapBuffers();

}

voidspinDisplay(void)

{

spin=spin+0.5;//控制平移的速度

if(spin>360.0)

spin=spin-360.0;

glutPostRedisplay();

}

voidinit(void)

{

glClearColor(0.0,0.0,0.0,0.0);

glShadeModel(GL_FLAT);

}

voidreshape(intw,inth)

{

glViewport(0,0,(GLsizei)w,(GLsizei)h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);/*函数voidglOrtho(left,right,bottom,top,near,far):

设置投影方式为正交投影(平行投影),其取景体积是一个各面均为矩形的六面体;*/

/*glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

}

voidmouse(intbutton,intstate,intx,inty)

{

switch(button){

caseGLUT_LEFT_BUTTON:

if(state==GLUT_DOWN)

glutIdleFunc(spinDisplay);

break;

caseGLUT_MIDDLE_BUTTON:

caseGLUT_RIGHT_BUTTON:

if(state==GLUT_DOWN)

glutIdleFunc(NULL);

break;

default:

break;

}

}

intmain(intargc,char**argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(100,100);

glutCreateWindow(argv[0]);

init();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMouseFunc(mouse);

glutMainLoop();

return0;/*ANSICrequiresmaintoreturnint.*/

3、已知某三角形的三顶点坐标为{50.0,25.0},{150.0,25.0},{100.0,100.0}。

要求:(1)创建一个长宽分别为600、600的窗口,窗口的左上角位于屏幕坐标(100,100)处。(提示:请试着修改gluOrtho2D函数参数,使得绘制的三角形尽可能居中显示)

代码如下:

#include<GL/glut.h>

#include<stdlib.h>

voidinit(void)

{

glClearColor(0.0,0.0,0.0,0.0);

glShadeModel(GL_FLAT);

}

voiddraw_triangle(void)

{

glBegin(GL_LINE_LOOP);

glVertex2f(0.0,25.0);

glVertex2f(25.0,-25.0);

glVertex2f(-25.0,-25.0);

glEnd();

}

voiddisplay(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();

}

voidreshape(intw,inth)

{

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);

}

intmain(intargc,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();

return0;

}

运行结果为:

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值