孙其功陪你学之——gluPerspective();glMatrixMode();glLoadIdentity():函数

本文介绍了OpenGL中的glMatrixMode()和glLoadIdentity()函数的作用与用法,并通过实例展示了如何使用这些函数来设置不同的矩阵堆栈及如何配置透视效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

glMatrixMode()函数和glLoadIdentity()函数

glMatrixMode():指定哪一个矩阵是当前矩阵

C语言描述

void glMatrixMode(GLenum mode)

参数

mode指定哪一个矩阵堆栈是下一个矩阵操作的目标,可选值:GL_MODELVIEW、GL_PROJECTION、GL_TEXTURE.

说明

glMatrixMode设置当前矩阵模式:

GL_MODEVIEW,对模型视景矩阵堆栈应用随后的矩阵操作。

GL_PROJECTION,对投影矩阵应用随后的矩阵操作。

GL_TEXTURE,对纹理矩阵堆栈应用随后的矩阵操作。

glLoadIdentity():该函数的功能是重置当前指定的矩阵为单位矩阵。

在glLoadIdentity()之后我们为场景设置了透视图。glMatrixMode(GL_MODELVIEW)设置当前矩阵为模型视图矩阵,模弄视图矩阵储存了有关物体的信。
gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble zNear,GLdouble zFar)
fovy,是眼睛睁开的角度,即,视角的大小,如果设置为0,相当你闭上眼睛了,所以什么也看不到,如果为180,那么可以认为你的视界很广阔,
aspect,是实际窗口的纵横比,即x/y
zNear,表示你近处的裁面,
zFar表示远处的裁面
#include <GL/glut.h>
#include <math.h>
#  define M_PI 3.141592649
int Width;   /* Width of window */
int Height;  /* Height of window */
void Redraw(void);
void Resize(int width, int height);

int                /* O - Exit status */
main(int  argc,    /* I - Number of command-line arguments */
     char *argv[]) /* I - Command-line arguments */
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
    glutInitWindowSize(792, 573);
    glutCreateWindow("1D Textured Rainbow");
    glutReshapeFunc(Resize);
    glutDisplayFunc(Redraw);
    glutMainLoop();
    return (0);
    }
void
Redraw(void)
    {
    GLfloat x, y, z, th;
    static GLubyte roygbiv_image[8][3] =
        {
        { 0x3f, 0x00, 0x3f }, /* Dark Violet (for 8 colors...) */
        { 0x7f, 0x00, 0x7f }, /* Violet */
        { 0xbf, 0x00, 0xbf }, /* Indigo */
        { 0x00, 0x00, 0xff }, /* Blue */
        { 0x00, 0xff, 0x00 }, /* Green */
        { 0xff, 0xff, 0x00 }, /* Yellow */
        { 0xff, 0x7f, 0x00 }, /* Orange */
        { 0xff, 0x00, 0x00 }  /* Red */
        };
    /* Clear the window to light blue */
    glClearColor(0.5, 0.5, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 /* Load the texture data */
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage1D(GL_TEXTURE_1D, 0, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE,
                 roygbiv_image);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    /* First draw the ground... */
    glDisable(GL_TEXTURE_1D);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glPushMatrix();
    glRotatef(10.0, 0.0, 1.0, 0.0);
    glTranslatef(0.0, -40.0, -100.0);
    glColor3f(0.0, 0.8, 0.0);
    glBegin(GL_POLYGON);
    for (th = 0.0; th < (2.0 * M_PI); th += (0.03125 * M_PI))
        {
        x = cos(th) * 200.0;
z = sin(th) * 200.0;
glVertex3f(x, 0.0, z);
        }
    glEnd();
    /* Then a rainbow... */
    glEnable(GL_TEXTURE_1D);
    glBegin(GL_QUAD_STRIP);
    for (th = 0.0; th <= M_PI; th += (0.03125 * M_PI))
        {
        x = cos(th) * 50.0;
y = sin(th) * 50.0;
z = -50.0;
glTexCoord1f(0.0);
glVertex3f(x, y, z);
 x = cos(th) * 55.0;
y = sin(th) * 55.0;
z = -50.0;
glTexCoord1f(1.0);
glVertex3f(x, y, z);
        }
    glEnd();
    glPopMatrix();
    glFinish();
    }
void
Resize(int width,  /* I - Width of window */
       int height) /* I - Height of window */
    {
    /* Save the new width and height */
    Width  = width;
    Height = height;
    /* Reset the viewport... */
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30.0, (float)width / (float)height, 0.1, 1000.0);//设置投射矩阵
    glMatrixMode(GL_MODELVIEW);
    }

 

参考来自:http://hi.baidu.com/jimmyhcl/item/a9f14010258fa45bf1090e79

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值