source: trunk/test/tutorial/tut02.cpp @ 1189

Last change on this file since 1189 was 1179, checked in by sam, 10 years ago

win32: add a lot of DirectX stubs.

  • Property svn:keywords set to Id
File size: 5.8 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 USE_SDL && defined __APPLE__
23#   include <SDL_main.h>
24#endif
25
26#if defined _WIN32
27#   undef main /* FIXME: still needed? */
28#   include <direct.h>
29#endif
30
31class Cube : public WorldEntity
32{
33public:
34    Cube()
35    {
36        m_angle = 0;
37
38        /* Front */
39        m_vertices[0] = vec3(-1.0, -1.0,  1.0);
40        m_vertices[1] = vec3( 1.0, -1.0,  1.0);
41        m_vertices[2] = vec3( 1.0,  1.0,  1.0);
42        m_vertices[3] = vec3(-1.0,  1.0,  1.0);
43        /* Back */
44        m_vertices[4] = vec3(-1.0, -1.0, -1.0);
45        m_vertices[5] = vec3( 1.0, -1.0, -1.0);
46        m_vertices[6] = vec3( 1.0,  1.0, -1.0);
47        m_vertices[7] = vec3(-1.0,  1.0, -1.0);
48
49        m_colors[0] = vec3(1.0, 0.0, 0.0);
50        m_colors[1] = vec3(0.0, 1.0, 0.0);
51        m_colors[2] = vec3(0.0, 0.0, 1.0);
52        m_colors[3] = vec3(1.0, 1.0, 1.0);
53        m_colors[4] = vec3(1.0, 0.0, 0.0);
54        m_colors[5] = vec3(0.0, 1.0, 0.0);
55        m_colors[6] = vec3(0.0, 0.0, 1.0);
56        m_colors[7] = vec3(1.0, 1.0, 1.0);
57
58        m_indices[0] = i16vec3(0, 1, 2);
59        m_indices[1] = i16vec3(2, 3, 0);
60        m_indices[2] = i16vec3(1, 5, 6);
61        m_indices[3] = i16vec3(6, 2, 1);
62        m_indices[4] = i16vec3(7, 6, 5);
63        m_indices[5] = i16vec3(5, 4, 7);
64        m_indices[6] = i16vec3(4, 0, 3);
65        m_indices[7] = i16vec3(3, 7, 4);
66        m_indices[8] = i16vec3(4, 5, 1);
67        m_indices[9] = i16vec3(1, 0, 4);
68        m_indices[10] = i16vec3(3, 2, 6);
69        m_indices[11] = i16vec3(6, 7, 3);
70
71        m_ready = false;
72    }
73
74    virtual void TickGame(float deltams)
75    {
76        WorldEntity::TickGame(deltams);
77
78        m_angle += deltams / 1000.0f * 45.0f;
79
80        mat4 anim = mat4::rotate(m_angle, vec3(0, 1, 0));
81        mat4 model = mat4::translate(vec3(0, 0, -4));
82        mat4 view = mat4::lookat(vec3(0, 2, 0), vec3(0, 0, -4), vec3(0, 1, 0));
83        mat4 proj = mat4::perspective(45.0f, 640.0f, 480.0f, 0.1f, 10.0f);
84
85        m_matrix = proj * view * model * anim;
86    }
87
88    virtual void TickDraw(float deltams)
89    {
90        WorldEntity::TickDraw(deltams);
91
92        if (!m_ready)
93        {
94            m_shader = Shader::Create(
95                "#version 120\n"
96                "attribute vec3 in_Vertex;"
97                "attribute vec3 in_Color;"
98                "uniform mat4 in_Matrix;"
99                "varying vec3 pass_Color;"
100                ""
101                "void main(void) {"
102                "    gl_Position = in_Matrix * vec4(in_Vertex, 1.0);"
103                "    pass_Color = in_Color;"
104                "}",
105
106                "#version 120\n"
107                "varying vec3 pass_Color;"
108                ""
109                "void main(void) {"
110                "    gl_FragColor = vec4(pass_Color, 1.0);"
111                "}");
112            m_coord = m_shader->GetAttribLocation("in_Vertex");
113            m_color = m_shader->GetAttribLocation("in_Color");
114            m_mvp = m_shader->GetUniformLocation("in_Matrix");
115            m_ready = true;
116
117#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
118            /* Method 1: store vertex buffer on the GPU memory */
119            glGenBuffers(1, &m_vbo);
120            glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
121            glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices,
122                         GL_STATIC_DRAW);
123            glGenBuffers(1, &m_cbo);
124            glBindBuffer(GL_ARRAY_BUFFER, m_cbo);
125            glBufferData(GL_ARRAY_BUFFER, sizeof(m_colors), m_colors,
126                         GL_STATIC_DRAW);
127            glGenBuffers(1, &m_ibo);
128            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
129            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_indices), m_indices,
130                         GL_STATIC_DRAW);
131#else
132#endif
133
134            /* FIXME: this object never cleans up */
135        }
136
137        m_shader->Bind();
138        m_shader->SetUniform(m_mvp, m_matrix);
139#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
140        glEnableVertexAttribArray(m_coord);
141        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
142        glVertexAttribPointer(m_coord, 3, GL_FLOAT, GL_FALSE, 0, 0);
143
144        glEnableVertexAttribArray(m_color);
145        glBindBuffer(GL_ARRAY_BUFFER, m_cbo);
146        glVertexAttribPointer(m_color, 3, GL_FLOAT, GL_FALSE, 0, 0);
147
148        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
149        int size;
150        glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
151
152
153        glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);
154
155        glDisableVertexAttribArray(m_coord);
156        glDisableVertexAttribArray(m_color);
157        glBindBuffer(GL_ARRAY_BUFFER, 0);
158#else
159        glEnableClientState(GL_VERTEX_ARRAY);
160        glVertexPointer(3, GL_FLOAT, 0, m_vertices);
161        glDisableClientState(GL_VERTEX_ARRAY);
162#endif
163    }
164
165private:
166    float m_angle;
167    mat4 m_matrix;
168    vec3 m_vertices[8];
169    vec3 m_colors[8];
170    i16vec3 m_indices[12];
171    Shader *m_shader;
172#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
173    GLuint m_vbo, m_cbo, m_ibo;
174#endif
175    int m_coord, m_color, m_mvp;
176    bool m_ready;
177};
178
179int main(int argc, char **argv)
180{
181    Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f);
182
183#if defined _MSC_VER
184    _chdir("..");
185#elif defined _WIN32
186    _chdir("../..");
187#endif
188
189    new DebugFps(5, 5);
190    new Cube();
191
192    app.Run();
193
194    return EXIT_SUCCESS;
195}
196
Note: See TracBrowser for help on using the repository browser.