ubuntu+opengl 程序设计(5) 开始三维画图

来源:互联网 发布:鼎桥通信怎么样 知乎 编辑:程序博客网 时间:2024/06/15 09:04

ubuntu+opengl程序设计(5) 开始三维画图!

--lihn1987(转载请著名作者)

哈哈,这次终于好绘制3D图形啦~~~~~~

首先!上图!



再上代码!

// Points.c
// OpenGL SuperBible
// Demonstrates OpenGL Primative GL_POINTS
// Program by Richard S. Wright Jr.

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>


// Define a constant for the value of PI
#define GL_PI 3.1415f

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;


// Called to draw scene
void RenderScene(void)
{
GLfloat x,y,z,angle;
// Storeage for coordinates and angles

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);

// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot,
1.0f, 0.0f, 0.0f);
glRotatef(yRot,
0.0f, 1.0f, 0.0f);

// Call only once for all remaining points
glBegin(GL_POINTS);

z = -
50.0f;
for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; angle += 0.1f)
{
x =
50.0f*sin(angle);
y =
50.0f*cos(angle);

// Specify the point and move the Z value up a little
glVertex3f(x, y, z);
z +=
0.5f;
}

// Done drawing points
glEnd();

// Restore transformations
glPopMatrix();

// Flush drawing commands
glutSwapBuffers();
}

// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(
0.0f, 0.0f, 0.0f, 1.0f );

// Set drawing color to green
glColor3f(
0.0f, 1.0f, 0.0f);
}

void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-=
5.0f;

if(key == GLUT_KEY_DOWN)
xRot +=
5.0f;

if(key == GLUT_KEY_LEFT)
yRot -=
5.0f;

if(key == GLUT_KEY_RIGHT)
yRot +=
5.0f;

if(key > 356.0f)
xRot =
0.0f;

if(key < -1.0f)
xRot =
355.0f;

if(key > 356.0f)
yRot =
0.0f;

if(key < -1.0f)
yRot =
355.0f;

// Refresh the Window
glutPostRedisplay();
}


void ChangeSize(int w, int h)
{
GLfloat nRange =
100.0f;

// Prevent a divide by zero
if(h == 0)
h =
1;

// Set Viewport to window dimensions
glViewport(
0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(
"Points Example");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();

return 0;
}

 

 

 

 

 

 

在这里我们考到了在绘制函数中有几个没有见过的函数

glPushMatrix

保存矩阵堆栈

glPopMatrix

弹出修改前堆栈

这两个函数用起来感觉很象汇编里面的压栈和弹栈操作哈,其具体作用适用来在修改绘制的过程中不影响原来的矩阵。我的理解是这样的哈,

本程序中在压栈和弹栈前做了以下操作

首先调用了

void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);

对绘制的矩阵进行旋转。

该函数第一个参数angle是旋转的角度,而后面的表示旋转的坐标轴

 

glBegin(GL_POINTS);

z = -50.0f;

for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; angle += 0.1f)

{

x = 50.0f*sin(angle);

y = 50.0f*cos(angle);

// Specify the point and move the Z value up a little

glVertex3f(x, y, z);

z += 0.5f;

}

// Done drawing points

glEnd();

 

这一段比较好理解

glBegin(GL_POINTS); glEnd();

这俩函数之间表示要绘制坐标点

而之后的

for(angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; angle += 0.1f)

表示将要在绘制3圈的原点,同时z轴在不停增加

至此绘制函数也就完了。

 

 

最后说下我们的按键消息回调函数

void SpecialKeys(int key, int x, int y)

其中key表示按键的键值,而xy表示当此事件发生时鼠标的坐标

原创粉丝点击