Skip to content

Commit

Permalink
Added vertexbuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Prtm2110 committed Jan 10, 2025
1 parent a8f2ef8 commit 3068e35
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 53 deletions.
2 changes: 2 additions & 0 deletions Spraky-core/Spraky-core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="x64\src\graphics\buffers\buffer.cpp" />
<ClCompile Include="x64\src\graphics\shader.cpp" />
<ClCompile Include="x64\src\graphics\window.cpp" />
<ClCompile Include="x64\src\maths\mat4.cpp" />
Expand All @@ -144,6 +145,7 @@
<ClCompile Include="x64\src\utils\fileutils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64\src\graphics\buffers\buffer.h" />
<ClInclude Include="x64\src\graphics\shader.h" />
<ClInclude Include="x64\src\graphics\window.h" />
<ClInclude Include="x64\src\maths\mat4.h" />
Expand Down
6 changes: 6 additions & 0 deletions Spraky-core/Spraky-core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ClCompile Include="x64\src\utils\fileutils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="x64\src\graphics\buffers\buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64\src\graphics\window.h">
Expand Down Expand Up @@ -68,6 +71,9 @@
<ClInclude Include="x64\src\graphics\shader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="x64\src\graphics\buffers\buffer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="x64\src\shaders\basic.frag" />
Expand Down
33 changes: 33 additions & 0 deletions Spraky-core/x64/src/graphics/buffers/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "buffer.h"

using namespace sparky;
using namespace graphics;

namespace sparky {
namespace graphics {

Buffer::Buffer(GLfloat* data, GLsizei count, GLuint componenetCount)
: m_ComponentCount(componenetCount)
{
//m_ComponentCount = componenetCount;this is same as the above line
glGenBuffers(1, &m_BufferID);
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID);
glBufferData(GL_ARRAY_BUFFER, count * sizeof(GLfloat), data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

}
/*Buffer::~Buffer()
{
glDeleteBuffers(1, &m_BufferID);
}*/
void Buffer::bind() const
{
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID);
}
void Buffer::unbind() const
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

}
}
23 changes: 23 additions & 0 deletions Spraky-core/x64/src/graphics/buffers/buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include<GL/glew.h>

namespace sparky {
namespace graphics {

class Buffer {

private:
GLuint m_BufferID;
GLuint m_ComponentCount;

public:
Buffer(GLfloat* data, GLsizei count, GLuint componenetCount);
void bind() const;
void unbind() const;
inline GLuint getComponentCount() const { return m_ComponentCount; }


};
}
}
106 changes: 53 additions & 53 deletions Spraky-core/x64/src/graphics/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace sparky {
namespace graphics {

Shader::Shader(const char* vertPath, const char* fragPath)
: m_VertPath(vertPath), m_FragPath(fragPath) // Updated variable names
: m_VertPath(vertPath), m_FragPath(fragPath)
{
m_ShaderID = load();
}
Expand All @@ -21,60 +21,60 @@ namespace sparky {

GLuint Shader::load()
{
GLuint program = glCreateProgram();
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
GLuint program = glCreateProgram();
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);

std::string vertSourceStr = read_file(m_VertPath);
const char* vertSource = vertSourceStr.c_str();
std::string vertSourceStr = read_file(m_VertPath);
const char* vertSource = vertSourceStr.c_str();

std::string fragSourceStr = read_file(m_FragPath);
const char* fragSource = fragSourceStr.c_str();

// Vertex Shader
glShaderSource(vertex, 1, &vertSource, NULL);
glCompileShader(vertex);

GLint result;
glGetShaderiv(vertex, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
GLint length;
glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(vertex, length, &length, &error[0]);
std::cout << "Failed to compile vertex shader!" << std::endl << &error[0] << std::endl;
glDeleteShader(vertex);
return 0;
}

// Fragment Shader
glShaderSource(fragment, 1, &fragSource, NULL);
glCompileShader(fragment);

glGetShaderiv(fragment, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
GLint length;
glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(fragment, length, &length, &error[0]);
std::cout << "Failed to compile fragment shader!" << std::endl << &error[0] << std::endl;
glDeleteShader(fragment);
return 0;
}

// Attach shaders to program
glAttachShader(program, vertex);
glAttachShader(program, fragment);

glLinkProgram(program);
glValidateProgram(program);

glDeleteShader(vertex);
glDeleteShader(fragment);

return program;
std::string fragSourceStr = read_file(m_FragPath);
const char* fragSource = fragSourceStr.c_str();

// Vertex Shader
glShaderSource(vertex, 1, &vertSource, NULL);
glCompileShader(vertex);

GLint result;
glGetShaderiv(vertex, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
GLint length;
glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(vertex, length, &length, &error[0]);
std::cout << "Failed to compile vertex shader!" << std::endl << &error[0] << std::endl;
glDeleteShader(vertex);
return 0;
}

// Fragment Shader
glShaderSource(fragment, 1, &fragSource, NULL);
glCompileShader(fragment);

glGetShaderiv(fragment, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
GLint length;
glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(fragment, length, &length, &error[0]);
std::cout << "Failed to compile fragment shader!" << std::endl << &error[0] << std::endl;
glDeleteShader(fragment);
return 0;
}

// Attach shaders to program
glAttachShader(program, vertex);
glAttachShader(program, fragment);

glLinkProgram(program);
glValidateProgram(program);

glDeleteShader(vertex);
glDeleteShader(fragment);

return program;
}

GLint Shader::getUniformLocation(const GLchar* name)
Expand Down

0 comments on commit 3068e35

Please sign in to comment.