Added rendering

This commit is contained in:
Robert 2020-09-02 12:15:29 +02:00
parent fc8a1b2d35
commit 2fae908a55
5 changed files with 156 additions and 5 deletions

View file

@ -20,6 +20,6 @@ target_link_libraries(ComplexPlotting
glfw
)
#add_custom_command(TARGET ComplexPlotting POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/shaders $<TARGET_FILE_DIR:ComplexPlotting>/shaders
#)
add_custom_command(TARGET ComplexPlotting POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/shader $<TARGET_FILE_DIR:ComplexPlotting>/shaders
)

View file

@ -2,9 +2,21 @@
#include <iostream>
#include <string>
#include <fstream>
PlotWindow::PlotWindow(int w, int h, int id, std::string title) :
window(nullptr), id(id)
window(nullptr), id(id),
vertices{
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
},
indices{
0, 1, 3,
1, 2, 3
},
vert(0), frag(0), shader(0), VAO(0), VBO(0), EBO(0)
{
window = glfwCreateWindow(w, h, ("Plot " + std::to_string(id) + " | " + title).c_str(), NULL, NULL);
if (window == nullptr)
@ -27,10 +39,95 @@ PlotWindow::PlotWindow(int w, int h, int id, std::string title) :
}
glViewport(0, 0, w, h);
int success;
char errorBuf[512];
// Load and compile Vertex Shader
vert = glCreateShader(GL_VERTEX_SHADER);
char* shaderSource = nullptr;
LoadShaderSourceCode("shaders/basic.vert", &shaderSource);
if (shaderSource == nullptr)
shaderSource = "";
glShaderSource(vert, 1, &shaderSource, NULL);
glCompileShader(vert);
glGetShaderiv(vert, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vert, 512, NULL, errorBuf);
std::cerr << "Vertex shader compilation error" << std::endl << errorBuf << std::endl;
glfwSetWindowShouldClose(window, 1);
return;
}
// load and compile Fragment Shader
frag = glCreateShader(GL_FRAGMENT_SHADER);
shaderSource = nullptr;
LoadShaderSourceCode("shaders/basic.frag", &shaderSource);
if (shaderSource == nullptr)
shaderSource = "";
glShaderSource(frag, 1, &shaderSource, NULL);
glCompileShader(frag);
glGetShaderiv(frag, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(frag, 512, NULL, errorBuf);
std::cerr << "Fragment shader compilation error" << std::endl << errorBuf << std::endl;
glfwSetWindowShouldClose(window, 1);
return;
}
// Link shaders into shader program
shader = glCreateProgram();
glAttachShader(shader, vert);
glAttachShader(shader, frag);
glLinkProgram(shader);
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shader, 512, NULL, errorBuf);
std::cerr << "Shader program linking error" << std::endl << errorBuf << std::endl;
glfwSetWindowShouldClose(window, 1);
return;
}
glDeleteShader(frag);
glDeleteShader(vert);
// Generate buffers
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
// Create VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Create EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void PlotWindow::Destroy()
{
glfwMakeContextCurrent(window);
glDeleteBuffers(1, &EBO);
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(shader);
glfwDestroyWindow(window);
}
@ -41,16 +138,42 @@ void PlotWindow::GiveContext()
void PlotWindow::Clear()
{
glClearColor(0.6f, 0.1f, 0.4f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void PlotWindow::Display()
{
glUseProgram(shader);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
}
void PlotWindow::FramebufferSizeCallback(GLFWwindow* window, int w, int h)
{
glfwMakeContextCurrent(window);
glViewport(0, 0, w, h);
}
void PlotWindow::LoadShaderSourceCode(const char* path, char** buffer)
{
std::ifstream file(path);
if (!file.good())
{
std::cerr << "File not found: " << path << std::endl;
*buffer = nullptr;
return;
}
std::string code((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
size_t len = code.size();
*buffer = (char*)malloc(len + sizeof(char));
if (*buffer == nullptr)
return;
memcpy_s(*buffer, len, code.c_str(), len);
(*buffer)[len] = '\0';
}

View file

@ -7,6 +7,10 @@
#pragma once
typedef unsigned int BufferObject;
typedef unsigned int Shader;
typedef unsigned int ShaderProgram;
class PlotWindow
{
public:
@ -24,4 +28,12 @@ private:
GLFWwindow* window;
static void FramebufferSizeCallback(GLFWwindow* window, int w, int h);
static void LoadShaderSourceCode(const char* path, char** buffer);
Shader vert, frag;
ShaderProgram shader;
BufferObject VAO, VBO, EBO;
float vertices[4 * 3];
unsigned int indices[2 * 3];
};

8
src/shader/basic.frag Normal file
View file

@ -0,0 +1,8 @@
#version 460 core
out vec4 oColor;
void main()
{
oColor = vec4(0.6f, 0.2f, 0.9f, 1.0f);
}

8
src/shader/basic.vert Normal file
View file

@ -0,0 +1,8 @@
#version 460 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}