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

Last change on this file since 1043 was 1043, checked in by sam, 12 years ago

test: start writing tutorials and other shit.

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