首页 > 代码库 > openGL+VS2010的例程--静态平滑变色三角形(二维)
openGL+VS2010的例程--静态平滑变色三角形(二维)
效果图如上:
步骤:首先,绘制顶点颜色不同的三角形;然后,设置边框大小改变时,重新按固定长宽比例投影,到整个显示界面。
实现代码如下:
#include <GL\glut.h>void Display(void){ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glBegin(GL_POLYGON); glColor3f(0.0,0.0,0.0); glVertex3f(-0.5,-0.5,-3.0); glColor3f(1.0,0.0,0.0); glVertex3f(0.5,-0.5,-3.0); glColor3f(0.0,0.0,1.0); glVertex3f(0.5,0.5,-3.0); glEnd(); glFlush(); //Finish rendering}void Reshape(int x, int y){ if (y == 0 || x == 0) return; //Nothing is visible then, so return //Set a new projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); //Angle of view:40 degrees //Near clipping plane distance: 0.5 //Far clipping plane distance: 20.0 gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0); glMatrixMode(GL_MODELVIEW); glViewport(0,0,x,y); //Use the whole window for rendering}int main (int argc, char **argv){ //Initialize GLUT glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(300,300); //Create a window with rendering context and everything else we need glutCreateWindow("Intro"); glClearColor(0.0,0.0,0.0,0.0); //Assign the two used Msg-routines glutDisplayFunc(Display); glutReshapeFunc(Reshape); //Let GLUT get the msgs glutMainLoop(); return 0;}
openGL+VS2010的例程--静态平滑变色三角形(二维)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。