source: trunk/test/tutorial/tut01.cpp @ 1228

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

gpu: port the vertex buffer abstraction layer to OpenGL.

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1//
2// Lol Engine - Triangle tutorial
3//
4// Copyright: (c) 2012 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 defined _WIN32 && defined USE_D3D9
23#   define FAR
24#   define NEAR
25#   include <d3d9.h>
26#endif
27
28#if USE_SDL && defined __APPLE__
29#   include <SDL_main.h>
30#endif
31
32#if defined __native_client__
33#   define main old_main
34#endif
35
36#if defined _WIN32
37#   undef main /* FIXME: still needed? */
38#   include <direct.h>
39#endif
40
41#if defined USE_D3D9
42extern IDirect3DDevice9 *g_d3ddevice;
43#elif defined _XBOX
44extern D3DDevice *g_d3ddevice;
45#endif
46
47class Triangle : public WorldEntity
48{
49public:
50    Triangle()
51    {
52        m_vertices[0] = vec2( 0.0,  0.8);
53        m_vertices[1] = vec2(-0.8, -0.8);
54        m_vertices[2] = vec2( 0.8, -0.8);
55        m_ready = false;
56    }
57
58    virtual void TickDraw(float deltams)
59    {
60        WorldEntity::TickDraw(deltams);
61
62        if (!m_ready)
63        {
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79            m_shader = Shader::Create(
80#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
81                "#version 120\n"
82                "attribute vec2 in_Position;"
83                "void main(void) {"
84                "    gl_Position = vec4(in_Position, 0.0, 1.0);"
85                "}",
86
87                "#version 120\n"
88                "void main(void) {"
89                "    gl_FragColor = vec4(0.7, 0.5, 0.2, 1.0);"
90                "}"
91#else
92                "void main(float2 in_Position : POSITION,"
93                "          out float4 out_Position : POSITION) {"
94                "    out_Position = float4(in_Position, 0.0, 1.0);"
95                "}",
96
97                "void main(out float4 out_FragColor : COLOR) {"
98                "    out_FragColor = float4(0.7, 0.5, 0.2, 1.0);"
99                "}"
100#endif
101            );
102            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
103
104            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
105
106            m_vbo = new VertexBuffer(sizeof(m_vertices));
107            void *vertices = m_vbo->Lock(0, 0);
108            memcpy(vertices, m_vertices, sizeof(m_vertices));
109            m_vbo->Unlock();
110
111            m_ready = true;
112
113            /* FIXME: this object never cleans up */
114        }
115
116        m_shader->Bind();
117        m_vdecl->Bind();
118        m_vdecl->SetStream(m_vbo, m_coord);
119#if defined _XBOX || defined USE_D3D9
120        g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
121#endif
122
123#if defined _XBOX || defined USE_D3D9
124        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
125#else
126        glDrawArrays(GL_TRIANGLES, 0, 3);
127#endif
128
129        m_vdecl->Unbind();
130#if defined _XBOX || defined USE_D3D9
131        /* FIXME: do we need to unset anything here? */
132#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
133        //glDisableVertexAttribArray(m_attrib);
134        //glBindBuffer(GL_ARRAY_BUFFER, 0);
135#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
136        /* Never used for now */
137        glDisableVertexAttribArray(m_attrib);
138#else
139        glDisableClientState(GL_VERTEX_ARRAY);
140#endif
141    }
142
143private:
144    vec2 m_vertices[3];
145    Shader *m_shader;
146    VertexDeclaration *m_vdecl;
147    VertexBuffer *m_vbo;
148    ShaderAttrib m_coord;
149    bool m_ready;
150};
151
152int main(int argc, char **argv)
153{
154    Application app("Tutorial 1: Triangle", ivec2(640, 480), 60.0f);
155
156#if defined _MSC_VER && !defined _XBOX
157    _chdir("..");
158#elif defined _WIN32 && !defined _XBOX
159    _chdir("../..");
160#endif
161
162    new DebugFps(5, 5);
163    new Triangle();
164
165    app.Run();
166    return EXIT_SUCCESS;
167}
168
Note: See TracBrowser for help on using the repository browser.