Texturing; OBJ loader
This commit is contained in:
121
common.hh
Normal file
121
common.hh
Normal file
@@ -0,0 +1,121 @@
|
||||
#ifndef __OPENGLPLAYGROUND_COMMON_HH__
|
||||
#define __OPENGLPLAYGROUND_COMMON_HH__
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GL/gl.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
class Exception {
|
||||
public:
|
||||
Exception() {
|
||||
}
|
||||
|
||||
virtual ~Exception() {}
|
||||
|
||||
virtual std::string toString() const = 0;
|
||||
};
|
||||
|
||||
class POSIXException : public Exception {
|
||||
public:
|
||||
POSIXException(int err): Exception(), _err(err) {
|
||||
}
|
||||
|
||||
virtual ~POSIXException() {}
|
||||
|
||||
int getErr() const {return _err;}
|
||||
|
||||
virtual std::string toString() const {
|
||||
return std::string("POSIXException: ") + std::strerror(_err);
|
||||
}
|
||||
|
||||
private:
|
||||
int _err;
|
||||
};
|
||||
|
||||
class GLException : public Exception {
|
||||
public:
|
||||
GLException(GLenum err) : Exception(), _err(err) {
|
||||
}
|
||||
|
||||
virtual ~GLException() {}
|
||||
|
||||
GLenum getErr() const {return _err;}
|
||||
|
||||
std::string errToString() const {
|
||||
std::string ret;
|
||||
if (_err == GL_INVALID_ENUM)
|
||||
ret += "GL_INVALID_ENUM ";
|
||||
if (_err == GL_INVALID_VALUE)
|
||||
ret += "GL_INVALID_VALUE ";
|
||||
if (_err == GL_INVALID_OPERATION)
|
||||
ret += "GL_INVALID_OPERATION ";
|
||||
if (_err == GL_INVALID_FRAMEBUFFER_OPERATION)
|
||||
ret += "GL_INVALID_FRAMEBUFFER_OPERATION ";
|
||||
if (_err == GL_OUT_OF_MEMORY)
|
||||
ret += "GL_OUT_OF_MEMORY ";
|
||||
if (_err == GL_STACK_UNDERFLOW)
|
||||
ret += "GL_STACK_UNDERFLOW ";
|
||||
if (_err == GL_STACK_OVERFLOW)
|
||||
ret += "GL_STACK_OVERFLOW ";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
virtual std::string toString() const {
|
||||
return "GLException: " + errToString() + "(" + std::to_string(_err) + ")";
|
||||
}
|
||||
|
||||
private:
|
||||
GLenum _err;
|
||||
};
|
||||
|
||||
class SDLException : public Exception {
|
||||
public:
|
||||
SDLException() : Exception(), _msg(SDL_GetError()) {
|
||||
}
|
||||
|
||||
virtual ~SDLException() {}
|
||||
|
||||
virtual std::string toString() const {
|
||||
return "SDLException: " + _msg;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _msg;
|
||||
};
|
||||
|
||||
static void checkGlError() {
|
||||
GLenum err;
|
||||
if ((err = glGetError()) != GL_NO_ERROR)
|
||||
throw GLException(err);
|
||||
}
|
||||
|
||||
static std::string fileToString(std::string const& name) {
|
||||
std::FILE *file = std::fopen(name.c_str(), "r");
|
||||
if (!file) {
|
||||
throw POSIXException(errno);
|
||||
}
|
||||
|
||||
std::string ret;
|
||||
char buf[512];
|
||||
std::size_t p;
|
||||
while((p = std::fread(buf, 1, 511, file)) > 0) {
|
||||
buf[p] = '\0';
|
||||
ret.append(buf);
|
||||
}
|
||||
if (!std::feof(file)) {
|
||||
std::fclose(file);
|
||||
throw POSIXException(errno);
|
||||
}
|
||||
|
||||
std::fclose(file);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user