您的位置:首页 > 运维架构

使用OpenGL绘制三维场景

2013-11-11 10:47 931 查看
计算机图形学(OpenGL版) (第3版)

COMPUTER GRAPHICS USING OpenGL

清华大学出版社

三维变换:在三维场景中如何把物体变换到所需的位置和朝向。(OpenGL 提供所需矩阵)

对一个物体设置变换,然后再恢复到之前的变换,以便为下一次变换做准备。

摄像机视景体: 一个平行六面体,上下左右四个侧面由窗口边界决定,前后两个面是近平面和远平面,沿着z轴投影到窗口,外面的则被裁剪。

操作模型视点矩阵:glMatrixMode(GL-MODELVIEW);

投影矩阵:glMatrixMode(GL_PROJECTION);

摄像机的定位和瞄准:

gluLookAt(eye.x , eye.y, eye.z, look.x, look.y,look.z, up.x, up.y,up.z);

//创建视点矩阵,然后后乘当前矩阵。

u、v、n三个归一向量(单位向量)。 ==》 左,上,垂直向内。

glMatrixMode(GL_PROJECTION); //设置观察体

glLoadIdentity();

glOrtho(-3.2,3.2,-2.4,2.4,1,50); //观察体 6.4 × 4.8 near=1,far=50

glMatrixMode(GL_MODELVIEW); //设置安防摄像机

glLoadIdentity();

gluLookAt(4,4,4, 0,1,0, 0,1,0); //摄像机视点(4,4,4), 朝向远点方向盯着(0,1,0)点,UPguess方向初设为(0,1,0)向量;

//初试三维

#include <windows.h>

#include <gl/glut.h>

#include <math.h>

#include<iostream>

using namespace std;

const GLint xPosition = 100;

const GLint yPosition = 100;

const GLint screenWidth = 640;

const GLint screenHeight = 480;

const GLdouble aspect = screenWidth/(screenHeight*1.0);

void myDisplay();

void myInit();

void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);

void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);

int main(int argc, char **argv){

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowPosition(xPosition,yPosition);

glutInitWindowSize(screenWidth,screenHeight);

glutCreateWindow("Basic Shapes");

glutDisplayFunc(myDisplay);

myInit();

glutMainLoop();

return 0;

}

void myDisplay(){

glClear(GL_COLOR_BUFFER_BIT);

GLUquadricObj *qobj = gluNewQuadric();

gluQuadricDrawStyle(qobj,GLU_LINE); //结果显示的是线条

gluCylinder(qobj,0.2,0.2,0.4,8,8);//圆柱,上下圆半径0.2,高度0.4,绕着z轴切8块(切蛋糕),每块有8个线。

//glPopMatrix();

glFlush();

}

void myInit(){

glClearColor(1.0,1.0,1.0,1.0);

glColor3f(0.0f,0.0f,0.0f);

glPointSize(2.0);

glLineWidth(2.0);

glMatrixMode(GL_PROJECTION);

//glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glOrtho(-2.0*aspect,2.0*aspect,-2.0,2.0,0.1,100);

gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: