码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-23 21:02:26      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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==0)
    {
        h=1;
    }
    glViewport(0,0,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 1:
        DEPTH_TEST=!DEPTH_TEST;
        break;
    case 2:
        CULL_FACE=!CULL_FACE;
        break;
    case 3:
        OUTLINE_BACK=!OUTLINE_BACK;
        break;
    case 4:
        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=1;
    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 = 0;angle<PI*2; angle+=(PI/8))
    {
        x=50.0f*cos(angle);
        y=50.0f*sin(angle);
        if ((color%2)==0)
        {
            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 = 0;angle<PI*2; angle+=(PI/8))
    {
        x=50.0f*cos(angle);
        y=50.0f*sin(angle);
        if ((color%2)==0)
        {
            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(500,300);
    glutInitWindowSize(600,600);
    glutCreateWindow("Trangle");

    Init();

    glutDisplayFunc(Mydisplay);
    glutReshapeFunc(ChangeSize);
    

    glutCreateMenu(MyMenu);
    glutAddMenuEntry("Depth Test",1);
    glutAddMenuEntry("CullFace",2);
    glutAddMenuEntry("Outline Back",3);
    glutAddMenuEntry("STOP DEPTHTEST",4);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glutSpecialFunc(MyspecialKey);
    glutMainLoop();


}

1.深度测试

2.隐藏面消除

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

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

 

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

标签:

原文地址:http://www.cnblogs.com/YTYMblog/p/5699467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!