Add exception handling to Attributes::bind, add default constructor to Model

This commit is contained in:
ohsqueezy 2023-07-13 21:38:53 -04:00
parent cdd01672c3
commit 89fbe359f6
5 changed files with 58 additions and 37 deletions

View File

@ -79,8 +79,31 @@ std::size_t sb::Attributes::size() const
void sb::Attributes::bind(const std::string& name, GLuint program)
{
index(glGetAttribLocation(program, name.c_str()));
glVertexAttribPointer(index(), dimensions(), type(), normalized(), 0, reinterpret_cast<GLvoid*>(_offset));
GLint index = glGetAttribLocation(program, name.c_str());
/* A return value of -1 indicates an error finding the given attribute name */
if (index == -1)
{
std::ostringstream message;
message << "Error getting attribute location of \"" << name << "\" in shader program. Did the program compile?" <<
" Is the attribute present and not optimized out?";
throw std::runtime_error(message.str());
}
this->index(index);
/* Debug */
std::ostringstream message;
message << "After getting attribute location of " << name;
sb::Log::gl_errors(message.str());
/* Define an array of vertex attributes that have data stored at the specified offset. */
glVertexAttribPointer(this->index(), dimensions(), type(), normalized(), 0, reinterpret_cast<GLvoid*>(_offset));
/* Debug */
message = std::ostringstream();
message << "After binding " << *this << " to " << name;
sb::Log::gl_errors(message.str());
}
void sb::Attributes::enable() const

View File

@ -1,14 +1,12 @@
/* /\ +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, and modify without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+--------------+
*/
/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#include "GLObject.hpp"
using namespace sb;

View File

@ -1,29 +1,28 @@
/* /\ +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+--------------+
/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+--------------+
[GLObject.hpp]
The abstract GLObject class is meant to be inherited and implemented by more specific
types of OpenGL objects, for example Textures or Buffer objects. It stores the object's
ID and declares virtual functions for generating, binding, and destroying the object for
the derivative class to implement.
The abstract GLObject class is meant to be inherited and implemented by more specific
types of OpenGL objects, for example Textures or Buffer objects. It stores the object's
ID and declares virtual functions for generating, binding, and destroying the object.
An appropriate deleter function must be passed in to the constructor from the derived
class. It can be a custom function, or it can be one of the GL deleter functions like
glDeleteTextures.
An appropriate deleter function must be passed in to the constructor from the derived
class. It can be a custom function, or it can be one of the GL deleter functions like
glDeleteTextures.
A VAO class and a general Buffer class are also defined here. The VAO class just fills
in the abstract methods of the GLObject class and doesn't have any further SPACE BOX
specific implementation. The buffer class is probably most useful being inherited by a
derived class that implements a more specific type of buffer that passes its target to
the base class, like the VBO class.
A VAO class and a general Buffer class are also defined here. The VAO class just fills
in the abstract methods of the GLObject class and doesn't have any further Space Box
specific implementation. The buffer class is probably most useful being inherited by a
derived class that implements a more specific type of buffer that passes its target to
the base class, like the VBO class.
*/
#pragma once

View File

@ -48,7 +48,7 @@ namespace sb
/*!
* Construct a sb::Model object with no attributes or textures.
*/
Model();
Model() {};
/*!
* Construct a sb::Model, adding sb::Attributes each already wrapped in a shared pointer. The attributes should be passed

View File

@ -23,6 +23,7 @@ VBO::VBO() : Buffer(GL_ARRAY_BUFFER) {}
void VBO::allocate(GLsizeiptr size, GLenum usage)
{
glBufferData(target(), size, nullptr, usage);
/* Debug */
std::ostringstream message;
message << "After allocating memory for " << *this;
@ -37,7 +38,7 @@ GLintptr VBO::add(sb::Attributes& attributes)
/* Debug */
std::ostringstream initialization_message;
initialization_message << "After setting " << attributes.size() << "bytes in " << *this;
initialization_message << "After setting " << attributes.size() << " bytes in " << *this;
sb::Log::gl_errors(initialization_message.str());
/* Increase offset and return */