Added primitives
This commit is contained in:
parent
e9f81dcb31
commit
8ad43456cf
|
@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.7)
|
|||
|
||||
project("ComplexPlotting")
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
add_executable(ComplexPlotting
|
||||
"main.cpp"
|
||||
)
|
||||
"ShaderProgram.hpp")
|
||||
|
||||
file(GLOB vendor_SRC
|
||||
${CMAKE_SOURCE_DIR}/vendor/src/*.c
|
||||
|
@ -13,9 +13,14 @@ target_sources(ComplexPlotting PRIVATE
|
|||
|
||||
target_include_directories(ComplexPlotting PRIVATE
|
||||
glfw
|
||||
./primitives
|
||||
${CMAKE_SOURCE_DIR}/vendor/include
|
||||
)
|
||||
|
||||
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
|
||||
)
|
95
src/ShaderProgram.hpp
Normal file
95
src/ShaderProgram.hpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
#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);
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
unsigned int vertexShader_id, fragmentShader_id, program_id;
|
||||
};
|
37
src/main.cpp
37
src/main.cpp
|
@ -3,6 +3,10 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#include "ShaderProgram.hpp"
|
||||
#include "Triangle.hpp"
|
||||
#include "Quad.hpp"
|
||||
|
||||
void LogGLFWerror()
|
||||
{
|
||||
const char* error = new const char;
|
||||
|
@ -42,6 +46,32 @@ int main(int argc, char** argv)
|
|||
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();
|
||||
|
@ -49,8 +79,15 @@ int main(int argc, char** argv)
|
|||
glClearColor(0.4f, 0.1f, 0.5f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
program.Use();
|
||||
q.Draw();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
return 0;
|
||||
}
|
18
src/primitives/IDrawable.hpp
Normal file
18
src/primitives/IDrawable.hpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#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;
|
||||
};
|
50
src/primitives/Quad.hpp
Normal file
50
src/primitives/Quad.hpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#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);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int EBO;
|
||||
float vertices[3 * 4];
|
||||
inline static unsigned int indices[6] = {
|
||||
0, 1, 3,
|
||||
1, 2, 3
|
||||
};
|
||||
};
|
36
src/primitives/Triangle.hpp
Normal file
36
src/primitives/Triangle.hpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#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);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
private:
|
||||
float vertices[9];
|
||||
};
|
8
src/shaders/triangle.frag
Normal file
8
src/shaders/triangle.frag
Normal file
|
@ -0,0 +1,8 @@
|
|||
#version 460 core
|
||||
out vec4 FragColor;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
}
|
7
src/shaders/triangle.vert
Normal file
7
src/shaders/triangle.vert
Normal file
|
@ -0,0 +1,7 @@
|
|||
#version 460 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
}
|
6
src/util.hpp
Normal file
6
src/util.hpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float x, y, z;
|
||||
} Vec3;
|
Loading…
Reference in a new issue