added shader
This commit is contained in:
parent
8f1ea5c7ff
commit
9daa5b5c2c
|
@ -59,6 +59,50 @@ void Application::Init(int width, int height, const std::string& title)
|
|||
glViewport(0, 0, width, height);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
// TODO: Remove, this should probably be done elsewhere
|
||||
model = VAOFactory::Produce(
|
||||
{
|
||||
-0.5f, -0.5f,
|
||||
0.0f, 0.5f,
|
||||
0.5f, -0.5f
|
||||
},
|
||||
{
|
||||
0, 1, 2
|
||||
},
|
||||
{
|
||||
{ 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 }
|
||||
}
|
||||
);
|
||||
|
||||
shader = ShaderFactory::Produce(
|
||||
R"(
|
||||
#version 460 core
|
||||
|
||||
layout (location = 0) in vec2 aPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
)",
|
||||
R"(
|
||||
#version 460 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(0.5f, 0.0f, 0.8f, 1.0f);
|
||||
}
|
||||
)"
|
||||
);
|
||||
|
||||
if (!shader->Good())
|
||||
{
|
||||
throw std::runtime_error("Shader creation failed");
|
||||
}
|
||||
}
|
||||
|
||||
void Application::Launch()
|
||||
|
@ -70,6 +114,9 @@ void Application::Launch()
|
|||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
shader->Use();
|
||||
model->Render();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "Shader.hpp"
|
||||
#include "VertexArrayObject.hpp"
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
|
@ -32,4 +34,7 @@ public:
|
|||
|
||||
private:
|
||||
GLFWwindow* window = nullptr;
|
||||
|
||||
Shader shader = nullptr;
|
||||
VertexArrayObject model = nullptr;
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
add_executable(visualizer
|
||||
"main.cpp" "Application.cpp"
|
||||
"VertexArrayObject.cpp")
|
||||
"VertexArrayObject.cpp" "Shader.cpp")
|
||||
|
||||
target_sources(visualizer PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c
|
||||
|
|
74
src/Shader.cpp
Normal file
74
src/Shader.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include "Shader.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <glad/glad.h>
|
||||
|
||||
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) :
|
||||
id(0)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||
const char* vertexShaderSource = vertexShader.c_str();
|
||||
glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL);
|
||||
glCompileShader(vertexShaderID);
|
||||
|
||||
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vertexShaderID, 512, NULL, infoLog);
|
||||
std::cerr << "Vertex shader creation failed: \n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(vertexShaderID);
|
||||
return;
|
||||
}
|
||||
|
||||
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
const char* fragmentShaderSource = fragmentShader.c_str();
|
||||
glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, NULL);
|
||||
glCompileShader(fragmentShaderID);
|
||||
|
||||
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(fragmentShaderID, 512, NULL, infoLog);
|
||||
std::cerr << "Fragment shader creation failed: \n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
return;
|
||||
}
|
||||
|
||||
id = glCreateProgram();
|
||||
glAttachShader(id, vertexShaderID);
|
||||
glAttachShader(id, fragmentShaderID);
|
||||
glLinkProgram(id);
|
||||
|
||||
glGetProgramiv(id, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(fragmentShaderID, 512, NULL, infoLog);
|
||||
std::cerr << "Shader program linking failed: \n" << infoLog << std::endl;
|
||||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
|
||||
id = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
}
|
||||
|
||||
AbstractShader::~AbstractShader()
|
||||
{
|
||||
glDeleteProgram(id);
|
||||
}
|
||||
|
||||
void AbstractShader::Use()
|
||||
{
|
||||
glUseProgram(id);
|
||||
}
|
31
src/Shader.hpp
Normal file
31
src/Shader.hpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class AbstractShader
|
||||
{
|
||||
friend class ShaderFactory;
|
||||
|
||||
public:
|
||||
AbstractShader(const std::string& vertexShader, const std::string& fragmentShader);
|
||||
AbstractShader(const AbstractShader& other) = delete;
|
||||
~AbstractShader();
|
||||
|
||||
inline bool Good() { return id != 0; }
|
||||
void Use();
|
||||
|
||||
private:
|
||||
unsigned int id;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<AbstractShader> Shader;
|
||||
|
||||
class ShaderFactory
|
||||
{
|
||||
public:
|
||||
inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader)
|
||||
{
|
||||
return std::make_shared<AbstractShader>(vertexShader, fragmentShader);
|
||||
}
|
||||
};
|
|
@ -10,11 +10,22 @@ AbstractVertexArrayObject::~AbstractVertexArrayObject()
|
|||
glDeleteVertexArrays(1, &vao);
|
||||
}
|
||||
|
||||
void AbstractVertexArrayObject::Render()
|
||||
{
|
||||
assert(vao != 0);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
// GLenum result = glGetError();
|
||||
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
|
||||
vao(0), vbo(0), ebo(0)
|
||||
vao(0), vbo(0), ebo(0), indexCount(indices.size())
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
glGenBuffers(1, &ebo);
|
||||
|
||||
// Determing native OpenGL GLenum depending on specified usage
|
||||
GLenum bufferUsage;
|
||||
|
@ -30,15 +41,11 @@ AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices
|
|||
}
|
||||
|
||||
// Create VBO
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), (const void*)(vertices.data()), bufferUsage);
|
||||
|
||||
// Create EBO
|
||||
glGenBuffers(1, &ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), (const void*)(indices.data()), bufferUsage);
|
||||
|
||||
// Set up pipeline layout
|
||||
|
|
|
@ -31,13 +31,14 @@ class AbstractVertexArrayObject
|
|||
|
||||
public:
|
||||
AbstractVertexArrayObject() = delete;
|
||||
AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage);
|
||||
~AbstractVertexArrayObject();
|
||||
|
||||
private:
|
||||
AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage);
|
||||
void Render();
|
||||
|
||||
private:
|
||||
unsigned int vao, vbo, ebo;
|
||||
size_t indexCount;
|
||||
};
|
||||
|
||||
// You cannot actually create this VAO, you are forced to use a shared pointer
|
||||
|
|
Loading…
Reference in a new issue