source: trunk/tutorial/04_texture.cpp @ 2001

Last change on this file since 2001 was 2001, checked in by sam, 8 years ago

tutorial: add a new texture generation example.

File size: 3.9 KB
Line 
1//
2// Lol Engine - Noise 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 "loldebug.h"
17
18using namespace std;
19using namespace lol;
20
21extern char const *lolfx_04_texture;
22
23class TextureDemo : public WorldEntity
24{
25public:
26    TextureDemo() :
27        m_frames(0),
28        m_ready(false)
29    {
30        m_vertices << vec2(-1.0,  1.0);
31        m_vertices << vec2(-1.0, -1.0);
32        m_vertices << vec2( 1.0, -1.0);
33        m_vertices << vec2(-1.0,  1.0);
34        m_vertices << vec2( 1.0, -1.0);
35        m_vertices << vec2( 1.0,  1.0);
36
37        m_heightmap = new uint8_t[4 * 512 * 1];
38    }
39
40    virtual ~TextureDemo()
41    {
42        delete m_heightmap;
43    }
44
45    virtual void TickGame(float seconds)
46    {
47        WorldEntity::TickGame(seconds);
48
49        /* Generate a new heightmap every 400 frames */
50        if (m_frames % 400 == 0)
51        {
52            for (int i = 0, height = 64; i < 512; i++)
53            {
54                m_heightmap[4 * i] = height;
55                m_heightmap[4 * i + 1] = 255; /* unused */
56                m_heightmap[4 * i + 2] = 255; /* unused */
57                m_heightmap[4 * i + 3] = 255; /* unused */
58   
59                height += rand() % 17 - 8;
60                height += rand() % 17 - 8;
61                height = std::max(15, std::min(height, 240));
62            }
63        }
64
65        /* Slightly disturb the terrain */
66        for (int i = 1; i < 511; i++)
67        {
68            int delta = (rand() & 1) ? 1 : -1;
69
70            if (rand() & 3)
71                continue;
72
73            uint8_t &center = m_heightmap[4 * i];
74            uint8_t &side1 = m_heightmap[4 * (i - delta)];
75            uint8_t &side2 = m_heightmap[4 * (i + delta)];
76
77            if (center > side1)
78            {
79                center--;
80                side1++;
81            }
82            else if (center > side2)
83            {
84                center--;
85                side2++;
86            }
87        }
88
89        /* Update frame counter */
90        ++m_frames;
91    }
92
93    virtual void TickDraw(float seconds)
94    {
95        WorldEntity::TickDraw(seconds);
96
97        /* Initialise GPU data */
98        if (!m_ready)
99        {
100            m_texture = new Texture(ivec2(512, 1), PixelFormat::A8R8G8B8);
101
102            m_shader = Shader::Create(lolfx_04_texture);
103            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
104
105            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
106
107            m_vbo = new VertexBuffer(m_vertices.Bytes());
108            void *vertices = m_vbo->Lock(0, 0);
109            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
110            m_vbo->Unlock();
111
112            m_ready = true;
113
114            /* FIXME: this object never cleans up */
115        }
116
117        /* Send new heightmap to GPU */
118        m_texture->SetData(m_heightmap);
119
120        m_shader->Bind();
121        m_shader->SetUniform(m_texture_uni, m_texture->GetTexture(), 0);
122        m_vdecl->SetStream(m_vbo, m_coord);
123        m_vdecl->Bind();
124        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
125        m_vdecl->Unbind();
126    }
127
128private:
129    Array<vec2> m_vertices;
130    Texture *m_texture;
131    Shader *m_shader;
132    ShaderAttrib m_coord;
133    ShaderUniform m_texture_uni;
134    VertexDeclaration *m_vdecl;
135    VertexBuffer *m_vbo;
136    uint8_t *m_heightmap;
137    int m_frames;
138    bool m_ready;
139};
140
141int main(int argc, char **argv)
142{
143    Application app("Tutorial 4: Texture", ivec2(640, 480), 60.0f);
144
145#if defined _MSC_VER && !defined _XBOX
146    _chdir("../..");
147#elif defined _WIN32 && !defined _XBOX
148    _chdir("../..");
149#endif
150
151    new TextureDemo();
152
153    app.Run();
154    return EXIT_SUCCESS;
155}
156
Note: See TracBrowser for help on using the repository browser.