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)
//
// 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=}; GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers]; const GLuint NumVertices = ; void init(void){ static const GLfloat vertices[NumVertices][] = {
{-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,); glGenVertexArrays(NumVAOs,VAOs);
glBindVertexArray(VAOs[Triangles]);
glBindBuffer(GL_ARRAY_BUFFER,Buffers[ArrayBuffer]);
glVertexAttribPointer(vPosition,,GL_FLOAT,GL_FALSE,,);
glEnableVertexAttribArray(vPosition); } void display(void){
static const float black[] = {0.0f,0.0f,0.0f,0.0f};
glClearBufferfv(GL_COLOR,,black);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, ,NumVertices);
} int main(){
glfwInit(); GLFWwindow *window =glfwCreateWindow(,,"Triangles",NULL,NULL);
glfwMakeContextCurrent(window);
glewInit();
init(); while(!glfwWindowShouldClose(window)){
display();
glfwSwapBuffers(window);
glfwPollEvents();
} glfwDestroyWindow(window);
glfwTerminate();
}

WINDOWS PLATFORM,VC2019, 全部静态链接

Windows CMakelists:

cmake_minimum_required(VERSION 3.5)

project(Triangle)

set(CMAKE_CXX_STANDARD )
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.)
include_directories(${GLEW_HOME}/include)
link_directories(${GLEW_HOME}/lib/Release/x64) # GLFW
set(GLFW_HOME D:/plugin_dev/libs/glfw-3.3..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  core

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

trangles.frag:

#version  core
layout (location =) out vec4 FragColor;
void main(){ FragColor = vec4(,,,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[]; vector <GLuint> shaders;
for(int i=;i<codes.size();i++){
GLuint shader = glCreateShader(shaderTypes[i]);
cout << "Shader ID :" << shader <<endl;
const char *code = codes[i].c_str();
glShaderSource(shader, , &code, NULL);
glCompileShader(shader);
// check compile error
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, , 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, , NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
} //glDeleteShader(vertexShader);
//glDeleteShader(fragmentShader); glUseProgram(shaderProgram);
for(int i=;i<shaders.size();i++)
glDeleteShader(shaders[i]); } #endif //CP_01_LOADSHADER_H

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=}; GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers]; const GLuint NumVertices = ; void init(void){ static const GLfloat vertices[NumVertices][] = {
{-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[] << endl; glCreateBuffers(NumBuffers,Buffers);
cout << "CreateVertexArrays Buffers[0]: "<<Buffers[] << endl; glNamedBufferStorage(Buffers[ArrayBuffer],sizeof(vertices),vertices,); 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, , &vertexShaderSource, NULL);
glCompileShader(vertexShader); int success;
char infoLog[];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, , NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
} unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, , &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, , 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, , 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(,,GL_FLOAT,GL_FALSE, * sizeof(float),BUFFER_OFFSET());
glEnableVertexAttribArray(); } void display(void){
static const float black[] = {0.0f,0.5f,0.0f,0.0f};
glClearBufferfv(GL_COLOR,,black);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, ,NumVertices); } int main(){
glfwInit(); GLFWwindow *window =glfwCreateWindow(,,"Triangles",NULL,NULL);
glfwMakeContextCurrent(window);
glewInit();
init(); while(!glfwWindowShouldClose(window)){
display();
glfwSwapBuffers(window);
glfwPollEvents();
} glfwDestroyWindow(window);
glfwTerminate();
}

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=}; GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers]; const GLuint NumVertices = ; void init(void){ static const GLfloat vertices[NumVertices][] = {
{-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[] << endl; glCreateBuffers(NumBuffers,Buffers);
cout << "CreateVertexArrays Buffers[0]: "<<Buffers[] << endl; glNamedBufferStorage(Buffers[ArrayBuffer],sizeof(vertices),vertices,); 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(,,GL_FLOAT,GL_FALSE, * sizeof(float),BUFFER_OFFSET());
glEnableVertexAttribArray(); } void display(void){
static const float black[] = {0.0f,0.5f,0.0f,0.0f};
glClearBufferfv(GL_COLOR,,black);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, ,NumVertices); } int main(){
glfwInit(); GLFWwindow *window =glfwCreateWindow(,,"Triangles",NULL,NULL);
glfwMakeContextCurrent(window);
glewInit();
init(); while(!glfwWindowShouldClose(window)){
display();
glfwSwapBuffers(window);
glfwPollEvents();
} glfwDestroyWindow(window);
glfwTerminate();
}

openGL learning的更多相关文章

  1. Github上的1000多本免费电子书重磅来袭!

    Github上的1000多本免费电子书重磅来袭!   以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能 ...

  2. Github 的一个免费编程书籍列表

    Index Ada Agda Alef Android APL Arduino ASP.NET MVC Assembly Language Non-X86 AutoHotkey Autotools A ...

  3. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  4. 1.openGL 初探

    GLEW, GLFW和GLM介绍 现在你有了工程,就让我们开始介绍下工程所用到的开源库和为啥需要这些. The OpenGL Extension Wrangler (GLEW)是用来访问OpenGL ...

  5. openGL实现二维图形和三维图形

    openGL是一个强大的底层图形库,其命令最初的时候使用C语言实现的.openGL定义了一个图形程序接口,常用于制作处理三维图像,功能强大,调用方便,在图像处理十分受欢迎. 实现图形主要使用的是ope ...

  6. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  7. OpenGL 开始学习指南

    近期需要做一个涌潮的预报与仿真模拟,为了使模型更具有真实感,且逼真,使用起来更灵活.感觉还是得从基础的OpenGL学习.鉴于Direct3D技术存在的众多不确定性,且评论不太好的原因,决定用OpenG ...

  8. OpenGL ES应用开发实践指南:iOS卷

    <OpenGL ES应用开发实践指南:iOS卷> 基本信息 原书名:Learning OpenGL ES for iOS:A Hands-On Guide to Modern 3D Gra ...

  9. 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】

    转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...

随机推荐

  1. 三十九、vue中element最原始的分页(未封装的)

    html<el-table ref="scoreUserTable" :data="scorePageUser.slice((currentPage1-1)*pag ...

  2. Vue 路由心得总结

    一. 嵌套路由 a.主页面,  main.vue ,   子页面分别为  shouye.vue   /    liuyan.vue   / about.vue   , 首先, 在main.vue加入导 ...

  3. STL—之迭代器,文中推荐的博客很给力

    在此,不再赘述迭代器本身的意义和作用,本质上,迭代器就是一种泛型指针.但是不同的容器对应的迭代器是不同的,我们使用迭代器的时候,需要认识到: 知道迭代器的类型是一件很重要的事情. 在此,给出一个描述迭 ...

  4. powershell 常用命令之取磁盘分区信息

    //查看mac 地址 PS C:\Users\yyy> get-wmiobject -class Win32_NetworkAdapterConfiguration -namespace &qu ...

  5. sql 语句中as的用法和作用

    我们的Sql语句在很多数据库中都是通用的,比如像Mysql数据库 Access数据库. Oracle数据库.  Sqlite数据库 .甚至在我们的Excel中也可以使用Sql语句. 在我的数据库中有u ...

  6. Android 1.7 中不支持 lambda 表达式

    Error:(129, 32) 错误: -source 1.7 中不支持 lambda 表达式 (请使用 -source 8 或更高版本以启用 lambda 表达式) lambda expressio ...

  7. tensorflow-TFRecord报错ValueError: Protocol message Feature has no "feature" field.

    编写代码用TFRecord数据结构存储数据集信息是报错:ValueError: Protocol message Feature has no "feature" field.或和 ...

  8. 《AutoCAD Civil 3D .NET二次开发》勘误2

    4.6.3节中代码: 原代码: 06 pdo.Keywords.Add("Pi", "Pi", "派<Pi>"); 07 pdo ...

  9. docker容器运行后退出,怎么才能一直运行?【转】

    现象 启动docker容器 docker run –name [CONTAINER_NAME] [CONTAINER_ID] 查看容器运行状态 docker ps -a 发现刚刚启动的mydocker ...

  10. [oracle]解决ora-01034 oracle not available

    一般都是数据库连接中断了,按照链接重连即可. https://jingyan.baidu.com/article/5552ef47c73eef518ffbc908.html