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

Last change on this file since 1172 was 1172, checked in by sam, 9 years ago

test: fix logic bugs in two tutorials.

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