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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算机图形学-图形几何变换相关的知识,希望对你有一定的参考价值。

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

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

技术分享

 

以上是关于计算机图形学-图形几何变换的主要内容,如果未能解决你的问题,请参考以下文章

计算机图形学几何变换基础

利用C语言实现二维图形的变换

opengl算法学习---图形几何变换

计算机图形学 开源的库都有哪些,主要用于二维

WPF的三维变换应用

图形学计算机图形学知识点提纲8