zenilib: zenilib/jni/application/modern.h Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
modern.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2 * Copyleft (C) 2013 Mitchell Keith Bloch (bazald).
3 *
4 * This source file is simply under the public domain.
5 */
6
7 #include <zenilib.h>
8
9 #include <memory>
10 #ifndef DISABLE_DX9
11 #include <d3dx9.h>
12 #endif
13
14 #if defined(_DEBUG) && defined(_WINDOWS)
15 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
16 #define new DEBUG_NEW
17 #endif
18
19 using namespace std;
20 using namespace Zeni;
21
22 GLfloat vertices[12] = { 0.0f, 0.0f,
23 0.0f, 300.0f,
24 400.0f, 0.0f};
25 GLuint colors[3] = {0xFF0000FF,
26 0xFF00FF00,
27 0xFFFF0000};
28 GLushort indices[3] = {0, 1, 2};
29
30 class Modern_State : public Gamestate_Base {
31 Modern_State(const Modern_State &);
32 Modern_State operator=(const Modern_State &);
33
34 public:
35 Modern_State()
36 : m_vertex_shader(get_Video().create_Vertex_Shader("shaders/shader.vert")),
37 m_fragment_shader(get_Video().create_Fragment_Shader("shaders/shader.frag")),
38 m_program(get_Video().create_Program()),
39 a_position(0),
40 a_color(0)
41 #ifndef DISABLE_DX9
42 ,
43 vertexDecl(0),
44 vertexBuffer(0),
45 colorBuffer(0),
46 indexBuffer(0)
47 #endif
48 {
49 set_pausable(true);
50
51 m_program->attach(*m_vertex_shader);
52 m_program->attach(*m_fragment_shader);
53 m_program->link();
54
55 memset(buffers, 0, sizeof(buffers));
56
57 if(Program_GL_Fixed * program = dynamic_cast<Program_GL_Fixed *>(m_program.get())) {
58 glGenBuffersARB(3, buffers);
59
60 glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers[0]);
61 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices), vertices, GL_STATIC_DRAW_ARB);
62 a_position = glGetAttribLocationARB(program->get(), "a_position");
63
64 glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers[1]);
65 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(colors), colors, GL_STATIC_DRAW_ARB);
66 a_color = glGetAttribLocationARB(program->get(), "a_color");
67
68 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, buffers[2]);
69 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(indices), indices, GL_STATIC_DRAW_ARB);
70
71 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
72 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
73 }
74 else if(Program_GL_Shader * program = dynamic_cast<Program_GL_Shader *>(m_program.get())) {
75 glGenBuffers(3, buffers);
76
77 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
78 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
79 a_position = glGetAttribLocation(program->get(), "a_position");
80
81 glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
82 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
83 a_color = glGetAttribLocation(program->get(), "a_color");
84
85 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
86 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
87
88 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
89 glBindBuffer(GL_ARRAY_BUFFER, 0);
90 }
91 #ifndef DISABLE_DX9
92 else if(Program_DX9 * program = dynamic_cast<Program_DX9 *>(m_program.get())) {
93 Video_DX9 &vdx = dynamic_cast< Video_DX9 &>(get_Video());
94
95 D3DVERTEXELEMENT9 decl[] = {{0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
96 {1, 0, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
97 D3DDECL_END()};
98 vdx.get_d3d_device()->CreateVertexDeclaration(decl, &vertexDecl);
99
100 void * data;
101 vdx.get_d3d_device()->CreateVertexBuffer(sizeof(vertices), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &vertexBuffer, 0);
102 if(FAILED(vertexBuffer->Lock(0, 0, &data, 0)))
103 throw Error("Lock failed.");
104 memcpy(data, vertices, sizeof(vertices));
105 vertexBuffer->Unlock();
106
107 vdx.get_d3d_device()->CreateVertexBuffer(sizeof(colors), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &colorBuffer, 0);
108 if(FAILED(colorBuffer->Lock(0, 0, &data, 0)))
109 throw Error("Lock failed.");
110 memcpy(data, colors, sizeof(colors));
111 colorBuffer->Unlock();
112
113 vdx.get_d3d_device()->CreateIndexBuffer(sizeof(indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &indexBuffer, 0);
114 if(FAILED(indexBuffer->Lock(0, 0, &data, 0)))
115 throw Error("Lock failed.");
116 memcpy(data, indices, sizeof(indices));
117 indexBuffer->Unlock();
118 }
119 #endif
120 }
121
122 ~Modern_State() {
123 #ifndef DISABLE_DX9
124 if(dynamic_cast<Video_DX9 *>(&get_Video())) {
125 if(indexBuffer)
126 indexBuffer->Release();
127 if(colorBuffer)
128 colorBuffer->Release();
129 if(vertexBuffer)
130 vertexBuffer->Release();
131 if(vertexDecl)
132 vertexDecl->Release();
133 }
134 else
135 #endif
136 glDeleteBuffers(3, buffers);
137 }
138
139 private:
140 void render() {
141 Video &vr = get_Video();
142 vr.set_backface_culling(false);
143
144 const Matrix4f modelViewMatrix = Matrix4f::Orthographic(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);
145 const bool use_shaders = get_Game().get_key_state(SDLK_SPACE);
146
147 if(use_shaders) {
148 vr.set_program(*m_program);
149
150 if(Program_GL_Fixed * program = dynamic_cast<Program_GL_Fixed *>(m_program.get())) {
151 GLuint modelViewProj = glGetUniformLocationARB(program->get(), "modelViewProj");
152 glUniformMatrix4fvARB(modelViewProj, 1, GL_FALSE, reinterpret_cast<const GLfloat *>(&modelViewMatrix));
153
154 glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers[0]);
155 glVertexAttribPointerARB(a_position, 2, GL_FLOAT, GL_FALSE, 0, 0);
156 glEnableVertexAttribArrayARB(a_position);
157
158 glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffers[1]);
159 glVertexAttribPointerARB(a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
160 glEnableVertexAttribArrayARB(a_color);
161
162 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, buffers[2]);
163 glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_SHORT, 0);
164
165 glDisableVertexAttribArrayARB(a_position);
166 glDisableVertexAttribArrayARB(a_color);
167 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
168 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
169 }
170 else if(Program_GL_Shader * program = dynamic_cast<Program_GL_Shader *>(m_program.get())) {
171 GLuint modelViewProj = glGetUniformLocation(program->get(), "modelViewProj");
172 glUniformMatrix4fv(modelViewProj, 1, GL_FALSE, reinterpret_cast<const GLfloat *>(&modelViewMatrix));
173
174 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
175 glVertexAttribPointer(a_position, 2, GL_FLOAT, GL_FALSE, 0, 0);
176 glEnableVertexAttribArray(a_position);
177
178 glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
179 glVertexAttribPointer(a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
180 glEnableVertexAttribArray(a_color);
181
182 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
183 glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_SHORT, 0);
184
185 glDisableVertexAttribArray(a_position);
186 glDisableVertexAttribArray(a_color);
187 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
188 glBindBuffer(GL_ARRAY_BUFFER, 0);
189 }
190 #ifndef DISABLE_DX9
191 else if(Program_DX9 * program = dynamic_cast<Program_DX9 *>(m_program.get())) {
192 Video_DX9 &vdx = dynamic_cast< Video_DX9 &>(get_Video());
193 Shader_DX9 &vsdx = dynamic_cast< Shader_DX9 &>(*m_vertex_shader.get());
194
195 if(FAILED(vsdx.get_constant_table()->SetMatrix(vdx.get_d3d_device(), "$_modelViewProj", reinterpret_cast<const D3DXMATRIX *>(&modelViewMatrix))))
196 throw Error("Setting modelViewProj failed.");
197 }
198 #endif
199 }
200 else {
201 vr.set_view_matrix(modelViewMatrix);
202 vr.set_projection_matrix(Matrix4f::Identity());
203 }
204
205 #ifndef DISABLE_DX9
206 if(Program_DX9 * program = dynamic_cast<Program_DX9 *>(m_program.get())) {
207 Video_DX9 &vdx = dynamic_cast< Video_DX9 &>(get_Video());
208
209 vdx.get_d3d_device()->SetVertexDeclaration(vertexDecl);
210 vdx.get_d3d_device()->SetStreamSource(0, vertexBuffer, 0, 2 * sizeof(float));
211 vdx.get_d3d_device()->SetStreamSource(1, colorBuffer, 0, 4 * sizeof(unsigned char));
212 vdx.get_d3d_device()->SetIndices(indexBuffer);
213
214 vdx.get_d3d_device()->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, 3, 0, 1);
215
216 vdx.get_d3d_device()->SetIndices(0);
217 vdx.get_d3d_device()->SetStreamSource(0, 0, 0, 0);
218 vdx.get_d3d_device()->SetStreamSource(1, 0, 0, 0);
219 vdx.get_d3d_device()->SetVertexDeclaration(0);
220 }
221 else
222 #endif
223 if(!use_shaders) {
224 glEnableClientState(GL_VERTEX_ARRAY);
225 glVertexPointer(2, GL_FLOAT, 0, vertices);
226 glEnableClientState(GL_COLOR_ARRAY);
227 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
228
229 glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_SHORT, indices);
230
231 glDisableClientState(GL_VERTEX_ARRAY);
232 glDisableClientState(GL_COLOR_ARRAY);
233 }
234
235 if(use_shaders)
236 vr.unset_program();
237 }
238
239 auto_ptr<Shader> m_vertex_shader;
240 auto_ptr<Shader> m_fragment_shader;
241 auto_ptr<Program> m_program;
242 GLuint a_position;
243 GLuint a_color;
244
245 GLuint buffers[3];
246 #ifndef DISABLE_DX9
247 LPDIRECT3DVERTEXDECLARATION9 vertexDecl;
248 LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
249 LPDIRECT3DVERTEXBUFFER9 colorBuffer;
250 LPDIRECT3DINDEXBUFFER9 indexBuffer;
251 #endif
252 };
GL_TRUE
#define GL_TRUE
Definition: gl2.h:51
glGenBuffersARB
#define glGenBuffersARB
Definition: glew.h:6914
GL_STATIC_DRAW_ARB
#define GL_STATIC_DRAW_ARB
Definition: glew.h:6888
D3DPT_TRIANGLESTRIP
Definition: d3d9types.h:308
GLushort
unsigned short GLushort
Definition: gl2.h:31
Zeni::Shader_DX9::get_constant_table
LPD3DXCONSTANTTABLE get_constant_table() const
Definition: Shader.hxx:53
D3DPOOL_MANAGED
Definition: d3d9types.h:1489
Zeni::Video_DX9
The Direct3D9 Rendering System.
Definition: Video_DX9.h:62
GL_FALSE
#define GL_FALSE
Definition: gl2.h:50
Zeni::Video_DX9::get_d3d_device
LPDIRECT3DDEVICE9 & get_d3d_device()
See DirectX Documentation for details.
Definition: Video_DX9.hxx:48
Zeni::Shader_DX9
Definition: Shader.h:203
GL_VERTEX_ARRAY
#define GL_VERTEX_ARRAY
Definition: glew_head.h:723
Modern_State::~Modern_State
~Modern_State()
Definition: modern.h:122
D3DDECLTYPE_UBYTE4N
Definition: d3d9types.h:747
Zeni::Video
The Video Rendering Singleton.
Definition: Video.h:71
D3DUSAGE_WRITEONLY
#define D3DUSAGE_WRITEONLY
Definition: d3d9types.h:1609
f
GLclampf f
Definition: glew.h:3390
D3DFMT_INDEX16
Definition: d3d9types.h:1409
Zeni::Video::set_view_matrix
virtual void set_view_matrix(const Matrix4f &view)=0
Set the view Matrix4f.
Definition: Video.cpp:226
Zeni::Game::get_key_state
bool get_key_state(const int &key) const
Get the state of a key.
Definition: Game.cpp:287
glEnableVertexAttribArray
GL_APICALL void GL_APIENTRY glEnableVertexAttribArray(GLuint index)
GL_UNSIGNED_SHORT
#define GL_UNSIGNED_SHORT
Definition: gl2.h:238
D3DDECLUSAGE_POSITION
Definition: d3d9types.h:697
D3DDECLMETHOD_DEFAULT
Definition: d3d9types.h:719
glGetAttribLocation
GL_APICALL int GL_APIENTRY glGetAttribLocation(GLuint program, const GLchar *name)
Zeni::render
static void render(const Vertex_Buffer_Macrorenderer &macrorenderer, std::vector< Vertex_Buffer::Vertex_Buffer_Range * > &descriptors)
Definition: Vertex_Buffer.cpp:581
zenilib.h
memset
#define memset
Definition: SDL_malloc.c:633
glGetUniformLocation
GL_APICALL int GL_APIENTRY glGetUniformLocation(GLuint program, const GLchar *name)
GL_TRIANGLE_STRIP
#define GL_TRIANGLE_STRIP
Definition: gl2.h:59
Zeni::get_Game
Game & get_Game()
Get access to the singleton.
Definition: Game.cpp:58
Zeni::Video::unset_program
virtual void unset_program()=0
Disable a program.
Modern_State::Modern_State
Modern_State()
Definition: modern.h:35
d3dx9.h
GL_ARRAY_BUFFER
#define GL_ARRAY_BUFFER
Definition: gl2.h:115
glUniformMatrix4fv
GL_APICALL void GL_APIENTRY glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
Modern_State
Definition: modern.h:30
GL_ELEMENT_ARRAY_BUFFER
#define GL_ELEMENT_ARRAY_BUFFER
Definition: gl2.h:116
GL_COLOR_ARRAY
#define GL_COLOR_ARRAY
Definition: glew_head.h:725
buffers
const GLuint * buffers
Definition: glew.h:1669
Zeni::Program_DX9
Definition: Shader.h:224
GL_ELEMENT_ARRAY_BUFFER_ARB
#define GL_ELEMENT_ARRAY_BUFFER_ARB
Definition: glew.h:6866
glGetAttribLocationARB
#define glGetAttribLocationARB
Definition: glew.h:7160
glBindBuffer
GL_APICALL void GL_APIENTRY glBindBuffer(GLenum target, GLuint buffer)
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
glGenBuffers
GL_APICALL void GL_APIENTRY glGenBuffers(GLsizei n, GLuint *buffers)
GL_STATIC_DRAW
#define GL_STATIC_DRAW
Definition: gl2.h:121
D3DDECLUSAGE_COLOR
Definition: d3d9types.h:707
LPDIRECT3DVERTEXBUFFER9
struct IDirect3DVertexBuffer9 * LPDIRECT3DVERTEXBUFFER9
Definition: d3d9.h:1507
glVertexAttribPointerARB
#define glVertexAttribPointerARB
Definition: glew.h:7135
glBindBufferARB
#define glBindBufferARB
Definition: glew.h:6910
program
GLbitfield GLuint program
Definition: gl2ext.h:1203
GLfloat
khronos_float_t GLfloat
Definition: gl2.h:33
glUniformMatrix4fvARB
#define glUniformMatrix4fvARB
Definition: glew.h:5697
glDisableClientState
#define glDisableClientState
Definition: gl_mangle.h:390
glVertexAttribPointer
GL_APICALL void GL_APIENTRY glVertexAttribPointer(GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *ptr)
SDLK_SPACE
Definition: SDL_keycode.h:55
D3DDECL_END
#define D3DDECL_END()
Definition: d3d9types.h:774
glColorPointer
#define glColorPointer
Definition: gl_mangle.h:236
Zeni::Video::set_projection_matrix
virtual void set_projection_matrix(const Matrix4f &projection)=0
Set the projection Matrix4f.
Definition: Video.cpp:230
glBufferDataARB
#define glBufferDataARB
Definition: glew.h:6911
GLuint
unsigned int GLuint
Definition: gl2.h:32
Zeni::Program_GL_Fixed
Definition: Shader.h:135
memcpy
#define memcpy
Definition: SDL_malloc.c:634
glEnableClientState
#define glEnableClientState
Definition: gl_mangle.h:438
Zeni::Gamestate_Base
The base class for all gamestates.
Definition: Gamestate.h:82
GL_UNSIGNED_BYTE
#define GL_UNSIGNED_BYTE
Definition: gl2.h:236
D3DDECLTYPE_FLOAT2
Definition: d3d9types.h:735
_D3DVERTEXELEMENT9
Definition: d3d9types.h:761
LPDIRECT3DVERTEXDECLARATION9
struct IDirect3DVertexDeclaration9 * LPDIRECT3DVERTEXDECLARATION9
Definition: d3d9.h:1001
Zeni::Video::set_backface_culling
virtual void set_backface_culling(const bool &on)=0
Set backface culling on/off.
Definition: Video.cpp:186
glDrawElements
#define glDrawElements
Definition: gl_mangle.h:416
GL_FLOAT
#define GL_FLOAT
Definition: gl2.h:241
Zeni::get_Video
Video & get_Video()
Get access to the singleton.
Definition: Video.cpp:149
glGetUniformLocationARB
#define glGetUniformLocationARB
Definition: glew.h:5674
Zeni::Matrix4f
A Featureful 4-Space Matrix Class.
Definition: Matrix4f.h:47
glVertexPointer
#define glVertexPointer
Definition: gl_mangle.h:2196
Zeni::Error
The Error Class.
Definition: Error.h:52
glDisableVertexAttribArray
GL_APICALL void GL_APIENTRY glDisableVertexAttribArray(GLuint index)
Zeni::Video::set_program
virtual void set_program(Program &program)=0
Enable a program.
glDeleteBuffers
GL_APICALL void GL_APIENTRY glDeleteBuffers(GLsizei n, const GLuint *buffers)
vertices
GLfloat vertices[12]
Definition: modern.h:22
glBufferData
GL_APICALL void GL_APIENTRY glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage)
GL_ARRAY_BUFFER_ARB
#define GL_ARRAY_BUFFER_ARB
Definition: glew.h:6865
colors
GLuint colors[3]
Definition: modern.h:25
glEnableVertexAttribArrayARB
#define glEnableVertexAttribArrayARB
Definition: glew.h:7077
Zeni::Program_GL_Shader
Definition: Shader.h:176
glDisableVertexAttribArrayARB
#define glDisableVertexAttribArrayARB
Definition: glew.h:7076
LPDIRECT3DINDEXBUFFER9
struct IDirect3DIndexBuffer9 * LPDIRECT3DINDEXBUFFER9
Definition: d3d9.h:1579
indices
GLsizei GLenum const GLvoid * indices
Definition: gl2ext.h:1012
_D3DMATRIX
Definition: d3d9types.h:80