source: trunk/tutorial/05_easymesh.cpp @ 2183

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

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1//
2// Lol Engine - EasyMesh tutorial
3//
4// Copyright: (c) 2011-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://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
17using namespace std;
18using namespace lol;
19
20class EasyMeshTutorial : public WorldEntity
21{
22public:
23    EasyMeshTutorial()
24    {
25        m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
26        m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
27        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
28        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
29        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
30
31        m_gears[0].m1.Compile("sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1");
32        m_gears[1].m1.Compile("sc#ff9 scb#ff9 acg 54 10 95 95 90 90 -5 -5 0.1 0 s .1 .1 .1");
33        m_gears[2].m1.Compile("sc#9ff scb#9ff acg 18 10 5 5 30 30 5 5 0.1 0 s .1 .1 .1");
34        m_gears[3].m1.Compile("sc#9ff scb#9ff acg 18 10 5 5 30 30 5 5 0.1 0 s .1 .1 .1");
35        m_gears[4].m1.Compile("sc#9ff scb#9ff acg 18 10 5 5 30 30 5 5 0.1 0 s .1 .1 .1");
36
37        m_angle = 0;
38
39        m_camera = new Camera(vec3(0.f, 600.f, 0.f),
40                              vec3(0.f, 0.f, 0.f),
41                              vec3(0, 1, 0));
42        m_camera->SetPerspective(70.f, 960.f, 600.f, .1f, 1000.f);
43        m_camera->SetTarget(vec3(0.f, -1.f, 0.f));
44        m_camera->SetPosition(vec3(-15.f, 10.f, 0.f));
45        Ticker::Ref(m_camera);
46
47        m_ready = false;
48    }
49
50    ~EasyMeshTutorial()
51    {
52        Ticker::Unref(m_camera);
53    }
54
55    virtual void TickGame(float seconds)
56    {
57        WorldEntity::TickGame(seconds);
58
59        m_angle += seconds * 70.0f;
60        m_mat = mat4::rotate(10.0f, vec3(0, 0, 1))
61              * mat4::rotate(m_angle, vec3(0, 1, 0));
62
63        m_gears[0].m3 += seconds * 150.0f;
64        m_gears[1].m3 += seconds * 150.0f * -2 / 9;
65        m_gears[2].m3 += seconds * 150.0f * -2 / 3;
66        m_gears[3].m3 += seconds * 150.0f * -2 / 3;
67        m_gears[4].m3 += seconds * 150.0f * -2 / 3;
68
69        m_gears[0].m2 = mat4::translate(vec3(0, 0, 0))
70                      * mat4::rotate(m_gears[0].m3, vec3(0, 1, 0));
71        m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
72                      * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
73        m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
74                      * mat4::rotate(m_gears[2].m3, vec3(0, 1, 0));
75        m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
76                      * mat4::rotate(m_gears[3].m3, vec3(0, 1, 0));
77        m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
78                      * mat4::rotate(m_gears[4].m3, vec3(0, 1, 0));
79    }
80
81    virtual void TickDraw(float seconds)
82    {
83        WorldEntity::TickDraw(seconds);
84
85        if (!m_ready)
86        {
87            for (int i = 0; i < m_gears.Count(); i++)
88                m_gears[i].m1.MeshConvert();
89            m_ready = true;
90        }
91
92        Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
93
94        for (int i = 0; i < m_gears.Count(); i++)
95            m_gears[i].m1.Render(m_mat * m_gears[i].m2);
96    }
97
98private:
99    Array<EasyMesh, mat4, float> m_gears;
100    float m_angle;
101    mat4 m_mat;
102    Camera *m_camera;
103
104    bool m_ready;
105};
106
107int main(int argc, char **argv)
108{
109    Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
110    new EasyMeshTutorial();
111    app.Run();
112
113    return EXIT_SUCCESS;
114}
115
Note: See TracBrowser for help on using the repository browser.