Moved Index and Vertex buffer to a class
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,2 @@
|
||||
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -145,10 +145,16 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\IndexBuffer.cpp" />
|
||||
<ClCompile Include="src\Main.cpp" />
|
||||
<ClCompile Include="src\Rernderer.cpp" />
|
||||
<ClCompile Include="src\VertexBuffer.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\IndexBuffer.h" />
|
||||
<ClInclude Include="src\Main.h" />
|
||||
<ClInclude Include="src\Renderer.h" />
|
||||
<ClInclude Include="src\VertexBuffer.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="res\shaders\Basic.shader" />
|
||||
|
||||
@@ -18,11 +18,29 @@
|
||||
<ClCompile Include="src\Main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Rernderer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\VertexBuffer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\IndexBuffer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\Main.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Renderer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\VertexBuffer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\IndexBuffer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="res\shaders\Basic.shader" />
|
||||
|
||||
@@ -6,27 +6,9 @@
|
||||
#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 << "): "
|
||||
<< function << " " << file << ":" << line << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#include "Renderer.h"
|
||||
#include "VertexBuffer.h"
|
||||
#include "IndexBuffer.h"
|
||||
|
||||
struct ShaderProgramSource {
|
||||
std::string VertexSource;
|
||||
@@ -138,97 +120,91 @@ int main(void){
|
||||
}
|
||||
|
||||
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
|
||||
{
|
||||
//vertex positions
|
||||
float positions[] = {
|
||||
-0.5f, -0.5f,
|
||||
0.5f, -0.5f,
|
||||
0.5f, 0.5f,
|
||||
-0.5f, 0.5f
|
||||
};
|
||||
|
||||
//vertex positions
|
||||
float positions[] = {
|
||||
-0.5f, -0.5f,
|
||||
0.5f, -0.5f,
|
||||
0.5f, 0.5f,
|
||||
-0.5f, 0.5f
|
||||
};
|
||||
|
||||
//index buffer
|
||||
unsigned int indecies[]{
|
||||
0, 1, 2,
|
||||
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, 4 * 2 * sizeof(float), positions, GL_STATIC_DRAW));
|
||||
|
||||
GLCall(glEnableVertexAttribArray(0));
|
||||
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
|
||||
|
||||
//index buffer setup
|
||||
unsigned int ibo; //index buffer object
|
||||
GLCall(glGenBuffers(1, &ibo));
|
||||
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));
|
||||
|
||||
//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));
|
||||
//index buffer
|
||||
unsigned int indecies[]{
|
||||
0, 1, 2,
|
||||
2, 3, 0
|
||||
};
|
||||
|
||||
//creating vertex array object and bindig it
|
||||
unsigned int vao;
|
||||
GLCall(glGenVertexArrays(1, &vao));
|
||||
GLCall(glBindVertexArray(vao));
|
||||
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
|
||||
|
||||
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
|
||||
//Vertex buffer(s)
|
||||
VertexBuffer vb(positions, 4 * 2 * sizeof(float));
|
||||
|
||||
if (r > 1.0f)
|
||||
increment = -0.05f;
|
||||
else if (r < 0.0f)
|
||||
increment = 0.05f;
|
||||
GLCall(glEnableVertexAttribArray(0));
|
||||
GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
|
||||
|
||||
r += increment;
|
||||
//index buffer
|
||||
IndexBuffer ib(indecies, 6);
|
||||
|
||||
/* Swap front and back buffers */
|
||||
GLCall(glfwSwapBuffers(window));
|
||||
//Shader to console
|
||||
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
|
||||
/*std::cout << "VERTEX" << std::endl;
|
||||
std::cout << source.VertexSource << std::endl; //to write shader to console
|
||||
std::cout << "FRAGMENT" << std::endl;
|
||||
std::cout << source.FragmentSource << std::endl;*/
|
||||
|
||||
/* Poll for and process events */
|
||||
GLCall(glfwPollEvents());
|
||||
//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));
|
||||
|
||||
//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));
|
||||
ib.Bind();
|
||||
|
||||
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
|
||||
|
||||
if (r > 1.0f)
|
||||
increment = -0.05f;
|
||||
else if (r < 0.0f)
|
||||
increment = 0.05f;
|
||||
|
||||
r += increment;
|
||||
|
||||
/* Swap front and back buffers */
|
||||
GLCall(glfwSwapBuffers(window));
|
||||
|
||||
/* Poll for and process events */
|
||||
GLCall(glfwPollEvents());
|
||||
}
|
||||
|
||||
GLCall(glDeleteProgram(shader));
|
||||
}
|
||||
|
||||
GLCall(glDeleteProgram(shader));
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user