蓝宝书 第三章
画三角形 triangle
1)绘制三角形(三个点确定一个三角形)
glBegin(GL_TRIANGLES);
glVertex2f(0.0f, 0.0f); // 点a
glVertex2f(25.0f, 25.0f); // 点b
glVertex2f(50.0f, 0.0f); // 点c
glVertex2f(-50.0f, 0.0f); // 点d
glVertex2f(-75.0f, 50.0f); // 点e
glVertex2f(-25.0f, 0.0f); // 点f
glEnd();
绘制三角形abc和三角形def
2)绘制三角形(新增三角形顶点)
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(0.0f, 0.0f); // 点a
glVertex2f(25.0f, 25.0f); // 点b
glVertex2f(50.0f, 0.0f); // 点c
glVertex2f(-50.0f, 0.0f); // 点d
glVertex2f(-75.0f, 50.0f); // 点e
glVertex2f(-25.0f, 0.0f); // 点f
glEnd();
绘制三角形abc、bcd、def(三点连线顺序不定,应该是与第一个三角形顺时针or逆时针连线统一)
3)绘制三角形(同一顶点,最后封闭)
glBegin(GL_TRIANGLE_FAN);glVertex2f(0.0f, 0.0f); // 点a
glVertex2f(25.0f, 25.0f); // 点b
glVertex2f(50.0f, 0.0f); // 点c
glVertex2f(-50.0f, 0.0f); // 点d
glVertex2f(-75.0f, 50.0f); // 点e
glVertex2f(-25.0f, 0.0f); // 点f
glEnd();
绘制三角形abc、acd、ade、aef及afb
相关代码见例3.8
例3.8 绘制三角形
#include <windows.h>
#include <math.h>
#include <GL\GL.h>
#include <GL\GLU.h>
#include <GL\glut.h>
GLboolean bCull = true;
GLboolean bDepth = true;
GLboolean bOutline = true;
GLfloat xRot = 30.0f;
GLfloat yRot = 30.0f;
// Define a constant for the value of PI
#define GL_PI 3.1415f
// This function does any needed initialization on the rendering
void RenderScene(void)
{
GLfloat x, y, angle; // Storage for coordinates and angles
int iPivot = 1; // Used to flag alternating colors
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Turn culling on if flag is set
if (bCull)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
// Enable depth testing if flag is set
if (bDepth)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
// Draw the back side as a wireframe only, if flag is set
if (bOutline)
glPolygonMode(GL_BACK, GL_LINE);
else
glPolygonMode(GL_BACK, GL_FILL);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// Begin a triangle fan
glBegin(GL_TRIANGLE_FAN);
// Pinnacle of cone is shared vertex for fan, moved up z-axis
// to produce a cone instead of a circle
glVertex3f(0.0f, 0.0f, 75.0f);
// Loop around in a circle and specify even points along the circle
// as the vertices of the triangle fan
for (angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI / 8.0f))
{
// Calculate x and y position of the next vertex
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
// Alternate color between red and green
if ((iPivot % 2) == 0)
glColor3f(0.0f, 1.0f, 0.0f);
else
glColor3f(1.0f, 0.0f, 0.0f);
// Increment pivot to change color next time
iPivot++;
// Specify the next vertex for the triangle fan
glVertex2f(x, y);
}
// Done drawing fan for cone
glEnd();
// Restore transformations
glPopMatrix();
glutSwapBuffers();
}
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);
// Set color shading model to flat
glShadeModel(GL_FLAT);
// Clockwise-wound polygons are front facing; this is reversed
// because we are using triangle fans
glFrontFace(GL_CW);
}
void ChangeSize(GLsizei w, GLsizei 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_RGBA);
glutInitWindowSize(800, 600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);//显示回调函数
glutReshapeFunc(ChangeSize);//窗口大小变形回调函数
SetupRC();
glutMainLoop();
return 0;
}