Added basic object wrapper for VAOs

This commit is contained in:
Robert 2021-01-20 16:51:55 +01:00
parent c3e49cd78e
commit 8921957161
7 changed files with 162 additions and 17 deletions

View file

@ -37,20 +37,36 @@ int main(int argc, char** argv)
oglu::LoadGLLoader((GLADloadproc)glfwGetProcAddress);
oglu::SetViewport(0, 0, windowSize, windowSize);
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
oglu::VertexAttribute topology[] = {
{ 0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0 }
{ 0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0 },
{ 1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)) }
};
oglu::Object triangle(vertices, sizeof(vertices), nullptr, 0, topology, sizeof(topology));
oglu::Shader shader("shaders/vertexShader.vert", "");
oglu::Shader* shader;
try
{
shader = new oglu::Shader("shaders/vertexShader.vert", "shaders/fragmentShader.frag");
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
oglu::Object square(vertices, sizeof(vertices), indices, sizeof(indices), topology, sizeof(topology));
while (!glfwWindowShouldClose(window))
{
@ -58,6 +74,9 @@ int main(int argc, char** argv)
oglu::ClearScreen(GL_COLOR_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f));
shader->Use();
square.BindAndDraw();
glfwSwapBuffers(window);
glfwPollEvents();
}

View file

@ -0,0 +1,9 @@
#version 330 core
in vec3 oCol;
out vec4 FragColor;
void main()
{
FragColor = vec4(oCol, 1.0f);
}

View file

@ -1,7 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aCol;
out vec3 oCol;
void main()
{
oCol = aCol;
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

View file

@ -22,9 +22,17 @@ namespace oglu
Object(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
GLuint GetVAO() { return VAO; }
void Bind();
void Unbind();
void Draw();
void BindAndDraw();
private:
inline void RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology);
GLuint VAO;
GLsizei count;
};
}

View file

@ -10,12 +10,15 @@ namespace oglu
{
public:
Shader(const char* vertexShaderFile, const char* fragmentShaderFile);
~Shader();
void Use();
private:
void LoadShaderSource(const char* filename, char** buffer);
private:
GLuint vertexShader, fragmentShader;
GLuint vertexShader, fragmentShader, program;
};
}

View file

@ -12,10 +12,58 @@ namespace oglu
const VertexAttribute* topology, size_t topologySize) :
VAO(0)
{
GLuint VBO;
//glGenBuffers(1, &VBO);
topologySize /= sizeof(VertexAttribute);
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
//glBufferData(GL_ARRAY_BUFFER, verticesSize, vertices, GL_STATIC_DRAW);
GLuint VBO;
glGenBuffers(1, &VBO);
GLuint EBO;
glGenBuffers(1, &EBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, verticesSize, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesSize, indices, GL_STATIC_DRAW);
for (int i = 0; i < topologySize; i++)
{
RegisterVertexAttribPointer(i, topology[i]);
}
glBindVertexArray(0);
count = indicesSize / sizeof(GLuint);
}
void Object::Bind()
{
glBindVertexArray(VAO);
}
void Object::Unbind()
{
}
void Object::Draw()
{
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (GLvoid*)0);
}
void Object::BindAndDraw()
{
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
}
void Object::RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology)
{
glVertexAttribPointer(topology.index, topology.size, topology.type, topology.normalized, topology.stride, topology.pointer);
glEnableVertexAttribArray(index);
}
}

View file

@ -10,11 +10,67 @@ namespace oglu
Shader::Shader(const char* vertexShaderFile, const char* fragmentShaderFile) :
vertexShader(0), fragmentShader(0)
{
std::cout << "jkskdjks" << std::endl;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Load vertex shader
char* source = nullptr;
LoadShaderSource(vertexShaderFile, &source);
std::cout << source << std::endl;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &source, NULL);
glCompileShader(vertexShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
delete source;
throw std::exception(std::string("Failed to compile shader " + std::string(vertexShaderFile) + "\n" + infoLog).c_str());
}
delete source;
// Load Fragment shader
LoadShaderSource(fragmentShaderFile, &source);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &source, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
delete source;
throw std::exception(std::string("Failed to compile shader " + std::string(fragmentShaderFile) + "\n" + infoLog).c_str());
}
delete source;
// Link into program
program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(program, 512, NULL, infoLog);
throw std::exception(std::string("Failed to link program.\n" + std::string(infoLog)).c_str());
}
// Dispose of shader objects
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
}
Shader::~Shader()
{
}
void Shader::Use()
{
glUseProgram(program);
}
void Shader::LoadShaderSource(const char* filename, char** buffer)
@ -28,9 +84,7 @@ namespace oglu
std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
(*buffer) = (char*)malloc(str.size() + 1);
std::cout << "dsnskdnksdnk" << std::endl;
memcpy(*buffer, str.c_str(), str.size());
std::cout << "diufoisdufo34" << std::endl;
(*buffer)[str.size()] = '\x00';
}
}