AlgebraMaster

Modern C++ 创造非凡 . 改变世界

导航

Make a simple opengl app

受不了main的各种callback,先设计第一版Application:

#define GLEW_STATIC
// GLEW
#include <GL/glew.h>
#include <chrono>
#undef GLFW_DLL
// GLFW
#include <GLFW/glfw3.h>
#include "AM_Utils.h"
#include "AM_ErrorCheck.h"
#include "AM_GeoPtrs.h"
#include "AM_NamespaceDef.h"
#include "AM_GameCamera.h"
#include "AM_LoadShader.h"
#include "AM_AppInterface.h"


using namespace std;
USE_ALGEBRAMSTER_NAMESPACE

const int width = 800;
const int height = 800;

class App:public AM_AppInterface{
public:
    App(int w, int h, const char*title):AM_AppInterface(w,h,title)
    {
    }
    void init() override{
        cout << ">>initialize geometry\n";

        // Triangle
        float vertices[] = {
                // positions          // colors
                -0.5f,  -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,
                0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,
                0.0f, 0.5f, 0.0f,   0.0f, 0.0f, 1.0f,

        };
        geoPtr->setupVertexBufferData(vertices, sizeof(vertices));
        geoPtr->addAttrib<float>(3, 6, 0);  // P     at  location 0
        geoPtr->addAttrib<float>(3, 6, 3);  // Cd    at  location 1
        geoPtr->unBind();
        
        glPatchParameteri(GL_PATCH_VERTICES,3);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        
        cout << ">>initialize shaders\n";
        shaderPtr->loadVertexShader("shaders/tessellation/CP_01/vert.glsl");
        shaderPtr->loadFragmentShader("shaders/tessellation/CP_01/frag.glsl");
        shaderPtr->loadTessellationControlShader("shaders/tessellation/CP_01/tcs.glsl");
        shaderPtr->loadTessellationEvaluationShader("shaders/tessellation/CP_01/tes.glsl");
        shaderPtr->link();
        shaderPtr->use();

    }
    void onKey(int key) override{
        camPtr->onKey(key, this->floatMs);
    }

    void render() override{
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0,0,0,1);
        glm::mat4 proj = AM_GameCamera::getProjMatrix(width, height, 45, 0.1, 10000.0f);
        glm::mat4 view = camPtr->getViewMatrix();
        // Render content
        shaderPtr->use();
        shaderPtr->setMat4("view", view);
        shaderPtr->setMat4("proj", proj);
        geoPtr->bind();
        GL_CALL(glDrawArrays(GL_PATCHES,0,3));
    }

private:
    GL_GeoPtr geoPtr = MakeGeo();
    LoadShaderPtr shaderPtr = MakeShader();
    gameCameraPtr camPtr = MakeCamera();

};

int main(){
    auto app = std::make_shared<App> (width,height,"HelloGL");
    app->renderLoop();
    return 0;
}

瞬间清爽。下面将参考更多的文章修改app的表面。

 

posted on 2020-06-30 11:14  gearslogy  阅读(283)  评论(0编辑  收藏  举报