Added indexbuffer and vertex buffer classes

This commit is contained in:
Vitor Fernandes 2020-06-20 14:42:21 +01:00
parent edec347ecc
commit 862aac0454
No known key found for this signature in database
GPG key ID: EBFB4EE09F348A26
3 changed files with 52 additions and 42 deletions

View file

@ -6,8 +6,15 @@ find_package(glfw3 3.3 REQUIRED)
find_package(OpenGL REQUIRED)
# Add gl1 executable build from gl1.cpp
add_executable(gl1 gl1.cpp)
# Add gl1 executable build from gl1.cpp, glew.c and Renderer.cpp
add_executable(
gl1
gl1.cpp
Renderer.cpp
VertexBuffer.cpp
IndexBuffer.cpp
glew.c
)
# Link gl1 with glew
#target_link_libraries(gl1 glew)

View file

@ -1,31 +1,15 @@
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <signal.h>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
typedef unsigned int uint;
//Only on opengl4
static void ErrorGLCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
{
std::cout << "Source: " << source << "Type: " << type << "Id: " << id << "Severity: " << severity << "Length: " << length << message << std::endl;
}
// Old way to process openGL errors
static void GLClearError()
{
while (glGetError() != GL_NO_ERROR)
; //Clear error flags
}
static void GLCheckError(){
while(GLenum error = glGetError()){
std::cout << "[OpenGL Error] (" << error << ")" << std::endl;
}
}
struct ShaderProgramSource
{
std::string VertexSource;
@ -140,6 +124,7 @@ int main(void)
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
//Print opengl version
std::cout << glGetString(GL_VERSION) << std::endl;
@ -151,37 +136,36 @@ int main(void)
0.5f, 0.5f,
-0.5f, 0.5f};
uint indexes[] = {
unsigned int indexes[] = {
0, 1, 2, 2, 3, 0};
//Create buffer
//Buffer id
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 10 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
unsigned int index_buffer;
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(float), indexes, GL_STATIC_DRAW);
VertexBuffer* vb = new VertexBuffer(positions, 4 * 2 * sizeof(float));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
IndexBuffer* ib = new IndexBuffer(indexes,6);
ShaderProgramSource source = ParseShader(SHADERS_PATH);
std::cout << "Shaders: " << source.VertexSource << std::endl;
std::cout << std::endl;
std::cout << source.FragmentSource << std::endl;
uint shader = CreateShader(source.VertexSource, source.FragmentSource);
glUseProgram(shader);
GLCall(glUseProgram(shader));
//Find the shader uniform color
int uniform_location = glGetUniformLocation(shader, "u_color");
if (uniform_location == -1)
{
std::cout << "Error fetching uniform location" << std::endl;
}
float red = 0.0f;
float increment = 0.05f;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
GLCall(glClear(GL_COLOR_BUFFER_BIT));
// Legacy OpenGL
//glBegin(GL_TRIANGLES);
@ -194,7 +178,22 @@ int main(void)
//without index buffer
//glDrawArrays(GL_TRIANGLES, 0, 12);
//GLClearError();
glDrawElements(GL_TRIANGLES, 6 * sizeof(uint), GL_UNSIGNED_INT, nullptr);
GLCall(glUseProgram(shader));
GLCall(glUniform4f(uniform_location, 0.2f, 0.3f, 0.8f, 1.0f));
//GLCall(glBindVertexArray(vb));
ib->Bind();
GLCall(glDrawElements(GL_TRIANGLES, 6 * sizeof(uint), GL_UNSIGNED_INT, nullptr));
if (red > 1.0f)
increment = -0.05f;
else if (red < 0.0f)
increment = 0.05f;
red += increment;
//GLCheckError();
/* Swap front and back buffers */
glfwSwapBuffers(window);
@ -203,6 +202,8 @@ int main(void)
glfwPollEvents();
}
delete ib;
delete vb;
glfwTerminate();
return 0;
}

View file

@ -10,6 +10,8 @@ void main(){
#version 330 core
layout(location = 0) out vec4 color;
uniform vec4 u_color;
void main(){
color=vec4(1.0,0.0,0.0,1.0);
color=u_color;
}