// Purpose: To show how to draw triangles and Pyramid
// Author: zieckey
// Date: 2009/10/7
#include "GL/glut.h" #include <math.h>
// Rotation amounts
static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f;
void SpecialKeys(int key, int x, int y); void ChangeSize( GLsizei w, GLsizei h ); void DrawAxis(); void SetupRC();
void RenderScene() { //清空颜色缓冲区,填充的颜色由 glClearColor( 0, 0.0, 0.0, 1 ); 指定为黑色
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);
DrawAxis();
//绘制三角扇形
{ glBegin( GL_TRIANGLE_FAN ); { glVertex3f( 0, 0, 125.f );//圆锥的锥顶点是三角扇形的公共的顶点,在z轴上
#define PI 3.14159f #define RADIUS 75.f GLint faceFlag = 0; GLfloat x = 0, y = 0, angle = 0; for ( ; angle <= 2.0f * PI; angle += 0.1f, faceFlag++ ) { x = RADIUS * sin( angle ); y = RADIUS * cos( angle ); if ( faceFlag % 2 == 0 ) { glColor3f( 1, 1, 1 ); } else { glColor3f( 0.5, 0.5, 0.5 ); } glVertex3f( x, y, 0 ); }
//最后一个扇形
x = RADIUS * sin( 0.f ); y = RADIUS * cos( 0.f ); glVertex3f( x, y, 0 ); } glEnd();
//绘制圆锥体的底面
{ glBegin( GL_TRIANGLE_FAN ); { glVertex3f( 0, 0, 0 );//底面的顶点是三角扇形的公共的顶点,在(0,0,0)坐标原点处
#define PI 3.14159f #define RADIUS 75.f GLint faceFlag = 0; GLfloat x = 0, y = 0, angle = 0; for ( ; angle <= 2.0f * PI; angle += 0.1f, faceFlag++ ) { x = RADIUS * sin( angle ); y = RADIUS * cos( angle ); if ( faceFlag % 2 == 0 ) { glColor3f( 1, 1, 1 ); } else { glColor3f( 0.5, 0.5, 0.5 ); } glVertex3f( x, y, 0 ); } } glEnd(); } }
// Restore transformations
glPopMatrix();
glutSwapBuffers(); }
int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (800, 600); glutInitWindowPosition (100, 100); glutCreateWindow( "Triangles Example" ); glutDisplayFunc( RenderScene ); glutReshapeFunc( ChangeSize ); glutSpecialFunc( SpecialKeys ); SetupRC(); glutMainLoop();
return 1; }
void SetupRC() { glClearColor( 0, 0.0, 0.0, 1 ); glColor3f( 1.0f, 0.0f, 0.0f ); glShadeModel( GL_FLAT );//把着色模式设置为单调着色
glFrontFace( GL_CW );//把顺时针方向的面作为正面(逆时针方向为 GL_CCW )
}
void ChangeSize( GLsizei w, GLsizei h ) { GLfloat nRange = 200.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(); }
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(xRot > 356.0f) xRot = 0.0f;
if(xRot < -1.0f) xRot = 355.0f;
if(yRot > 356.0f) yRot = 0.0f;
if(yRot < -1.0f) yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();// this will refresh the window, so, it works the same to call RenderScene() directly
}
void DrawAxis() { //绘制x、y、z坐标轴
{ glColor3f( 0.0f, 0.0f, 1.0f );//指定线的颜色,蓝色
glBegin( GL_LINES ); { // x-axis
glVertex3f( 0.0f, 0.0f, 0.0f); glVertex3f( 200.0f, 0.0f, 0.0f);
// x-axis arrow
glVertex3f( 200.0f, 0.0f, 0.0f); glVertex3f( 193.0f, 3.0f, 0.0f); glVertex3f( 200.0f, 0.0f, 0.0f); glVertex3f( 193.0f,-3.0f, 0.0f); } glEnd();
glColor3f( 0.0f, 1.0f, 0.0f );//指定线的颜色,绿色
glBegin( GL_LINES ); { // y-axis
glVertex3f( 0.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 200.0f, 0.0f); glVertex3f( 0.0f, 200.0f, 0.0f); glVertex3f( 3.0f, 193.0f, 0.0f); glVertex3f( 0.0f, 200.0f, 0.0f); glVertex3f( -3.0f, 193.0f, 0.0f); } glEnd();
glColor3f( 1.0f, 0.0f, 0.0f );//指定线的颜色,红色
glBegin( GL_LINES ); { // z-axis
glVertex3f( 0.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, 200.0f ); glVertex3f( 0.0f, 0.0f, 200.0f ); glVertex3f( 0.0f, 3.0f, 193.0f ); glVertex3f( 0.0f, 0.0f, 200.0f ); glVertex3f( 0.0f, -3.0f, 193.0f); } glEnd(); } }
|