source: trunk/demos/tutorial/04_texture.cpp @ 2822

Last change on this file since 2822 was 2822, checked in by sam, 7 years ago

tutorial: add missing uniform location.

  • Property svn:eol-style set to LF
File size: 3.3 KB
Line 
1//
2// Lol Engine - Graphing tutorial
3//
4// Copyright: (c) 2012-2013 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://www.wtfpl.net/ 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
21static int const TEXTURE_WIDTH = 256;
22
23LOLFX_RESOURCE_DECLARE(04_texture);
24
25class TextureDemo : public WorldEntity
26{
27public:
28    TextureDemo() :
29        m_frames(0),
30        m_ready(false)
31    {
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        m_vertices << vec2( 1.0, -1.0);
37        m_vertices << vec2( 1.0,  1.0);
38
39        m_heightmap = new uint8_t[TEXTURE_WIDTH * 1];
40    }
41
42    virtual ~TextureDemo()
43    {
44        delete m_heightmap;
45    }
46
47    virtual void TickGame(float seconds)
48    {
49        WorldEntity::TickGame(seconds);
50
51        /* Generate a new heightmap at the beginning */
52        if (m_frames == 0)
53            memset(m_heightmap, 255, TEXTURE_WIDTH);
54
55        /* Scroll left */
56        for (int i = 0; i < TEXTURE_WIDTH - 1; i++)
57            m_heightmap[i] = m_heightmap[i + 1];
58
59        int height = m_heightmap[TEXTURE_WIDTH - 1];
60        height = (height + 127 + 40 * lol::sin(m_frames * 0.03) + rand() % 97 - 38) / 2;
61        height = std::max(15, std::min(height, 240));
62        m_heightmap[TEXTURE_WIDTH - 1] = height;
63
64        /* Update frame counter */
65        ++m_frames;
66    }
67
68    virtual void TickDraw(float seconds)
69    {
70        WorldEntity::TickDraw(seconds);
71
72        /* Initialise GPU data */
73        if (!m_ready)
74        {
75            m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
76
77            m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
78            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
79            m_texture_uni = m_shader->GetUniformLocation("u_Texture");
80
81            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
82
83            m_vbo = new VertexBuffer(m_vertices.Bytes());
84            void *vertices = m_vbo->Lock(0, 0);
85            memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
86            m_vbo->Unlock();
87
88            m_ready = true;
89
90            /* FIXME: this object never cleans up */
91        }
92
93        /* Send new heightmap to GPU */
94        m_texture->SetData(m_heightmap);
95
96        m_shader->Bind();
97        m_shader->SetUniform(m_texture_uni, m_texture->GetTexture(), 0);
98        m_vdecl->SetStream(m_vbo, m_coord);
99        m_vdecl->Bind();
100        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
101        m_vdecl->Unbind();
102    }
103
104private:
105    Array<vec2> m_vertices;
106    Texture *m_texture;
107    Shader *m_shader;
108    ShaderAttrib m_coord;
109    ShaderUniform m_texture_uni;
110    VertexDeclaration *m_vdecl;
111    VertexBuffer *m_vbo;
112    uint8_t *m_heightmap;
113    int m_frames;
114    bool m_ready;
115};
116
117int main(int argc, char **argv)
118{
119    System::Init(argc, argv);
120
121    Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
122
123//new DebugFps(50, 50);
124    new TextureDemo();
125
126    app.Run();
127    return EXIT_SUCCESS;
128}
129
Note: See TracBrowser for help on using the repository browser.