这次主要学习的是关于在opengl中绘制直线,也比较简单
现上图吧
这图乍一看上去是不是像是一条条弯曲的直线???
我开始就把他看成这样了,
不过这是个视觉问题,仔细看过起,其实还是一个同心的直线,呵呵
然后上代码
// Lines.cpp
// Demonstrates primative GL_LINES
// OpenGL SuperBible
// Richard S. Wright Jr.
// rwright@starstonesoftware.com
#include
#include
#include
// Define a constant for the value of PI
#define GL_PI3.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 lines
glBegin(GL_LINES);
z = 0.0f;
for(angle = 0.0f; angle <= GL_PI; angle += (GL_PI / 20.0f))
{
// Top half of the circle
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
glVertex3f(x, y, z);
// Bottom half of the circle
x = 50.0f*sin(angle+GL_PI);
y = 50.0f*cos(angle+GL_PI);
glVertex3f(x, y, z);
}
// Done drawing points
glEnd();
glPopMatrix();
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);
}
///
// Respond to arrow keys
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();
}
///
// Window has changed size, recalculate projection
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 coordinate system
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);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
///
// Main Program Entry Point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("Lines Example");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
同以前的内容相比呢,陌生的部分只有一小段哈
即以下一小段
glBegin(GL_LINES);
z = 0.0f;
for(angle = 0.0f; angle <= GL_PI; angle += (GL_PI / 20.0f))
{
// Top half of the circle
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
glVertex3f(x, y, z);
// Bottom half of the circle
x = 50.0f*sin(angle+GL_PI);
y = 50.0f*cos(angle+GL_PI);
glVertex3f(x, y, z);
}
// Done drawing lines
glEnd();
现说下其中的数学问题
for(angle = 0.0f; angle <= GL_PI; angle += (GL_PI / 20.0f))
表示从0角度开始,绘制GL_PI角度,一共绘制20条直线。假如我们把GL_PI改为GL_PI/2
即
for(angle = 0.0f; angle <= GL_PI/2; angle += ((GL_PI/2) / 20.0f))
得到的图形如下所示
而
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
是得到直线左上角坐标
x = 50.0f*sin(angle+GL_PI);
y = 50.0f*cos(angle+GL_PI);
是得到直线右下角坐标
数学问题算是解释完了,下面看看我们核心的绘制问题吧
简化后的这段程序可以写成
glBegin(GL_LINES);glVertex3f(-50, 50, 0);
glVertex3f(50, -50, 0);
glVertex3f(-50,-50, 0);
glVertex3f(50, 50, 0);glEnd();
得到的图形如下所示
可以看出,opengl绘制一条直线就是写两个坐标点而已,而每两个坐标点组成一对表示一条直线
glVertex3f所作的事情更具坐在的glBegin参数不同而不同,比如上一篇文章中说道的绘制点中 一个glVertex3f就表示绘制的点的坐标。不过总的来说glVertex3f就是确定一个点的位置而已
好了今天就学到这里了,祝大家学习开心哈
现在每天看点英文,觉得英文的水平进步点了,不过还是很菜,看来读E文书还是有作用的,呵呵