使用OpenGL绘制三维场景

计算机图形学(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);
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值