OpenGL Study 3
来源:互联网 发布:大淘客cms怎么样 编辑:程序博客网 时间:2024/06/05 09:18
Lesson04
实现图形的旋转
bool DrawGLScene(GLvoid)
{
//clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//reset the current modelview matrix
glLoadIdentity();
//move left 1.5 units,deep 6.0 units from current position
//move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef(-1.5f,0.0f,-6.0f);
//rotate around the y axis
glRotatef(rtri,0.0f,1.0f,0.0f);
//draw a triangle,with smooth coloring
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
//reset the current modleview matrix
glLoadIdentity();
//move from (0.0,0.0,0.0) to (1.5,0.0,-6.0)
glTranslatef(1.5f,0.0f,-6.0f);
//rotate around the x axis
glRotatef(rquad,1.0f,0.0f,0.0f);
//draw a quad,the vertex is represented in the model coordinate
//with flat coloring
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
rtri+=0.2f;
rquad-=0.15f;
return true;
}
{
//clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//reset the current modelview matrix
glLoadIdentity();
//move left 1.5 units,deep 6.0 units from current position
//move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef(-1.5f,0.0f,-6.0f);
//rotate around the y axis
glRotatef(rtri,0.0f,1.0f,0.0f);
//draw a triangle,with smooth coloring
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
//reset the current modleview matrix
glLoadIdentity();
//move from (0.0,0.0,0.0) to (1.5,0.0,-6.0)
glTranslatef(1.5f,0.0f,-6.0f);
//rotate around the x axis
glRotatef(rquad,1.0f,0.0f,0.0f);
//draw a quad,the vertex is represented in the model coordinate
//with flat coloring
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glEnd();
rtri+=0.2f;
rquad-=0.15f;
return true;
}
使用glRotatef(r,x,y,z)使图形绕向量(x,y,z)旋转r弧度,r>0时,正对环绕轴来看,逆时针旋转。
代码中两次出现了glLoadIdentity()函数,在每绘制一个图形之前都将模型视图矩阵置一,然后在移动,旋转进行必要的变换,再绘制图形,这样可以防止不同图形之间变换矩阵的影响。
Lesson05
绘制3D物体
bool DrawGLScene(GLvoid)
{
//clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//reset the current modelview matrix
glLoadIdentity();
//move left 1.5 units,deep 6.0 units from current position
//move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef(-1.5f,0.0f,-6.0f);
//rotate around the y axis
glRotatef(rtri,0.0f,1.0f,0.0f);
//draw a diamond made of 4 triangles,with smooth coloring
glBegin(GL_TRIANGLES);
//front
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);//green
glVertex3f(-1.0f,-1.0f,1.0f);
glColor3f(0.0f,0.0f,1.0f);//blue
glVertex3f(1.0f,-1.0f,1.0f);
//right
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);//blue
glVertex3f(1.0f,-1.0f,1.0f);
glColor3f(0.5f,0.5f,0.0f);//rg
glVertex3f(1.0f,-1.0f,-1.0f);
//back
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.5f,0.5f,0.0f);//rg
glVertex3f(1.0f,-1.0f,-1.0f);
glColor3f(0.5f,0.0f,0.5f);//rb
glVertex3f(-1.0f,-1.0f,-1.0f);
//back
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.5f,0.0f,0.5f);//rb
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f);//green
glVertex3f(-1.0f,-1.0f,1.0f);
glEnd();
//reset the current modleview matrix
glLoadIdentity();
//move from (0.0,0.0,0.0) to (1.5,0.0,-6.0)
glTranslatef(1.5f,0.0f,-6.0f);
//rotate around the x axis
glRotatef(rquad,1.0f,0.0f,0.0f);
//draw a cubic made of 6 quads,the vertex is represented in the model coordinate
//with flat coloring
glBegin(GL_QUADS);
//top,y=1
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
//bottom,y=-1
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
//front,z=1
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
//right,x=1
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,1.0f);
//back,z=-1
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
//left,x=-1
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glEnd();
rtri+=0.2f;
rquad-=0.15f;
return true;
}
{
//clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//reset the current modelview matrix
glLoadIdentity();
//move left 1.5 units,deep 6.0 units from current position
//move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef(-1.5f,0.0f,-6.0f);
//rotate around the y axis
glRotatef(rtri,0.0f,1.0f,0.0f);
//draw a diamond made of 4 triangles,with smooth coloring
glBegin(GL_TRIANGLES);
//front
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);//green
glVertex3f(-1.0f,-1.0f,1.0f);
glColor3f(0.0f,0.0f,1.0f);//blue
glVertex3f(1.0f,-1.0f,1.0f);
//right
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);//blue
glVertex3f(1.0f,-1.0f,1.0f);
glColor3f(0.5f,0.5f,0.0f);//rg
glVertex3f(1.0f,-1.0f,-1.0f);
//back
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.5f,0.5f,0.0f);//rg
glVertex3f(1.0f,-1.0f,-1.0f);
glColor3f(0.5f,0.0f,0.5f);//rb
glVertex3f(-1.0f,-1.0f,-1.0f);
//back
glColor3f(1.0f,0.0f,0.0f);//red
glVertex3f(0.0f,1.0f,0.0f);
glColor3f(0.5f,0.0f,0.5f);//rb
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f);//green
glVertex3f(-1.0f,-1.0f,1.0f);
glEnd();
//reset the current modleview matrix
glLoadIdentity();
//move from (0.0,0.0,0.0) to (1.5,0.0,-6.0)
glTranslatef(1.5f,0.0f,-6.0f);
//rotate around the x axis
glRotatef(rquad,1.0f,0.0f,0.0f);
//draw a cubic made of 6 quads,the vertex is represented in the model coordinate
//with flat coloring
glBegin(GL_QUADS);
//top,y=1
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
//bottom,y=-1
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
//front,z=1
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
//right,x=1
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,1.0f);
//back,z=-1
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
//left,x=-1
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glEnd();
rtri+=0.2f;
rquad-=0.15f;
return true;
}
使用4个三角形组成一个金字塔,采用6个正方形组成一个正方体。
- OpenGL Study 3
- OpenGL study
- Study OpenGL
- OpenGL Study
- OpenGL Study 1
- OpenGL Study 2
- OpenGL Study 4
- OpenGL Study 5
- OpenGL Study 6
- opengl study resource
- How to study Android OpenGL ES
- How to study Android OpenGL ES
- awk study(3)
- Vim Tutorial Study(3)
- vc-study-3
- Mysql study note 3
- jQuery study note 3
- 3D model study
- list
- JTS学习-com.vividsolutions.jts.algorithm包
- 6个最常用的Word论文排版技巧
- /*发送消息,单项的传递消息*/
- 这是吿诉自己,要学会...
- OpenGL Study 3
- [分享] Linux下用Anjuta写个Hello World 的C++程序竟如此简单!
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Web 测试
- Eclipse环境配置
- 基于S3C2410平台的LCD for Linux 2.6 驱动移植
- /*接收消息--单项的接收消息*/
- JDBC基础1
- 耻民