OpenGL 抗锯齿处理

#define SMALL_STARS     100
#define MEDIUM_STARS     40
#define LARGE_STARS      15

#define SCREEN_X        800
#define SCREEN_Y        600

#include "GLTools.h"
#include "GLShaderManager.h"
#include "GLMatrixStack.h"
#include "GLBatch.h"
#include "GLFrustum.h"
#include 
#ifdef __APPLE__
#include 
#else
#include 
#endif

static GLBatch smallStarBatch;/// 小星星
///中星星
static GLBatch mediumStarBath;

static GLBatch largeStarBath;

static GLBatch moonbatch;
static GLBatch mountationBatch;

static GLShaderManager shaderManager;

static GLFrustum viewFrustum;
static void SpecialFunckeys(int key, int x, int y){
    
    glutPostRedisplay();
}

static void ProcessMenu(int value) {
    switch (value) {
        case 1:
            glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glEnable(GL_BLEND);
            glEnable(GL_POINT_SMOOTH);
            glEnable(GL_LINE_SMOOTH);
            glEnable(GL_POLYGON_SMOOTH);
            break;
        case 2:
            glDisable(GL_BLEND);
            glDisable(GL_POINT_SMOOTH);
            glDisable(GL_LINE_SMOOTH);
            glDisable(GL_POLYGON_SMOOTH);
            break;

        default:
            break;
    }
    glutPostRedisplay();
}

static void Render() {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    glutSetWindowTitle("refresh");
    GLfloat yellow[] = {1.0,1.0,1.0,1.0};
    shaderManager.UseStockShader(GLT_SHADER_FLAT,viewFrustum.GetProjectionMatrix(),yellow);
    /// 关闭多重采样
    glDisable(GL_MULTISAMPLE);
    // 指定混合因子
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //开始混合处理
    glEnable(GL_BLEND);
    glEnable(GL_POINT_SMOOTH);
    glEnable(GL_LINE_SMOOTH);
     
    /// 绘制图形
    glPointSize(1.0f);
    smallStarBatch.Draw();
    
    glPointSize(4.0f);
    mediumStarBath.Draw();
    
    glPointSize(10.f);
    largeStarBath.Draw();
    
    glLineWidth(4);
    mountationBatch.Draw();
    
    /// 关闭对点/线混合抗锯齿
    glDisable(GL_POINT_SMOOTH);
    glDisable(GL_LINE_SMOOTH);
    
    /// 针对多边形开启多重采样
    glEnable(GL_MULTISAMPLE);
    moonbatch.Draw();
    glDisable(GL_MULTISAMPLE);
    
    glutSwapBuffers();
    /// 混合处理仅针对线 和点
    /// 多重采样针对点是多边形
    /// 因此 对于同时存在点线和多边形的 两种组合使用 来处理
    
//    /// 开始混合处理
//    glEnable(GL_BLEND);
//    /// 指定混合因子
//    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
//    /// 指定混合方程式
//    glBlendEquation(GL_FUNC_ADD);
//    ///对点进行抗锯齿处理
//    glEnable(GL_POINT_SMOOTH);
//    ///对线进行抗锯齿处理
//    glEnable(GL_LINE_SMOOTH);
//    ///对多边形进行抗锯齿处理
//    glEnable(GL_POLYGON_SMOOTH);
    
  //  glutSwapBuffers();
}
static void changeSize(int width, int height) {
    if (height == 0) {
        height = 1;
    }
    glViewport(0, 0,width,height);
    viewFrustum.SetOrthographic(0.0f, SCREEN_X, 0.0f, SCREEN_Y, -1.0f, 1.0f);
}
static void SetupRC() {
    /// 数据创建
    M3DVector3f vVerts[SMALL_STARS];
    int i = 0;
    shaderManager.InitializeStockShaders();
    
    smallStarBatch.Begin(GL_POINTS,SMALL_STARS);
    for (i = 0;i < SMALL_STARS;i++) {
        vVerts[i][0] = (GLfloat)(rand() % SCREEN_X);
        vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100;;
        vVerts[i][2] = 0.0f;
    }
    smallStarBatch.CopyVertexData3f(vVerts);
    smallStarBatch.End();
    
    mediumStarBath.Begin(GL_POINTS, MEDIUM_STARS);
    for (i = 0;i < MEDIUM_STARS;i++) {
        vVerts[i][0] = (GLfloat)(rand() % SCREEN_X);
        vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100;;
        vVerts[i][2] = 0.0f;
    }
    mediumStarBath.CopyVertexData3f(vVerts);
    mediumStarBath.End();

    largeStarBath.Begin(GL_POINTS, LARGE_STARS);
    for (i = 0;i < MEDIUM_STARS;i++) {
        vVerts[i][0] = (GLfloat)(rand() % SCREEN_X);
        vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100;;
        vVerts[i][2] = 0.0f;
    }
    largeStarBath.CopyVertexData3f(vVerts);
    largeStarBath.End();

    M3DVector3f vMountains[12] = { 0.0f, 25.0f, 0.0f,
        50.0f, 100.0f, 0.0f,
        100.0f, 25.0f, 0.0f,
        225.0f, 125.0f, 0.0f,
        300.0f, 50.0f, 0.0f,
        375.0f, 100.0f, 0.0f,
        460.0f, 25.0f, 0.0f,
        525.0f, 100.0f, 0.0f,
        600.0f, 20.0f, 0.0f,
        675.0f, 70.0f, 0.0f,
        750.0f, 25.0f, 0.0f,
        800.0f, 90.0f, 0.0f };
    mountationBatch.Begin(GL_LINE_STRIP, 12);
    mountationBatch.CopyVertexData3f(vMountains);
    mountationBatch.End();
    
    GLfloat x = 700.0f;
    GLfloat y = 500.0f;
    GLfloat r = 50.0f;
    GLfloat angle = 0.0f;
    
    moonbatch.Begin(GL_TRIANGLE_FAN, 34);
    int nVerts = 0;
    vVerts[nVerts][0] = x;
    vVerts[nVerts][1] = y;
    vVerts[nVerts][2] = 0.0f;
    for(angle = 0; angle < 2.0f * 3.141592f; angle += 0.2f) {
        nVerts++;
        vVerts[nVerts][0] = x + float(cos(angle)) * r;
        vVerts[nVerts][1] = y + float(sin(angle)) * r;
        vVerts[nVerts][2] = 0.0f;
    }
    nVerts++;
    
    vVerts[nVerts][0] = x + r;;
    vVerts[nVerts][1] = y;
    vVerts[nVerts][2] = 0.0f;
    moonbatch.CopyVertexData3f(vVerts);
    moonbatch.End();
    
    /// 背景颜色
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

}
int main(int argc,char *argv[]) {
    gltSetWorkingDirectory(argv[0]);
    ///GLUT_MULTISAMPLE 多重采样
    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    glutInit(&argc, argv);
    glutInitWindowSize(800, 600);
    glutCreateWindow("new");
    GLenum en = glewInit();
    if (GLEW_OK != en)
    {
        fprintf(stderr, "Error: %s\n", glewGetErrorString(en));
        return 1;
    }
    
    glutCreateMenu(ProcessMenu);
    glutAddMenuEntry("Antialiased Rendering",1);
    glutAddMenuEntry("Normal Rendering",2);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    
    glutSpecialFunc(SpecialFunckeys);
    glutDisplayFunc(Render);
    glutReshapeFunc(changeSize);
    
    SetupRC();
    glutMainLoop();
    
}

你可能感兴趣的:(图形渲染)