OpenGL -- 透视图,模型变换

来源:互联网 发布:双休日算法定节假日吗 编辑:程序博客网 时间:2024/06/11 15:35

透视图有正交视图(正视图),摄像机视图模式(可以看到远近效果)等
模型变换–平移,旋转,缩放等

透视图

截图

这里写图片描述

这里写图片描述

代码

#include <gl\glut.h>    #include <gl\GLU.h>#include <gl\GL.h>#include <math.h>#include <windows.h>#include <stdio.h>#include <stdlib.h>#define PI 3.1415926     int WinWidth, WinHeight;void Draw(){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glColor3f(1.0, 0.0, 0.0);    glBegin(GL_LINES);    glVertex3f(-5, 0, 20); // 竖线1    glVertex3f(-5, 0, -50);    glVertex3f(5, 0, 20); // 竖线2    glVertex3f(5, 0, -20);    glEnd();    glColor3f(0.0, 1.0, 0.0);    int i = -100;    for (int i = -50; i <= 20; i += 2)    {        // 线段是等长的,并且在坐标系中的y z值相同的        glBegin(GL_LINES);            glVertex3f(-5, 0, i);            glVertex3f(5, 0, i);        glEnd();    }    glutSwapBuffers();}void Reshape(int w, int h){    WinWidth = w;    WinHeight = h;    // //改变显示区域,起始位置为客户端窗口左下角(非坐标原点)    glViewport(0, 0, w, h);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    //宽高比改为当前值,视线区域与屏幕大小一致;    gluPerspective(45, 1.0*WinWidth / WinHeight, 1, 1000);    // 开启更新深度缓冲区的功能    glEnable(GL_DEPTH_TEST);    // 摄像机视图观看,从 (0,5,20) 往(0,0,0)处看,(0,1,0)为正方向    gluLookAt(0, 5, 20, 0, 0, 0, 0, 1, 0); }int main(int argc, char *argv[]){    WinWidth = 400;    WinHeight = 400;    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);    //glutInitWindowPosition(100, 100);    glutInitWindowSize(WinWidth, WinHeight);    glutCreateWindow("HelloOpenGL");    /*        目的:当窗口尺寸改变时,图形比例不发生变化        思路:窗口宽高比改变时,通过改变窗口显示区域大小,并利用投影矩阵改变观测物体大小使之适应变化。    */    glutReshapeFunc(&Reshape);    glutDisplayFunc(&Draw);    glutMainLoop();    return 0;}

参考
http://www.cppblog.com/COOOOOOOOL/archive/2009/12/28/104255.html


模型变换

代码

#include <gl\glut.h>    #include <gl\GLU.h>#include <gl\GL.h>#include <math.h>#include <windows.h>#include <stdio.h>#include <stdlib.h>float Width;float Height;void display(void){    glClear(GL_COLOR_BUFFER_BIT);    // 1    glLoadIdentity();    glColor3f(1.0, 0.0, 0.0);    glutWireSphere(0.2, 10, 8); // draw smaller planet    glBegin(GL_LINES); // 划线段        glVertex2f(-0.5,0);        glVertex2f(0.5,0);    glEnd();    // 2    glPushMatrix();        glLoadIdentity();        glColor3f(0.0, 1.0, 0.0);        glTranslatef(0.4f, 0.4f, 0.0f); // 平移变换        glutWireSphere(0.2, 10, 8); // draw smaller planet    glPopMatrix();    // 3    glPushMatrix();        glLoadIdentity();        glColor3f(0.0, 0.0, 1.0);        glTranslatef(0.4, -0.1, -0.4); // 平移变换        glRotatef(30.0f, 0.0f, 1.0f, 0.0f); // 绕y轴 逆时针30度        glutWireSphere(0.2, 10, 8); // draw smaller planet    glPopMatrix();    // 二维剪裁,正射投影    gluOrtho2D(0.0, Width, 0.0, Height);    glColor3f(1.0, 0.0, 0.0);    int p1[] = { 110, 50 };    int p2[] = { 20, 0 };    int p3[] = { 55, 80 };    int p4[] = { 90, 0 };    int p5[] = { 0, 50 };    glBegin(GL_LINE_LOOP); // 画五角星        glVertex2iv(p1);        glVertex2iv(p2);        glVertex2iv(p3);        glVertex2iv(p4);        glVertex2iv(p5);    glEnd();    glColor3f(0.0, 1.0, 0.0);    glTranslatef(140.0, 0.0, 0.0); // 平移变换    glScalef(2, 2, 1); // 缩放    glBegin(GL_LINE_LOOP);        glVertex2iv(p1);        glVertex2iv(p2);        glVertex2iv(p3);        glVertex2iv(p4);        glVertex2iv(p5);    glEnd();    glLoadIdentity();    glBegin(GL_LINES); // 划线段        glVertex2f(0, -0.5);        glVertex2f(0, 0.5);    glEnd();    glutSwapBuffers();}void Reshape(int w, int h){    Width = w;    Height = h;    // //改变显示区域,起始位置为客户端窗口左下角(非坐标原点)    glViewport(0, 0, Width, Height);    glMatrixMode(GL_PROJECTION);//修改投影矩阵    glLoadIdentity();//导入单位阵    gluOrtho2D(0.0, Width, 0.0, Height);}int main(int argc, char** argv){    Width = 500;    Height = 500;    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);    //glutInitWindowPosition(100, 100);    glutInitWindowSize(Width, Height);    glutCreateWindow(argv[0]);    glutReshapeFunc(&Reshape);    glutDisplayFunc(display);    glutMainLoop();    return 0;}

运行截图

这里写图片描述

0 0
原创粉丝点击