绘制图形与3D增强技巧(三)----三角形图元TRANGLE

1.

glBegin(GL_TRANGLES);

........

glend();

2.多边形的环绕方向:逆时针和顺时针的正反面

GLFront(GL_CCW)和GLFront(GL_CW);

3.三角形带

glBegin(GL_TRANGLE_STRIP);

.......

glend();

4.三角形扇

glBegin(GL_TRANGLE_FAN);

.......

glend();

例子:

// TRANGLE.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<GL\glut.h>
#include<math.h>
#define PI 3.14
GLfloat Range=100.0f ;
bool DEPTH_TEST=false;
bool CULL_FACE=false;
bool OUTLINE_BACK=false;
static GLfloat xRot=0.0f;
static GLfloat yRot=0.0f;
bool STOP_DEPTHTEST=false;
void Init()
{
glClearColor(0.0f,0.0f,0.0f,1.0f); glFrontFace(GL_CW); //单调着色
glShadeModel(GL_FLAT);
}
void ChangeSize(int w,int h)
{
if (h==)
{
h=;
}
glViewport(,,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w<=h)
{
glOrtho(-Range,Range,-Range*h/w,Range*h/w,-Range,Range);
}
else
{
glOrtho(-Range*w/h,Range*w/h,-Range,Range,-Range,Range);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); }
void MyMenu(int value)
{
switch (value)
{
case :
DEPTH_TEST=!DEPTH_TEST;
break;
case :
CULL_FACE=!CULL_FACE;
break;
case :
OUTLINE_BACK=!OUTLINE_BACK;
break;
case :
STOP_DEPTHTEST=true;
default:
break;
}
}
void MyspecialKey(int key,int x,int y)
{
if (key==GLUT_KEY_LEFT)
{
yRot-=5.0f;
}
if (key==GLUT_KEY_RIGHT)
{
yRot+=5.0f;
}
if (key==GLUT_KEY_UP)
{
xRot-=5.0f;
}
if (key==GLUT_KEY_DOWN)
{
xRot+=5.0f;
}
if (xRot>355.0f)
{
xRot=0.0f;
}
if (xRot<=-5.0f)
{
xRot=355.0f;
}
if (yRot>355.0f)
{
yRot=0.0f;
}
if (yRot<=-5.0f)
{
yRot=355.0f;
} glutPostRedisplay();
}
void Mydisplay()
{
GLint color=;
GLfloat angle=0.0f;
GLfloat x,y;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if (DEPTH_TEST)
{
glEnable(GL_DEPTH_TEST);
}
else
{
glDisable(GL_DEPTH_TEST);
} if (CULL_FACE)
{
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}
else
{
glDisable(GL_CULL_FACE);
} if (STOP_DEPTHTEST)
{
glDisable(GL_DEPTH_TEST);
}
if (OUTLINE_BACK)
{
glPolygonMode(GL_BACK,GL_LINE);
}
else
{
glPolygonMode(GL_BACK,GL_FILL);
} glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f); glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f,0.0f,75.0f);
for (angle = ;angle<PI*; angle+=(PI/))
{
x=50.0f*cos(angle);
y=50.0f*sin(angle);
if ((color%)==)
{
glColor3f(0.0f,1.0f,0.0f);
}
else
{
glColor3f(1.0f,0.0f,0.0f);
}
color++;
glVertex3f(x,y,0.0f);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f,0.0f,0.0f);
for (angle = ;angle<PI*; angle+=(PI/))
{
x=50.0f*cos(angle);
y=50.0f*sin(angle);
if ((color%)==)
{
glColor3f(0.0f,1.0f,0.0f);
}
else
{
glColor3f(1.0f,0.0f,0.0f);
}
color++;
glVertex3f(x,y,0.0f);
}
glEnd(); glPopMatrix();
glutSwapBuffers(); }
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("Trangle"); Init(); glutDisplayFunc(Mydisplay);
glutReshapeFunc(ChangeSize); glutCreateMenu(MyMenu);
glutAddMenuEntry("Depth Test",);
glutAddMenuEntry("CullFace",);
glutAddMenuEntry("Outline Back",);
glutAddMenuEntry("STOP DEPTHTEST",);
glutAttachMenu(GLUT_RIGHT_BUTTON); glutSpecialFunc(MyspecialKey);
glutMainLoop(); }

1.深度测试

2.隐藏面消除

3.线框模式:glpoloygonModel(GLenum face,GL_LINE/GL_FILL);

4.着色模式 glshademodel(GL_FLAT)和glshademodel(GL_SMOOTH);

上一篇:周一干货~手把手教你安装 Visual Studio 安卓模拟器


下一篇:nodejs中如何使用mysql数据库[node-mysql翻译]