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