abstracted creation of vertex and index buffers

This commit is contained in:
2022-05-02 15:09:57 +10:00
parent fc714ff283
commit 36a0c0035d
54 changed files with 265 additions and 65 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\OpenGL.sln",
"PreviewInSolutionExplorer": false
}

BIN
.vs/slnx.sqlite Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,5 +1,2 @@
 Application.cpp
LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib 'MSVCRT' 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 'D:\cpp\OpenGL\Debug\vc120.pdb'; linking object as if no debug info
OpenGL.vcxproj -> D:\cpp\OpenGL\Debug\OpenGL.exe

BIN
OpenGL/Debug/Renderer.obj Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -152,10 +152,18 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\Application.cpp" />
<ClCompile Include="src\IndexBuffer.cpp" />
<ClCompile Include="src\Renderer.cpp" />
<ClCompile Include="src\VertexBuffer.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="res\shaders\Basic.shader" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\IndexBuffer.h" />
<ClInclude Include="src\Renderer.h" />
<ClInclude Include="src\VertexBuffer.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@@ -18,8 +18,28 @@
<ClCompile Include="src\Application.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Renderer.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>
<None Include="res\shaders\Basic.shader" />
</ItemGroup>
<ItemGroup>
<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>
</Project>

Binary file not shown.

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ProjectOutputs>
<ProjectOutput>
<FullPath>D:\cpp\OpenGL\Release\OpenGL.exe</FullPath>
</ProjectOutput>
</ProjectOutputs>
<ContentFiles />
<SatelliteDlls />
<NonRecipeFileRefs />
</Project>

BIN
OpenGL/Release/OpenGL.iobj Normal file

Binary file not shown.

BIN
OpenGL/Release/OpenGL.ipdb Normal file

Binary file not shown.

View File

@@ -0,0 +1,8 @@
 Application.cpp
LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
Generating code
Previous IPDB not found, fall back to full compilation.
All 214 functions were compiled because no usable IPDB/IOBJ from previous compilation was found.
Finished generating code
glew32s.lib(glew.obj) : warning LNK4099: PDB 'vc120.pdb' was not found with 'glew32s.lib(glew.obj)' or at 'D:\cpp\OpenGL\Release\vc120.pdb'; linking object as if no debug info
OpenGL.vcxproj -> D:\cpp\OpenGL\Release\OpenGL.exe

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native32Bit:VCToolsVersion=14.31.31103:TargetPlatformVersion=10.0.19041.0:
Release|Win32|D:\cpp\OpenGL\|

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
OpenGL/Release/vc143.pdb Normal file

Binary file not shown.

View File

@@ -13,7 +13,9 @@ void main()
layout(location = 0) out vec4 color;
uniform vec4 u_Color;
void main()
{
color = vec4(1, 1, 0, 1.0);
color = u_Color;
};

View File

@@ -4,8 +4,13 @@
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
struct ShaderProgramSource
{
std::string VertexSource;
@@ -86,10 +91,16 @@ static unsigned int CreateShader(const std::string& vertexShader, const std::str
int main(void)
{
srand(static_cast <unsigned> (time(0)));
/* Initialize the library */
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", nullptr, nullptr);
@@ -103,78 +114,104 @@ int main(void)
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (glewInit() != GLEW_OK)
std::cout << "ERROR" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
constexpr float triforcePos[] = {
-1.0f, -1.0f, //0
-0.5f, 0.0f, //1 --
0.0f, -1.0f, //2
0.0f, 1.0f, //3
0.5f, 0.0f, //4 --
1.0f, -1.0f //5
};
unsigned int triforceInd[] = {
0, 1, 2,
2, 4, 5,
1, 3, 4
};
constexpr float positions[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f,
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), triforcePos, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
unsigned int ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * 3 * sizeof(unsigned int), triforceInd, GL_STATIC_DRAW);
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
glUseProgram(shader);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
constexpr float triforcePos[] = {
-1.0f, -1.0f, //0
-0.5f, 0.0f, //1 --
0.0f, -1.0f, //2
0.0f, 1.0f, //3
0.5f, 0.0f, //4 --
1.0f, -1.0f //5
glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, nullptr);
};
unsigned int triforceInd[] = {
0, 1, 2,
2, 4, 5,
1, 3, 4
};
constexpr float positions[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f,
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
unsigned int vao;
GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao));
VertexBuffer vb(positions, 4 * 2 * sizeof(float));
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
IndexBuffer ib(indices, 3 * 2);
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
glUseProgram(shader);
/* Swap front and back buffers */
glfwSwapBuffers(window);
int location = glGetUniformLocation(shader, "u_Color");
ASSERT(location != -1);
glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f);
/* Poll for and process events */
glfwPollEvents();
GLCall(glBindVertexArray(0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0);
float r = 0.0f;
float increment = 0.05f;
int x = 0;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
x++;
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader);
glUniform4f(location, r, 0.3f, 0.8f, 1.0f);
GLCall(glBindVertexArray(vao));
ib.Bind();
GLCall(glDrawElements(GL_TRIANGLES, 9, 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 */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(shader);
}
glDeleteProgram(shader);
glfwTerminate();
return 0;
}

View File

@@ -0,0 +1,25 @@
#include "IndexBuffer.h"
#include "Renderer.h"
IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int count)
: m_Count(count)
{
GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW));
}
IndexBuffer::~IndexBuffer()
{
GLCall(glDeleteBuffers(1, &m_RendererID));
}
void IndexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID));
}
void IndexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}

16
OpenGL/src/IndexBuffer.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
class IndexBuffer
{
private:
unsigned int m_RendererID;
unsigned int m_Count;
public:
IndexBuffer(const unsigned int* data, unsigned int count);
~IndexBuffer();
void Bind() const;
void Unbind() const;
inline unsigned int GetCount() const { return m_Count; }
};

18
OpenGL/src/Renderer.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "Renderer.h"
#include <iostream>
void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
}
bool GLLogCall(const char* function, const char* file, int line)
{
while (const GLenum error = glGetError())
{
std::cout << "[OpenGL Error] (" << error << "): " << function << " " << file << ":" << line << std::endl;
return false;
}
return true;
}

11
OpenGL/src/Renderer.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <GL/glew.h>
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
void GLClearError();
bool GLLogCall (const char* function, const char* file, int line);

View File

@@ -0,0 +1,25 @@
#include "VertexBuffer.h"
#include "Renderer.h"
VertexBuffer::VertexBuffer(const void* data, unsigned int size)
{
GLCall(glGenBuffers(1, &m_RendererID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
GLCall(glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW));
}
VertexBuffer::~VertexBuffer()
{
GLCall(glDeleteBuffers(1, &m_RendererID));
}
void VertexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
}
void VertexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
}

13
OpenGL/src/VertexBuffer.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
class VertexBuffer
{
private:
unsigned int m_RendererID{};
public:
VertexBuffer(const void* data, unsigned int size);
~VertexBuffer();
void Bind() const;
void Unbind() const;
};

BIN
Release/OpenGL.exe Normal file

Binary file not shown.

BIN
Release/OpenGL.pdb Normal file

Binary file not shown.