Initialize GLFW

This commit is contained in:
Robert 2020-08-31 17:55:45 +02:00
parent e3febb936e
commit fcadf735c9
10 changed files with 10 additions and 318 deletions

View file

@ -1,7 +1,7 @@
add_executable(ComplexPlotting
"main.cpp"
"ShaderProgram.hpp")
"PlotWindow.hpp")
file(GLOB vendor_SRC
${CMAKE_SOURCE_DIR}/vendor/src/*.c
@ -21,6 +21,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/shaders $<TARGET_FILE_DIR:ComplexPlotting>/shaders
#)

0
src/PlotWindow.hpp Normal file
View file

View file

@ -1,95 +0,0 @@
#pragma once
#include <glad/glad.h>
#include <fstream>
#include <streambuf>
class ShaderProgram
{
public:
ShaderProgram() = default;
~ShaderProgram()
{
glDeleteShader(fragmentShader_id);
glDeleteShader(vertexShader_id);
}
std::string VertexShader, FragmentShader;
int Build(char** error)
{
int success;
// Vertex Shader initialization
if (VertexShader != "")
{
vertexShader_id = glCreateShader(GL_VERTEX_SHADER);
char* source = new char;
LoadFile(VertexShader, &source);
glShaderSource(vertexShader_id, 1, &source, NULL);
glCompileShader(vertexShader_id);
glGetShaderiv(vertexShader_id, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader_id, 512, NULL, *error);
return success;
}
}
// Fragment Shader initialization
if (FragmentShader != "")
{
fragmentShader_id = glCreateShader(GL_FRAGMENT_SHADER);
char* source = new char;
LoadFile(FragmentShader, &source);
glShaderSource(fragmentShader_id, 1, &source, NULL);
glCompileShader(fragmentShader_id);
glGetShaderiv(fragmentShader_id, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader_id, 512, NULL, *error);
return success;
}
}
// Link shaders
program_id = glCreateProgram();
glAttachShader(program_id, vertexShader_id);
glAttachShader(program_id, fragmentShader_id);
glLinkProgram(program_id);
glGetProgramiv(program_id, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(program_id, 512, NULL, *error);
return success;
}
return 1;
}
void Use()
{
glUseProgram(program_id);
}
unsigned int vertexShader_id, fragmentShader_id, program_id;
private:
void LoadFile(std::string path, char** source)
{
std::ifstream file(path);
std::string shader(
(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()
);
size_t s = shader.size();
*source = (char*)realloc(*source, s + sizeof(char));
memcpy_s(*source, s, shader.c_str(), s);
(*source)[s] = '\0';
}
};

View file

@ -1,97 +1,13 @@
#include <glad/glad.h>
#include <glfw/glfw3.h>
#include <iostream>
#include "ShaderProgram.hpp"
#include "Triangle.hpp"
#include "Quad.hpp"
void LogGLFWerror()
{
const char* error = new const char;
glfwGetError(&error);
std::cerr << error << std::endl;
}
void FramebufferSizeCallback(GLFWwindow* window, int w, int h)
{
glViewport(0, 0, w, h);
}
#include <glfw/glfw3.h>
int main(int argc, char** argv)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(800, 800, "Complex Plot", NULL, NULL);
if (window == nullptr)
int result = glfwInit();
if (result != GL_TRUE)
{
LogGLFWerror();
return -1;
const char* buffer = new char[512];
glfwGetError(&buffer);
std::cout << "Failed to initialize GLFW: " << std::endl << buffer << std::endl;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cerr << "GLAD failed to initialize" << std::endl;
return -1;
}
glViewport(0, 0, 800, 800);
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
// Create Shader Program
ShaderProgram program;
program.VertexShader = "shaders/triangle.vert";
program.FragmentShader = "shaders/triangle.frag";
char* error = new char;
if (!program.Build(&error))
{
std::cout << error << std::endl;
return -1;
}
// Make triangle
Triangle t(
{ -0.5f, -0.5f, 0.f },
{ 0.5f, -0.5f, 0.f },
{ 0.f, 0.5, 0.f }
);
Quad q(
{ -0.5f, 0.5f, 0.0f },
{ 0.5f, 0.5f, 0.0f },
{ 0.5f, -0.5f, 0.0f },
{ -0.5f, -0.5f, 0.0f }
);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
float time = glfwGetTime();
int timeUniformLocation = glGetUniformLocation(program.program_id, "t");
glClearColor(0.4f, 0.1f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
program.Use();
glUniform1f(timeUniformLocation, time);
q.Draw();
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

View file

@ -1,18 +0,0 @@
#pragma once
#include <glad/glad.h>
class IDrawable
{
public:
virtual void Draw() = 0;
protected:
IDrawable()
{
glCreateVertexArrays(1, &VAO);
glCreateBuffers(1, &VBO);
}
unsigned int VBO, VAO;
};

View file

@ -1,49 +0,0 @@
#pragma once
#include <memory>
#include "IDrawable.hpp"
#include "util.hpp"
class Quad :
public IDrawable
{
public:
Quad(Vec3 topLeft, Vec3 topRight, Vec3 botRight, Vec3 botLeft)
{
memcpy_s(vertices + 0, sizeof(vertices), &topLeft, sizeof(topLeft));
memcpy_s(vertices + 3, sizeof(vertices), &topRight, sizeof(topRight));
memcpy_s(vertices + 6, sizeof(vertices), &botRight, sizeof(botRight));
memcpy_s(vertices + 9, sizeof(vertices), &botLeft, sizeof(botLeft));
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
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_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Draw() override
{
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices);
}
private:
unsigned int EBO;
float vertices[3 * 4];
inline static unsigned int indices[6] = {
0, 1, 3,
1, 2, 3
};
};

View file

@ -1,35 +0,0 @@
#include <memory>
#include "IDrawable.hpp"
#include "util.hpp"
class Triangle :
public IDrawable
{
public:
Triangle(Vec3 v1, Vec3 v2, Vec3 v3)
{
memcpy_s(vertices + 0, 9 * sizeof(float), &v1, 3 * sizeof(float));
memcpy_s(vertices + 3, 9 * sizeof(float), &v2, 3 * sizeof(float));
memcpy_s(vertices + 6, 9 * sizeof(float), &v3, 3 * sizeof(float));
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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 Draw() override
{
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
private:
float vertices[9];
};

View file

@ -1,11 +0,0 @@
#version 460 core
out vec4 FragColor;
in vec4 vertexColor;
uniform float t;
void main()
{
FragColor = vertexColor * vec4((sin(t) + 1.f) / 2, (cos(t) + 1.f) / 2, (-sin(t) + 1.f), 1.f);
}

View file

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

View file

@ -1,6 +0,0 @@
#pragma once
typedef struct
{
float x, y, z;
} Vec3;