1 | // |
---|
2 | // Lol Engine - Triangle 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 | |
---|
19 | using namespace std; |
---|
20 | using 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 | |
---|
30 | class Triangle : public WorldEntity |
---|
31 | { |
---|
32 | public: |
---|
33 | Triangle() |
---|
34 | { |
---|
35 | m_vertices[0] = vec2( 0.0, 0.8); |
---|
36 | m_vertices[1] = vec2(-0.8, -0.8); |
---|
37 | m_vertices[2] = vec2( 0.8, -0.8); |
---|
38 | } |
---|
39 | |
---|
40 | virtual void TickDraw(float deltams) |
---|
41 | { |
---|
42 | WorldEntity::TickDraw(deltams); |
---|
43 | |
---|
44 | if (!m_ready) |
---|
45 | { |
---|
46 | m_shader = Shader::Create( |
---|
47 | "#version 120\n" |
---|
48 | "attribute vec2 coord2d;" |
---|
49 | "void main(void) {" |
---|
50 | " gl_Position = vec4(coord2d, 0.0, 1.0);" |
---|
51 | "}", |
---|
52 | |
---|
53 | "#version 120\n" |
---|
54 | "void main(void) {" |
---|
55 | " gl_FragColor[0] = 0.0;" |
---|
56 | " gl_FragColor[1] = 0.0;" |
---|
57 | " gl_FragColor[2] = 1.0;" |
---|
58 | "}"); |
---|
59 | m_attrib = m_shader->GetAttribLocation("coord2d"); |
---|
60 | m_ready = true; |
---|
61 | |
---|
62 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
63 | /* Method 1: store vertex buffer on the GPU memory */ |
---|
64 | glGenBuffers(1, &m_vbo); |
---|
65 | glBindBuffer(GL_ARRAY_BUFFER, m_vbo); |
---|
66 | glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices, |
---|
67 | GL_STATIC_DRAW); |
---|
68 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
69 | /* Method 2: upload vertex information at each frame */ |
---|
70 | #else |
---|
71 | #endif |
---|
72 | |
---|
73 | /* FIXME: this object never cleans up */ |
---|
74 | } |
---|
75 | |
---|
76 | m_shader->Bind(); |
---|
77 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
78 | glBindBuffer(GL_ARRAY_BUFFER, m_vbo); |
---|
79 | glEnableVertexAttribArray(m_attrib); |
---|
80 | glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
81 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
82 | /* Never used for now */ |
---|
83 | glEnableVertexAttribArray(m_attrib); |
---|
84 | glVertexAttribPointer(m_attrib, 2, GL_FLOAT, GL_FALSE, 0, m_vertices); |
---|
85 | #else |
---|
86 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
87 | glVertexPointer(3, GL_FLOAT, 0, m_vertices); |
---|
88 | #endif |
---|
89 | |
---|
90 | glDrawArrays(GL_TRIANGLES, 0, 3); |
---|
91 | |
---|
92 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
93 | glDisableVertexAttribArray(m_attrib); |
---|
94 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
95 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
96 | /* Never used for now */ |
---|
97 | glDisableVertexAttribArray(m_attrib); |
---|
98 | #else |
---|
99 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
100 | #endif |
---|
101 | } |
---|
102 | |
---|
103 | private: |
---|
104 | vec2 m_vertices[3]; |
---|
105 | Shader *m_shader; |
---|
106 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ |
---|
107 | GLuint m_vbo; |
---|
108 | #endif |
---|
109 | int m_attrib; |
---|
110 | bool m_ready; |
---|
111 | }; |
---|
112 | |
---|
113 | int main() |
---|
114 | { |
---|
115 | Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f); |
---|
116 | |
---|
117 | new DebugFps(5, 5); |
---|
118 | new Triangle(); |
---|
119 | |
---|
120 | app.Run(); |
---|
121 | |
---|
122 | return EXIT_SUCCESS; |
---|
123 | } |
---|
124 | |
---|