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