DisplayObject.hpp
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
TE4 - T-Engine 4
Copyright (C) 2009 - 2015 Nicolas Casalini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nicolas Casalini "DarkGod"
darkgod@te4.org
*/
#ifndef DISPLAYOBJECTS_H
#define DISPLAYOBJECTS_H
extern "C" {
#include "tgl.h"
#include "useshader.h"
#include "font.h"
}
#include <vector>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "glm/ext.hpp"
using namespace glm;
using namespace std;
typedef struct {
vec4 pos;
vec2 tex;
vec4 color;
} vertex;
class Renderer;
class DisplayObject;
class DisplayObject {
private:
int lua_ref = LUA_NOREF;
DisplayObject *parent = NULL;
protected:
lua_State *L = NULL;
mat4 model;
float x = 0, y = 0, z = 0;
float rot_x = 0, rot_y = 0, rot_z = 0;
float scale_x = 1, scale_y = 1, scale_z = 1;
bool changed = false;
public:
DisplayObject() { model = mat4(); };
virtual ~DisplayObject() {
if (lua_ref != LUA_NOREF && L) luaL_unref(L, LUA_REGISTRYINDEX, lua_ref);
};
void setLuaState(lua_State *L) { this->L = L; };
void setLuaRef(int ref) {lua_ref = ref; };
int unsetLuaRef() { int ref = lua_ref; lua_ref = LUA_NOREF; return ref; };
void setParent(DisplayObject *parent) { this->parent = parent; };
void setChanged();
bool isChanged() { return changed; };
void resetChanged() { changed = false; };
void recomputeModelMatrix();
void translate(float x, float y, float z, bool increment);
void rotate(float x, float y, float z, bool increment);
void scale(float x, float y, float z, bool increment);
};
class DOVertexes : public DisplayObject{
public:
vector<vertex> vertices;
int tex_lua_ref = LUA_NOREF;
GLuint tex;
shader_type *shader;
DOVertexes() {
vertices.reserve(4);
tex = 0;
shader = default_shader;
};
virtual ~DOVertexes() {
if (tex_lua_ref != LUA_NOREF && L) luaL_unref(L, LUA_REGISTRYINDEX, tex_lua_ref);
};
void clear();
int addQuad(
float x1, float y1, float u1, float v1,
float x2, float y2, float u2, float v2,
float x3, float y3, float u3, float v3,
float x4, float y4, float u4, float v4,
float r, float g, float b, float a
);
void setTexture(GLuint tex, int lua_ref) {
if (tex_lua_ref != LUA_NOREF && L) luaL_unref(L, LUA_REGISTRYINDEX, tex_lua_ref);
this->tex = tex;
tex_lua_ref = lua_ref;
};
void setShader(shader_type *s) { shader = s; };
};
class DOContainer : public DisplayObject{
protected:
vector<DisplayObject*> dos;
public:
DOContainer() {};
virtual ~DOContainer() {};
void add(DisplayObject *dob);
void remove(DisplayObject *dob);
void clear();
};
#endif