Vao and code cleanup

This commit is contained in:
Tom
2021-06-07 17:35:06 +02:00
parent 4956a0af5b
commit 86f61bed6b
9 changed files with 40 additions and 10 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,2 +1,5 @@
Main.cpp
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
glew32s.lib(glew.obj) : warning LNK4099: PDB 'vc120.pdb' was not found with 'glew32s.lib(glew.obj)' or at 'C:\dev\Glfw_Practice\c++\Window_practice\Debug\vc120.pdb'; linking object as if no debug info
Window_practice.vcxproj -> C:\dev\Glfw_Practice\c++\Window_practice\Debug\Window_practice.exe

Binary file not shown.

Binary file not shown.

View File

@@ -6,15 +6,18 @@
#include <string>
#include <sstream>
//Macros
#define ASSERT(x) if(!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
//Clearing OpenGl error list
static void GLClearError() {
while (glGetError() != GL_NO_ERROR);
}
//OpenGl logging
static bool GLLogCall(const char* function, const char* file, int line) {
while (GLenum error = glGetError()) {
std::cout << "[OpenGL Error] (" << error << "): "
@@ -30,6 +33,7 @@ struct ShaderProgramSource {
std::string FragmentSource;
};
//For reading in the shader from file
static ShaderProgramSource ParseShader(const std::string& filepath) {
std::ifstream stream(filepath);
@@ -60,6 +64,7 @@ static ShaderProgramSource ParseShader(const std::string& filepath) {
return { ss[0].str(), ss[1].str() };
}
//Shader compiler
static unsigned int CompileShader(unsigned int type, const std::string& source) {
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
@@ -93,13 +98,12 @@ static unsigned int CreateShader(const std::string& vertexShader, const std::str
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
GLCall(glAttachShader(program, vs));
GLCall(glAttachShader(program, fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs));
GLCall(glDeleteShader(fs));
return program;
}
@@ -111,6 +115,11 @@ int main(void){
return -1;
}
//OpenGL version and profile
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Main window creation
GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGl practice", NULL, NULL);
if (!window){
@@ -120,7 +129,6 @@ int main(void){
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); //v-sync
//GLEW init
@@ -145,11 +153,16 @@ int main(void){
2, 3, 0
};
//creating vertex array object and bindig it
unsigned int vao;
GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao));
//Vertex buffer(s)
unsigned int buffer;
GLCall(glGenBuffers(1, &buffer)); //number of buffer to generate
GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer)); //binding the buffer
GLCall(glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), positions, GL_STATIC_DRAW));
GLCall(glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), positions, GL_STATIC_DRAW));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
@@ -160,30 +173,44 @@ int main(void){
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo)); //binding the buffer
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indecies, GL_STATIC_DRAW));
//Shader to console
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
std::cout << "VERTEX" << std::endl;
std::cout << source.VertexSource << std::endl;
std::cout << "FRAGMENT" << std::endl;
std::cout << source.FragmentSource << std::endl;
//Creating the shader
unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
glUseProgram(shader);
//Shader Uniform
//need to bound a shader before this
GLCall(int location = glGetUniformLocation(shader, "u_Color")); //finding u_Color location
ASSERT(location != -1);
GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0)); //unbinding the buffer
//unbinding the buffers
GLCall(glBindVertexArray(0));
GLCall(glUseProgram(0));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
//for animation
float r = 0.0f;
float increment = 0.05f;
//Main loop
while (!glfwWindowShouldClose(window)){
/* Render here */
GLCall(glClear(GL_COLOR_BUFFER_BIT));
GLCall(glUseProgram(shader));
GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f));
GLCall(glBindVertexArray(vao));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
if (r > 1.0f)