码迷,mamicode.com
首页 > 其他好文 > 详细

计算机图形学-图形几何变换

时间:2017-10-15 18:06:57      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:lint   pos   空闲   str   平移   rspec   double   ini   lsh   

内容:金字塔的平移以及旋转的实现,通过鼠标控制金字塔的转速以及运行窗口的退出

#include <GL/glut.h>
#include <stdlib.h>
GLfloat   rtri;//金字塔旋转角度
float add = 0.1f;
void init(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glShadeModel(GL_SMOOTH);//设为smooth处理方式,注意看区别
    glEnable(GL_DEPTH_TEST);//激活深度测试
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    //清除颜色缓存和深度缓存
    glLoadIdentity();
    glTranslatef(-1.5f, 0.0f, -6.0f);
    glRotatef(rtri, 0.0f, 1.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glEnd();


    rtri += add;//加一个角度
    glutSwapBuffers();//交换双缓存
}
void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
    if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
         if (add == 0) 
             add = 0.1f; 
         add = add * 2; 
    }
    if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){
        if (add == 0) 
            add = 0.1f;  
        add = add / 2;
    }
    if (button == GLUT_MIDDLE_BUTTON && action == GLUT_DOWN)
        exit(0);
    glutPostRedisplay(); // 刷新窗口
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);//使用双缓存模式和深度缓存
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Transform2");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);//设置空闲时调用的函数
    glutMouseFunc(MousePlot);//鼠标响应
    glutMainLoop();
    return 0;
}

技术分享

 

计算机图形学-图形几何变换

标签:lint   pos   空闲   str   平移   rspec   double   ini   lsh   

原文地址:http://www.cnblogs.com/avecle/p/7671193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!