AlgebraMaster

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

导航

-OPENGL-

PACKAGES MAY BE USED

 DENOISE:

https://github.com/OpenImageDenoise/oidn

 

LINUX PLATFORM

cmake_minimum_required(VERSION 2.8)
project(CP_01)


set(GLFW_HOME /home/gearslogy/Plugin_dev/opengl/GLEXT)
include_directories(${GLFW_HOME}/include/)
link_directories(${GLFW_HOME}/lib/)


find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})
if(NOT OPENGL_FOUND)
    message(ERROR " OPENGL not found!")
endif(NOT OPENGL_FOUND)

add_executable(CP_01 main.cpp)
target_link_libraries(CP_01 glfw GLEW)
View Code

 

//
// Created by gearslogy on 11/1/18.
//

#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>


enum VAO_IDs {Triangles ,NumVAOs};
enum Buffer_IDs{ArrayBuffer,NumBuffers};
enum Attrib_IDS{vPosition=0};

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];


const GLuint NumVertices = 6;

void init(void){

    static const GLfloat vertices[NumVertices][2] = {
            {-0.90f, -0.90f},
            {0.85f,  -0.90f},
            {-0.90f, 0.85f},

            {0.90f,  -0.85f},
            {0.90f,   0.90f},
            {-0.85f, 0.90f},
    };

    glCreateBuffers(NumBuffers,Buffers);
    glNamedBufferStorage(Buffers[ArrayBuffer],sizeof(vertices),vertices,0);

    glGenVertexArrays(NumVAOs,VAOs);
    glBindVertexArray(VAOs[Triangles]);
    glBindBuffer(GL_ARRAY_BUFFER,Buffers[ArrayBuffer]);
    glVertexAttribPointer(vPosition,2,GL_FLOAT,GL_FALSE,0,0);
    glEnableVertexAttribArray(vPosition);

}

void display(void){
    static const float black[] = {0.0f,0.0f,0.0f,0.0f};
    glClearBufferfv(GL_COLOR,0,black);
    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES,0 ,NumVertices);
}



int main(){
    glfwInit();

    GLFWwindow *window =glfwCreateWindow(1280,720,"Triangles",NULL,NULL);
    glfwMakeContextCurrent(window);
    glewInit();
    init();


    while(!glfwWindowShouldClose(window)){
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
}
View Code

 

WINDOWS PLATFORM,VC2019, 全部静态链接

Windows CMakelists:

cmake_minimum_required(VERSION 3.5)

project(Triangle)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# OPENGL
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})

if(NOT OPENGL_FOUND)
    message(ERROR " OPENGL not found!")
endif(NOT OPENGL_FOUND)

# GLEW
set(GLEW_HOME D:/plugin_dev/libs/glew-2.1.0)
include_directories(${GLEW_HOME}/include)
link_directories(${GLEW_HOME}/lib/Release/x64)

# GLFW
set(GLFW_HOME D:/plugin_dev/libs/glfw-3.3.1.bin.WIN64)
include_directories(${GLFW_HOME}/include/)
link_directories(${GLFW_HOME}/lib-vc2019)

add_executable(Triangle main.cpp)
target_link_libraries(Triangle glew32s glfw3 opengl32)

在win32平台一定要显式的写出opengl32链接库。原因来自:这里

 统统设置STATIC

#define GLEW_STATIC
// GLEW

#include <GL/glew.h>
#include <cstdlib>
#undef GLFW_DLL
// GLFW
#include <GLFW/glfw3.h>

 

 

 

 

2,simple vertex shader & fragment shader

trangles.vert:

#version 450 core

layout (location =0) in vec4 vPosition;
void main()
{
     gl_Position = vec4(vPosition.x, vPosition.y, vPosition.z, 1.0);
}

trangles.frag:

#version 450 core
layout (location =0) out vec4 FragColor;
void main(){

    FragColor = vec4(1,0,0,1.0);
}

loadshader.h

//
// Created by gearslogy on 11/2/18.
//

#ifndef CP_01_LOADSHADER_H
#define CP_01_LOADSHADER_H

#include <string>
#include <iostream>
#include <fstream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <vector>
#include <algorithm>

using namespace std;
class FileIO
{
public:
    static string load(const char *path){
        ifstream in;
        in.open(path,ios::in);
        if(!in.is_open())
            cout << "read " << path << " error\n";

        string temp;

        string line;
        while(getline(in,line)){
            temp += line;
            temp += "\n";
        }

        in.close();
        return temp;
    }
};


void ApplyShaders(GLuint &shaderProgram,
                    const vector<string> &codes,
                    const vector<GLenum> shaderTypes )
{
    if(codes.size()!=shaderTypes.size())
    {
        cout << "shader src size not match type size\n";
        return ;
    }

    // FOR LOG
    int success;
    char infoLog[512];


    vector <GLuint> shaders;
    for(int i=0;i<codes.size();i++){
        GLuint shader = glCreateShader(shaderTypes[i]);
        cout << "Shader ID :" << shader <<endl;
        const char *code = codes[i].c_str();
        glShaderSource(shader, 1, &code, NULL);
        glCompileShader(shader);
        // check compile error
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
            glGetShaderInfoLog(shader, 512, NULL, infoLog);
            std::cout << "ERROR::COMPILATION_FAILED\n" << infoLog << std::endl;
        }
        glAttachShader(shaderProgram, shader);
        // append shader to vector to delete later
    }


    glLinkProgram(shaderProgram);
    // check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }

    //glDeleteShader(vertexShader);
    //glDeleteShader(fragmentShader);

    glUseProgram(shaderProgram);
    for(int i=0;i<shaders.size();i++)
        glDeleteShader(shaders[i]);

}




#endif //CP_01_LOADSHADER_H
View Code

 

main.cpp:

#include <GL/glew.h>
#include <GL/gl.h>

#include <GLFW/glfw3.h>
#include <iostream>
#include "loadshader.h"
#define  BUFFER_OFFSET(offset) ((void *)(offset))


using namespace std;
enum VAO_IDs {Triangles ,NumVAOs};         // NumVAOs 1
enum Buffer_IDs{ArrayBuffer,NumBuffers};
enum Attrib_IDS{vPosition=0};

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];


const GLuint NumVertices = 6;





void init(void){

    static const GLfloat vertices[NumVertices][2] = {
            {-0.90f, -0.90f},
            {0.85f,  -0.90f},
            {-0.90f, 0.85f},

            {0.90f,  -0.85f},
            {0.90f,   0.90f},
            {-0.85f, 0.90f},
    };
    glCreateVertexArrays(NumVAOs,VAOs);//glGenVertexArrays(NumVAOs,VAOs);
    cout << "CreateVertexArrays VAOs[0]: "<<VAOs[0] << endl;

    glCreateBuffers(NumBuffers,Buffers);
    cout << "CreateVertexArrays Buffers[0]: "<<Buffers[0] << endl;

    glNamedBufferStorage(Buffers[ArrayBuffer],sizeof(vertices),vertices,0);



    
    const char *vertexShaderSource = FileIO::load("../triangles.vert").c_str();
    const char *fragmentShaderSource = FileIO::load("../triangles.frag").c_str();


    unsigned int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }


    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }


    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    // check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }


    glUseProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);




    glBindVertexArray(VAOs[Triangles]);
    glBindBuffer(GL_ARRAY_BUFFER,Buffers[ArrayBuffer]);
    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2 * sizeof(float),BUFFER_OFFSET(0));
    glEnableVertexAttribArray(0);



}

void display(void){
    static const float black[] = {0.0f,0.5f,0.0f,0.0f};
    glClearBufferfv(GL_COLOR,0,black);
    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES,0 ,NumVertices);

}



int main(){
    glfwInit();

    GLFWwindow *window =glfwCreateWindow(640,480,"Triangles",NULL,NULL);
    glfwMakeContextCurrent(window);
    glewInit();
    init();


    while(!glfwWindowShouldClose(window)){
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
}
View Code

 

replace main.cpp shader's methods then use the loadshader.h ApplyShaders:

main.cpp:

//
// Created by gearslogy on 11/1/18.
//

#include <GL/glew.h>
#include <GL/gl.h>

#include <GLFW/glfw3.h>
#include <iostream>
#include "loadshader.h"
#define  BUFFER_OFFSET(offset) ((void *)(offset))


using namespace std;
enum VAO_IDs {Triangles ,NumVAOs};         // NumVAOs 1
enum Buffer_IDs{ArrayBuffer,NumBuffers};
enum Attrib_IDS{vPosition=0};

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];


const GLuint NumVertices = 6;





void init(void){

    static const GLfloat vertices[NumVertices][2] = {
            {-0.90f, -0.90f},
            {0.85f,  -0.90f},
            {-0.90f, 0.85f},

            {0.90f,  -0.85f},
            {0.90f,   0.90f},
            {-0.85f, 0.90f},
    };
    glCreateVertexArrays(NumVAOs,VAOs);//glGenVertexArrays(NumVAOs,VAOs);
    cout << "CreateVertexArrays VAOs[0]: "<<VAOs[0] << endl;

    glCreateBuffers(NumBuffers,Buffers);
    cout << "CreateVertexArrays Buffers[0]: "<<Buffers[0] << endl;

    glNamedBufferStorage(Buffers[ArrayBuffer],sizeof(vertices),vertices,0);




    vector <string> codes;
    vector <GLenum> shaderTypes;
    codes.push_back(FileIO::load("../triangles.vert"));
    codes.push_back(FileIO::load("../triangles.frag"));
    shaderTypes.push_back(GL_VERTEX_SHADER);
    shaderTypes.push_back(GL_FRAGMENT_SHADER);

    GLuint shaderProgram = glCreateProgram();
    ApplyShaders(shaderProgram,codes,shaderTypes);



    
    glBindVertexArray(VAOs[Triangles]);
    glBindBuffer(GL_ARRAY_BUFFER,Buffers[ArrayBuffer]);
    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2 * sizeof(float),BUFFER_OFFSET(0));
    glEnableVertexAttribArray(0);



}

void display(void){
    static const float black[] = {0.0f,0.5f,0.0f,0.0f};
    glClearBufferfv(GL_COLOR,0,black);
    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES,0 ,NumVertices);

}



int main(){
    glfwInit();

    GLFWwindow *window =glfwCreateWindow(640,480,"Triangles",NULL,NULL);
    glfwMakeContextCurrent(window);
    glewInit();
    init();


    while(!glfwWindowShouldClose(window)){
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
}
View Code

 

posted on 2018-11-01 15:54  gearslogy  阅读(484)  评论(0编辑  收藏  举报