OpenGL 抗锯齿 初探

2016-12-30 18:58

http://learnopengl-cn.readthedocs.io/zh/latest/04%20Advanced%20OpenGL/11%20Anti%20Aliasing/

main.cpp 按 Q / E 进行抗锯齿切换

// 引入GLEW库 定义静态链接
#define GLEW_STATIC
#include <glew.h>
// 引入GLFW库
#include <GLFW/glfw3.h>
// 引入SOIL库
#include <SOIL/SOIL.h>
// 引入GLM库
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>

#include <iostream>
#include <vector>
#include <cstdlib>

#include "Camera.h"
#include "Shader.h"

#pragma comment(lib, "./SOIL.lib")

#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glew32s.lib")
#pragma comment (lib, "glfw3.lib") 
#pragma comment (lib, "glfw3dll.lib") 
#pragma comment (lib, "glew32mxs.lib")
#pragma comment (lib, "assimp.lib")


void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xPos, double yPos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void Do_Movement();

GLint WIDTH = 800, HEIGHT = 600;

Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
bool bKeys[1024];

GLfloat dDeltaTime = 0.0f;
GLfloat dLastFrame = 0.0f;
GLfloat dLastX = 500, dLastY = 300;

GLboolean  bFirstMouse = GL_TRUE;


int main(int argc, char** argv)
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // 需要创建一个支持多采样的缓冲区(multisample buffer),一般由窗口系统生成
    // 这里使用GLFW的windowHint来让GLFW库生成这个多采样缓冲区
    glfwWindowHint(GLFW_SAMPLES, 4);

    GLFWwindow* pWnd = glfwCreateWindow(WIDTH, HEIGHT, "OGL Anti Aliasing", nullptr, nullptr);
    glfwMakeContextCurrent(pWnd);

    glfwSetKeyCallback(pWnd, key_callback);
    glfwSetCursorPosCallback(pWnd, mouse_callback);
    glfwSetScrollCallback(pWnd, scroll_callback);

    glewExperimental = GL_TRUE;
    glewInit();

    glViewport(0, 0, WIDTH, HEIGHT);

    // 开启OpenGL多采样
    glEnable(GL_MULTISAMPLE);

    glEnable(GL_DEPTH_TEST);

    GLfloat cubeVertices[] = {      // 坐标点
        -0.5f, -0.5f, -0.5f,        0.5f, -0.5f, -0.5f,     0.5f,  0.5f, -0.5f,
        0.5f,  0.5f, -0.5f,     -0.5f,  0.5f, -0.5f,        -0.5f, -0.5f, -0.5f,

        -0.5f, -0.5f,  0.5f,        0.5f, -0.5f,  0.5f,     0.5f,  0.5f,  0.5f,
        0.5f,  0.5f,  0.5f,     -0.5f,  0.5f,  0.5f,        -0.5f, -0.5f,  0.5f,

        -0.5f,  0.5f,  0.5f,        -0.5f,  0.5f, -0.5f,        -0.5f, -0.5f, -0.5f,
        -0.5f, -0.5f, -0.5f,        -0.5f, -0.5f,  0.5f,        -0.5f,  0.5f,  0.5f,

        0.5f,  0.5f,  0.5f,     0.5f,  0.5f, -0.5f,     0.5f, -0.5f, -0.5f,
        0.5f, -0.5f, -0.5f,     0.5f, -0.5f,  0.5f,     0.5f,  0.5f,  0.5f,

        -0.5f, -0.5f, -0.5f,        0.5f, -0.5f, -0.5f,     0.5f, -0.5f,  0.5f,
        0.5f, -0.5f,  0.5f,     -0.5f, -0.5f,  0.5f,        -0.5f, -0.5f, -0.5f,

        -0.5f,  0.5f, -0.5f,        0.5f,  0.5f, -0.5f,     0.5f,  0.5f,  0.5f,
        0.5f,  0.5f,  0.5f,     -0.5f,  0.5f,  0.5f,        -0.5f,  0.5f, -0.5f
    };

    GLuint cubeVAO, cubeVBO;
    glGenVertexArrays(1, &cubeVAO);
    glBindVertexArray(cubeVAO);
    {
        glGenBuffers(1, &cubeVBO);
        glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
        {
            glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (GLvoid*)0);
            glEnableVertexAttribArray(0);
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    glBindVertexArray(0);

    GLfloat nCurrentFrame = glfwGetTime();
    glClearColor(0.1f, 0.2f, 0.23f, 1.0f);

    Shader shader("./shader/vertices", "./shader/fragement");

    while (!glfwWindowShouldClose(pWnd))
    {
        nCurrentFrame = glfwGetTime();
        dDeltaTime = nCurrentFrame - dLastFrame;
        dLastFrame = nCurrentFrame;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glfwPollEvents();
        Do_Movement();


        shader.useShaderPrograme();

        glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 1000.0f);
        glUniformMatrix4fv(glGetUniformLocation(shader.getPrograme(), "projection"), 1,
            GL_FALSE, glm::value_ptr(projection));

        glUniformMatrix4fv(glGetUniformLocation(shader.getPrograme(), "view"), 1,
            GL_FALSE, glm::value_ptr(camera.GetViewMatrix()));

        glUniformMatrix4fv(glGetUniformLocation(shader.getPrograme(), "model"), 1,
            GL_FALSE, glm::value_ptr(glm::mat4()));

        glBindVertexArray(cubeVAO);
        {
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }
        glBindVertexArray(0);

        glfwSwapBuffers(pWnd);
    }

    glfwTerminate();

    return 0;
}



void Do_Movement()
{
    if (bKeys[GLFW_KEY_W])
        camera.ProcessKeyboard(FORWARD, dDeltaTime);
    if (bKeys[GLFW_KEY_S])
        camera.ProcessKeyboard(BACKWARD, dDeltaTime);
    if (bKeys[GLFW_KEY_A])
        camera.ProcessKeyboard(LEFT, dDeltaTime);
    if (bKeys[GLFW_KEY_D])
        camera.ProcessKeyboard(RIGHT, dDeltaTime);
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);

    if (key == GLFW_KEY_Q && action == GLFW_PRESS)
    {
        // 开启多重采样
        glEnable(GL_MULTISAMPLE);
    }
    else if (key == GLFW_KEY_E && action == GLFW_PRESS)
    {
        // 关闭多重采样
        glDisable(GL_MULTISAMPLE);
    }

    if (action == GLFW_PRESS)
        bKeys[key] = true;
    else if (action == GLFW_RELEASE)
        bKeys[key] = false;
}

void mouse_callback(GLFWwindow* window, double xPos, double yPos)
{
    if (bFirstMouse)
    {
        dLastX = xPos;
        dLastY = yPos;
        bFirstMouse = false;
    }

    GLfloat xoffset = xPos - dLastX;
    GLfloat yoffset = dLastY - yPos;

    dLastX = xPos;
    dLastY = yPos;

    camera.ProcessMouseMovement(xoffset, yoffset);
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
    camera.ProcessMouseScroll(yoffset);
}

camera.h shader.h 参考之前文章

GLSL Shader部分

vertices

#version 330 core

uniform mat4 model;  
uniform mat4 view;  
uniform mat4 projection;

layout (location = 0)  in vec3 pos;

void main()
{
        gl_Position = projection * view * model * vec4(pos, 1.0f);  
}

fragement

#version 330 core

out vec4 color;

void main()
{
    color = vec4(1.0f, 0.8f, 0.5f, 1.0f);
}

未抗锯齿状态

这里写图片描述

抗锯齿状态
这里写图片描述