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

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

test: fix OS X compilation; we still need SDLmain.a on that platform.

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