1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2015-12-22 17:43:51 +01:00
commit edfc5298e1
252 changed files with 93965 additions and 0 deletions

View file

@ -0,0 +1,69 @@
#ifndef SPECTRE_BATCH_RENDERER2D_H
#define SPECTRE_BATCH_RENDERER2D_H
#include "Renderer2D.h"
#include <Spectre/Scene/Camera2D.h>
#include <Spectre/Graphics/RenderState.h>
#include <Spectre/Graphics/Vertex2D.h>
#include <Spectre/Math/Matrix4.h>
#include <vector>
class ShaderProgram;
class Texture;
class BatchRenderer2D : public Renderer2D
{
public :
BatchRenderer2D();
~BatchRenderer2D();
void setBatchSize(unsigned short value);
void begin();
void end();
void submit(const Renderable2D& renderable);
void render();
protected :
struct Batch
{
RenderType type;
const Texture* texture;
unsigned int count;
unsigned int offset;
};
typedef std::vector<Batch> BatchQueue;
void prepareQueue();
unsigned int addRenderable(Vertex2D* buffer, const Renderable2D* renderable);
void uploadBatch(const Batch& batch);
void drawBatch(Batch& batch);
protected :
std::vector<const Renderable2D*> m_queue;
unsigned int m_IBO;
unsigned int m_VBO;
RenderState m_state;
unsigned short m_size;
ShaderProgram m_textShader;
ShaderProgram m_spriteShader;
};
#endif /* SPECTRE_BATCH_RENDERER2D_H */

View file

@ -0,0 +1,25 @@
#ifndef SPECTRE_DEFAULT_RENDERER2D_H
#define SPECTRE_DEFAULT_RENDERER2D_H
#include <vector>
#include "Renderer2D.h"
class DefaultRenderer2D : public Renderer2D
{
public :
DefaultRenderer2D();
virtual void submit(const Renderable2D& renderable);
virtual void render();
protected :
std::vector<const Renderable2D*> m_queue;
ShaderProgram m_shader;
};
#endif /* SPECTRE_DEFAULT_RENDERER2D_H */

View file

@ -0,0 +1,74 @@
#ifndef SPECTRE_GRAPHCIS_FONT_H
#define SPECTRE_GRAPHCIS_FONT_H
#include <map>
#include <vector>
#include <string>
#include <Spectre/Math/Vector4.h>
#include <Spectre/Math/Vector2.h>
#include <Spectre/Graphics/Texture.h>
class FontDriver;
// TODO: Fixup this api :)
class Font
{
public :
struct Info {
std::string name;
};
struct Glyph {
vec2b size; // Width, Height of bounding box.
vec2b offset; // Offset from cursor where the box begins.
unsigned char advance;
//vec4u tex_coords;
vec2u texture_origin;
const Texture* texture; // Texture atlas.
};
Font();
~Font();
bool loadFromFile(const std::string& filename, unsigned int size = 22);
bool loadFromMemory(const void *data, unsigned int size);
Glyph getGlyph(unsigned int code) const;
const Texture* getTexture() const;
protected :
bool loadData();
void loadChar(unsigned char code) const;
void createTexture();
void resizeTexture() const;
Vector2u findTextureRegion(const Image& img) const;
protected :
FontDriver *m_driver;
mutable std::map<unsigned char, Glyph> m_charset;
unsigned int m_size; // font size, in pixels. not points.
// Texture atlas used by the font.
mutable Texture m_texture;
mutable vec2u m_texpos;
mutable unsigned m_shelf;
std::vector<unsigned char> m_rawData;
};
#endif /* SPECTRE_GRAPHCIS_FONT_H */

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,62 @@
#ifndef SPECTRE_GRAPHICS_IMAGE_H
#define SPECTRE_GRAPHICS_IMAGE_H
#include <string>
#include <vector>
#include <Spectre/Math/Color.h>
#include <Spectre/Math/Vector2.h>
#include <Spectre/Graphics/PixelFormat.h>
class Image
{
public :
Image();
void create(unsigned width, unsigned height, const Color& color = Color::Black);
void create(unsigned width, unsigned height, const void* pixels);
void create(PixelFormat format, unsigned width, unsigned height, const Color& color = Color::Black);
void create(PixelFormat format, unsigned width, unsigned height, const void* pixels);
const Vector2u& getSize() const;
unsigned int getWidth() const;
unsigned int getHeight() const;
unsigned int getBpp() const;
unsigned int getStride() const;
PixelFormat getFormat() const;
// Returns true if the image has a alpha channel.
bool hasAlpha() const;
bool loadFromFile(const std::string& filename);
bool loadFromMemory(const void *data, unsigned int size);
void saveToFile(const std::string& filename) const;
void setPixel(unsigned x, unsigned y, const Color& c);
Color getPixel(unsigned x, unsigned y) const;
void setPixels(const void* pixels);
const unsigned char* getPixels() const;
void flipY();
private :
Vector2u m_size;
PixelFormat m_format;
std::vector<unsigned char> m_pixels;
};
#endif /* SPECTRE_GRAPHICS_IMAGE_H */

View file

@ -0,0 +1,15 @@
#ifndef SPECTRE_OPENGL_H
#define SPECTRE_OPENGL_H
#ifdef _WIN32
#ifdef _MSC_VER
#include <Windows.h>
#endif /* !_MSC_VER */
#include "GL/glew.h"
#include "GL/wglew.h"
#endif /* !_WIN32 */
#endif /* SPECTRE_OPENGL_H */

View file

@ -0,0 +1,12 @@
#ifndef SPECTRE_GRAPHICS_PIXELFORMAT_H
#define SPECTRE_GRAPHICS_PIXELFORMAT_H
enum PixelFormat
{
PF_Unknown = 0,
PF_RGB = 1,
PF_RGBA = 2,
PF_Alpha = 3, // 1 byte alpha channel.
};
#endif SPECTRE_GRAPHICS_PIXELFORMAT_H

View file

@ -0,0 +1,27 @@
#ifndef SPECTRE_GRAPHICS_RENDERSTATE_H
#define SPECTRE_GRAPHICS_RENDERSTATE_H
class ShaderProgram;
class Texture;
class RenderState
{
public :
RenderState();
~RenderState();
void enableTexture(const Texture *texture);
void enableShader(const ShaderProgram *program);
void cleanup();
protected :
const Texture *m_texture;
const ShaderProgram *m_shader;
};
#endif /* SPECTRE_GRAPHICS_RENDERSTATE_H */

View file

@ -0,0 +1,36 @@
#ifndef SPECTRE_GRAPHICS_RENDERABLE_H
#define SPECTRE_GRAPHICS_RENDERABLE_H
#include "Vertex2D.h"
#include "Transformable.h"
#include <Spectre/Math/Matrix4.h>
#include <Spectre/Math/Vector2.h>
#include <vector>
class ShaderProgram;
class Texture;
enum RenderType
{
RenderType_Scene = 0,
RenderType_UI = 1
};
class Renderable2D : public Transformable
{
public :
Renderable2D();
Renderable2D(const Vector2f& position);
virtual ~Renderable2D();
virtual const std::vector<Vertex2D>& getVertices() const = 0;
virtual const std::vector<unsigned short>& getIndices() const = 0;
virtual const Texture* getTexture() const { return NULL; };
virtual const RenderType getRenderType() const = 0;
};
#endif /* SPECTRE_GRAPHCIS_RENDERABLE_H */

View file

@ -0,0 +1,28 @@
#ifndef SPECTRE_GRAPHICS_RENDERER2D_H
#define SPECTRE_GRAPHICS_RENDERER2D_H
#include <Spectre/Graphics/Renderable.h>
#include <Spectre/Graphics/ShaderProgram.h>
#include <Spectre/Scene/Camera2D.h>
class Renderer2D
{
public :
Renderer2D();
virtual ~Renderer2D();
void setCamera(const Camera2D& camera);
virtual void begin() {};
virtual void submit(const Renderable2D& renderable) = 0;
virtual void render() = 0;
protected :
const Camera2D* m_camera;
};
#endif /* SPECTRE_GRAPHICS_RENDERER2D_H */

View file

@ -0,0 +1,54 @@
#ifndef SPECTRE_GRAPHICS_SHADER_H
#define SPECTRE_GRAPHICS_SHADER_H
#include <string>
class Shader
{
public :
enum Type {
Vertex,
Fragment
};
Shader(Type type, const std::string& name = "");
~Shader();
unsigned int getHandle() const;
const std::string& getName() const;
// Load shader from file.
bool loadFromFile(const std::string& file);
// Load shader from memory
bool loadFromMemory(const std::string& source);
const std::string& getError() const;
// Is this shader compiled?
bool isCompiled() const;
protected :
// Compile the shader.
// Returns true if the shader compiled without errors. false otherwise.
bool compile();
std::string fetchErrorLog();
protected :
// Type of shader. Vertex, fragment, geometry etc.
Type m_type;
unsigned int m_handle; // Shader id.
// A name for the shader (usually the filename if loaded from file).
std::string m_name;
std::string m_error;
};
#endif /* SPECTRE_GRAPHICS_SHADER_H */

View file

@ -0,0 +1,86 @@
#ifndef SPECTRE_GRAPHICS_SHADER_PROGRAM_H
#define SPECTRE_GRAPHICS_SHADER_PROGRAM_H
#include <string>
#include <Spectre/Math/Matrix4.h>
#include "Shader.h"
struct Color;
class ShaderProgram
{
public :
ShaderProgram();
~ShaderProgram();
void create();
void destroy();
void addShader(const Shader& shader);
// Load a shader program from file.
//
// The following naming conventions are supported:
// <name>.glsl - Virtual file, will load all real shader files with the same name.
// <name>.vert.glsl - Vertex shader.
// <name>.frag.glsl - Fragment shader.
bool loadFromFile(const std::string& filename);
// Load a shader from file.
bool loadFromFile(const std::string& filename, Shader::Type type);
// Load a shader from memory.
bool loadFromMemory(const std::string& source, Shader::Type type);
// Link the program.
bool link();
// Returns true if the program was linked successfully
bool isLinked() const;
// Enable this shader
// All shader operations after this call will affect this shader.
void enable() const;
// Disable this shader
// Operations will no longer affect this shader after this call.
void disable() const;
// Get the last shader error.
std::string getLastError() const;
// ---------------------
// Variables.
// ---------------------
bool setMVPMatrix(const Matrix4f& matrix);
// ---------------------
// General Variables.
// ---------------------
bool setAttribute(const std::string& name, float value) const;
bool setAttribute(const std::string& name, const Matrix4f& matrix) const;
bool setUniform(const std::string& name, const Matrix4f& matrix) const;
bool setUniform(const std::string& name, const Color& color) const;
protected :
bool getAttribLoc(const std::string& name, int& loc) const;
bool getUniformLoc(const std::string& name, int& loc) const;
std::string fetchErrorLog();
protected :
unsigned int m_id; // program id
mutable std::string m_error;
};
#endif /* SPECTRE_GRAPHICS_SHADER_PROGRAM_H */

View file

@ -0,0 +1,47 @@
#ifndef SPECTRE_GRAPHICS_SPRITE_H
#define SPECTRE_GRAPHICS_SPRITE_H
#include <Spectre/Math/Color.h>
#include "Vertex2D.h"
#include "Renderable.h"
class Texture;
class Sprite : public Renderable2D
{
public :
Sprite();
Sprite(const vec2f& pos, const vec2f& size);
void init();
void setSize(vec2f size);
void setColor(const Color& color);
void setTexture(const Texture& texture);
void setTextureCoords(const vec2u& pos, const vec2u& size);
virtual const Texture* getTexture() const;
virtual const std::vector<Vertex2D>& getVertices() const;
virtual const std::vector<unsigned short>& getIndices() const;
virtual const RenderType getRenderType() const { return RenderType_Scene; };
protected :
void updateGeometry();
std::vector<unsigned short> m_indicies;
std::vector<Vertex2D> m_vertices;
const Texture* m_texture;
};
#endif /* SPECTRE_GRAPHICS_SPRITE_H */

View file

@ -0,0 +1,73 @@
#ifndef SPECTRE_GRAPHICS_TEXT_H
#define SPECTRE_GRAPHICS_TEXT_H
#include <string>
#include <Spectre/Math/Color.h>
#include <Spectre/Graphics/Font.h>
#include <Spectre/Graphics/Transformable.h>
#include <Spectre/Graphics/Renderable.h>
class Text : public Renderable2D
{
public :
/* TODO
enum Properties
{
Regular = 0,
Bold = 1 << 0,
Italic = 1 << 1,
Underline = 1 << 2,
StrikeThrough = 1 << 3
};
enum Alignment
{
Left,
Right,
Center
}; */
Text();
Text(const std::string text, const Font& font);
void setString(const std::string& string);
const std::string& getString() const;
void setFont(const Font& font);
const Font* getFont() const;
void setColor(const Color& color);
const Color& getColor() const;
Vector2f getSize() const;
virtual const std::vector<Vertex2D>& getVertices() const;
virtual const std::vector<unsigned short>& getIndices() const;
virtual const RenderType getRenderType() const { return RenderType_UI; };
virtual const Texture* getTexture() const;
private :
void updateGeometry() const;
private :
// String containing the text.
std::string m_string;
const Font* m_font;
Color m_color;
std::vector<unsigned short> m_indicies;
mutable bool m_geometryNeedsUpdate;
mutable std::vector<Vertex2D> m_vertices;
};
#endif /* SPECTRE_GRAPHICS_TEXT_H */

View file

@ -0,0 +1,91 @@
#ifndef SPECTRE_GRAPHICS_TEXTURE_H
#define SPECTRE_GRAPHICS_TEXTURE_H
#include <Spectre/Core/NonCopyable.h>
#include <Spectre/Math/Vector2.h>
#include <Spectre/Graphics/PixelFormat.h>
#include <Spectre/Graphics/Image.h>
#include <string>
// Basic OpenGL Texture object.
class Texture : public NonCopyable
{
public:
enum Filter {
Nearest = 0x2600,
Linear = 0x2601,
NearestMipmapNearest = 0x2700,
NearestMipmapLinear = 0x2702,
LinearMipmapNearest = 0x2701,
LinearMipmapLinear = 0x2703,
};
enum WrapMode {
Repeat = 0x2901,
RepeatMirror = 0x8370,
ClampToEdge = 0x812F,
ClampToBorder = 0x812D,
};
Texture();
~Texture();
void create(unsigned width, unsigned heigth, PixelFormat format = PixelFormat::PF_RGB);
void create(const Vector2u& size, PixelFormat format = PixelFormat::PF_RGB);
// Create texture from image.
void create(const Image& image);
// Create a texture from a image file on disk.
void create(const std::string& filename, bool isTopDown = true);
// Destroy the texture.
void destroy();
bool isEmpty() const;
const Vector2u& getSize() const;
void update();
void update(vec2u pos, const Image& image);
void update(const Image& image);
// TODO: Fixme
Image copyToImage() const;
void setMinFilter(enum Filter filter);
void setMagFilter(enum Filter filter);
void setSmooth(bool value);
void setWrapMode(enum WrapMode mode);
void setRepeat(bool value);
void enable() const;
void disable() const;
static unsigned int getMaxSize();
// Operators
bool operator<(const Texture* other) const;
bool operator!=(const Texture* other) const;
protected :
unsigned int m_id;
Vector2u m_size;
PixelFormat m_format;
};
#endif /* SPECTRE_GRAPHICS_TEXTURE_H */

View file

@ -0,0 +1,59 @@
#ifndef SPECTRE_GRAPHICS_TRANSFORMABLE_H
#define SPECTRE_GRAPHICS_TRANSFORMABLE_H
#include <Spectre/Math/Transform.h>
#include <Spectre/Math/Vector2.h>
class Transformable
{
public :
Transformable();
Transformable(const Vector2f& position);
virtual ~Transformable();
// Position
void setPosition(const Vector2f& position);
void setPosition(float x, float y);
void move(const Vector2f& offset);
const Vector2f& getPosition() const;
// Rotation
void setRotation(float angle);
void rotate(float angle);
float getRotation() const;
// Scale
void setScale(const Vector2f& offset);
void setScale(float scale);
void scale(const Vector2f& offset);
void scale(float scale);
const Vector2f getScale() const;
const Transform& getTransform() const;
private :
Vector2f m_position;
float m_rotation;
Vector2f m_scale;
mutable bool m_transformNeedsUpdate;
mutable Transform m_transform;
};
#endif /* SPECTRE_GRAPHICS_TRANSFORMABLE_H */

View file

@ -0,0 +1,30 @@
#ifndef SPECTRE_GRAPHICS_VERTEX2D_H
#define SPECTRE_GRAPHICS_VERTEX2D_H
#include <Spectre/Math/Vector2.h>
#include <Spectre/Math/Vector3.h>
// Based on https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/attributes.php
enum VertexAttrib
{
VertexAttribPosition = 0,
VertexAttribColor0 = 3,
VertexAttribColor1 = 4,
VertexAttribTexCoord0 = 8,
};
// Simple POD structure for vertex attributes.
struct Vertex2D
{
Vector2f position;
Vector3f color;
Vector2f uv;
Vertex2D();
Vertex2D(Vector2f pos);
Vertex2D(Vector2f pos, Vector2f uv);
Vertex2D(Vector2f pos, Vector3f color, Vector2f uv = 0.0f);
};
#endif /* SPECTRE_GRAPHICS_VERTEX2D_H */