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

 

转载于:https://www.cnblogs.com/yuwl26/p/4218740.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值