source: trunk/test/tutorial/02_cube.cpp @ 1242

Last change on this file since 1242 was 1242, checked in by sam, 8 years ago

tutorial: some refactoring in the tutorials.

  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1//
2// Lol Engine - Cube tutorial
3//
4// Copyright: (c) 2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lolgl.h"
17#include "loldebug.h"
18
19using namespace std;
20using namespace lol;
21
22#if defined _WIN32 && defined USE_D3D9
23#   define FAR
24#   define NEAR
25#   include <d3d9.h>
26#endif
27
28#if USE_SDL && defined __APPLE__
29#   include <SDL_main.h>
30#endif
31
32#if defined _WIN32
33#   undef main /* FIXME: still needed? */
34#   include <direct.h>
35#endif
36
37#if defined USE_D3D9
38extern IDirect3DDevice9 *g_d3ddevice;
39#elif defined _XBOX
40extern D3DDevice *g_d3ddevice;
41#endif
42
43class Cube : public WorldEntity
44{
45public:
46    Cube()
47    {
48        m_angle = 0;
49
50        /* Front vertices/colors */
51        m_mesh[0] = vec3(-1.0, -1.0,  1.0); m_mesh[1] = vec3(1.0, 0.0, 1.0);
52        m_mesh[2] = vec3( 1.0, -1.0,  1.0); m_mesh[3] = vec3(0.0, 1.0, 0.0);
53        m_mesh[4] = vec3( 1.0,  1.0,  1.0); m_mesh[5] = vec3(1.0, 0.5, 0.0);
54        m_mesh[6] = vec3(-1.0,  1.0,  1.0); m_mesh[7] = vec3(1.0, 1.0, 0.0);
55        /* Back */
56        m_mesh[8]  = vec3(-1.0, -1.0, -1.0); m_mesh[9]  = vec3(1.0, 0.0, 0.0);
57        m_mesh[10] = vec3( 1.0, -1.0, -1.0); m_mesh[11] = vec3(0.0, 0.5, 0.0);
58        m_mesh[12] = vec3( 1.0,  1.0, -1.0); m_mesh[13] = vec3(0.0, 0.5, 1.0);
59        m_mesh[14] = vec3(-1.0,  1.0, -1.0); m_mesh[15] = vec3(0.0, 0.0, 1.0);
60
61        m_indices[0] = i16vec3(0, 1, 2);
62        m_indices[1] = i16vec3(2, 3, 0);
63        m_indices[2] = i16vec3(1, 5, 6);
64        m_indices[3] = i16vec3(6, 2, 1);
65        m_indices[4] = i16vec3(7, 6, 5);
66        m_indices[5] = i16vec3(5, 4, 7);
67        m_indices[6] = i16vec3(4, 0, 3);
68        m_indices[7] = i16vec3(3, 7, 4);
69        m_indices[8] = i16vec3(4, 5, 1);
70        m_indices[9] = i16vec3(1, 0, 4);
71        m_indices[10] = i16vec3(3, 2, 6);
72        m_indices[11] = i16vec3(6, 7, 3);
73
74        m_ready = false;
75    }
76
77    virtual void TickGame(float deltams)
78    {
79        WorldEntity::TickGame(deltams);
80
81        m_angle += deltams / 1000.0f * 45.0f;
82
83        mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
84        mat4 model = mat4::translate(vec3(0, 0, -4.5));
85        mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
86        mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
87
88        m_matrix = proj * view * model * anim;
89    }
90
91    virtual void TickDraw(float deltams)
92    {
93        WorldEntity::TickDraw(deltams);
94
95        if (!m_ready)
96        {
97            m_shader = Shader::Create(
98#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
99                "#version 120\n"
100                "attribute vec3 in_Vertex;"
101                "attribute vec3 in_Color;"
102                "uniform mat4 in_Matrix;"
103                "varying vec3 pass_Color;"
104                ""
105                "void main(void) {"
106                "    gl_Position = in_Matrix * vec4(in_Vertex, 1.0);"
107                "    pass_Color = in_Color;"
108                "}",
109
110                "#version 120\n"
111                "varying vec3 pass_Color;"
112                ""
113                "void main(void) {"
114                "    gl_FragColor = vec4(pass_Color, 1.0);"
115                "}"
116#else
117                "void main(float3 in_Vertex : POSITION,"
118                "          float3 in_Color : COLOR,"
119                "          uniform float4x4 in_Matrix,"
120                "          out float4 out_Position : POSITION,"
121                "          out float3 pass_Color : COLOR) {"
122                "    pass_Color = in_Color;"
123                "    out_Position = mul(in_Matrix, float4(in_Vertex, 1.0));"
124                "}",
125
126                "void main(float3 pass_Color : COLOR,"
127                "          out float4 out_FragColor : COLOR) {"
128                "    out_FragColor = float4(pass_Color, 1.0);"
129                "}"
130#endif
131            );
132            m_mvp = m_shader->GetUniformLocation("in_Matrix");
133            m_coord = m_shader->GetAttribLocation("in_Vertex",
134                                                  VertexUsage::Position, 0);
135            m_color = m_shader->GetAttribLocation("in_Color",
136                                                  VertexUsage::Color, 0);
137
138            m_vdecl =
139              new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
140                                                            VertexUsage::Color));
141
142            m_vbo = new VertexBuffer(sizeof(m_mesh));
143            void *mesh = m_vbo->Lock(0, 0);
144            memcpy(mesh, m_mesh, sizeof(m_mesh));
145            m_vbo->Unlock();
146
147#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
148            /* Method 1: store vertex buffer on the GPU memory */
149            glGenBuffers(1, &m_ibo);
150            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
151            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_indices), m_indices,
152                         GL_STATIC_DRAW);
153#elif defined _XBOX || defined USE_D3D9
154            int16_t *indices;
155            if (FAILED(g_d3ddevice->CreateIndexBuffer(sizeof(m_indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL)))
156                Abort();
157            if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0)))
158                Abort();
159            memcpy(indices, m_indices, sizeof(m_indices));
160            m_ibo->Unlock();
161#else
162            /* TODO */
163#endif
164
165            /* FIXME: this object never cleans up */
166            m_ready = true;
167        }
168
169        Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
170
171        m_shader->Bind();
172        m_shader->SetUniform(m_mvp, m_matrix);
173        m_vdecl->SetStream(m_vbo, m_coord, m_color);
174        m_vdecl->Bind();
175
176#if defined _XBOX || defined USE_D3D9
177        if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
178            Abort();
179        if (FAILED(g_d3ddevice->SetIndices(m_ibo)))
180            Abort();
181        if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, sizeof(m_indices) / sizeof(*m_indices))))
182            Abort();
183#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
184        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
185        int size;
186        glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
187
188
189        glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
190
191        glBindBuffer(GL_ARRAY_BUFFER, 0);
192#else
193        /* FIXME */
194        glEnableClientState(GL_VERTEX_ARRAY);
195        glVertexPointer(3, GL_FLOAT, 0, m_vertices);
196        glDisableClientState(GL_VERTEX_ARRAY);
197#endif
198        m_vdecl->Unbind();
199    }
200
201private:
202    float m_angle;
203    mat4 m_matrix;
204    vec3 m_mesh[16];
205    i16vec3 m_indices[12];
206    Shader *m_shader;
207    ShaderAttrib m_coord, m_color;
208    ShaderUniform m_mvp;
209    VertexDeclaration *m_vdecl;
210    VertexBuffer *m_vbo;
211#if defined USE_D3D9
212    IDirect3DIndexBuffer9 *m_ibo;
213#elif defined _XBOX
214    D3DIndexBuffer *m_ibo;
215#else
216#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
217    GLuint m_ibo;
218#endif
219#endif
220    bool m_ready;
221};
222
223int main(int argc, char **argv)
224{
225    Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
226
227#if defined _MSC_VER && !defined _XBOX
228    _chdir("..");
229#elif defined _WIN32 && !defined _XBOX
230    _chdir("../..");
231#endif
232
233    new DebugFps(5, 5);
234    new Cube();
235
236    app.Run();
237
238    return EXIT_SUCCESS;
239}
240
Note: See TracBrowser for help on using the repository browser.