天天看点

OpenGL 抗锯齿 初探

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

http://blog.csdn.net/wangdingqiaoit/article/details/52830310

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 = , HEIGHT = ;

Camera camera(glm::vec3(, , ));
bool bKeys[];

GLfloat dDeltaTime = ;
GLfloat dLastFrame = ;
GLfloat dLastX = , dLastY = ;

GLboolean  bFirstMouse = GL_TRUE;


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

    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

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

    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(, , WIDTH, HEIGHT);

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

    glEnable(GL_DEPTH_TEST);

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

        -, -,  ,        , -,  ,     ,  ,  ,
        ,  ,  ,     -,  ,  ,        -, -,  ,

        -,  ,  ,        -,  , -,        -, -, -,
        -, -, -,        -, -,  ,        -,  ,  ,

        ,  ,  ,     ,  , -,     , -, -,
        , -, -,     , -,  ,     ,  ,  ,

        -, -, -,        , -, -,     , -,  ,
        , -,  ,     -, -,  ,        -, -, -,

        -,  , -,        ,  , -,     ,  ,  ,
        ,  ,  ,     -,  ,  ,        -,  , -
    };

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

    GLfloat nCurrentFrame = glfwGetTime();
    glClearColor(, , , );

    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, , );
        glUniformMatrix4fv(glGetUniformLocation(shader.getPrograme(), "projection"), ,
            GL_FALSE, glm::value_ptr(projection));

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

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

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

        glfwSwapBuffers(pWnd);
    }

    glfwTerminate();

    return ;
}



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 = )  in vec3 pos;

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

fragement

#version 330 core

out vec4 color;

void main()
{
    color = vec4(, , , );
}
           

未抗锯齿状态

OpenGL 抗锯齿 初探

抗锯齿状态

OpenGL 抗锯齿 初探

继续阅读