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

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

system: try to autodetect the data directory from the executable path;
currently works for images (SDL and GDI+ loaders) and sound samples.

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1//
2// Lol Engine - EasyMesh tutorial
3//
4// Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
5//            (c) 2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
6//   This program is free software; you can redistribute it and/or
7//   modify it under the terms of the Do What The Fuck You Want To
8//   Public License, Version 2, as published by Sam Hocevar. See
9//   http://www.wtfpl.net/ for more details.
10//
11
12#if defined HAVE_CONFIG_H
13#   include "config.h"
14#endif
15
16#include "core.h"
17
18using namespace std;
19using namespace lol;
20
21class EasyMeshTutorial : public WorldEntity
22{
23public:
24    EasyMeshTutorial()
25    {
26        m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
27        m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
28        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
29        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
30        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
31
32        m_gears[0].m1.Compile("[sc#00f ab 8 1 8 ty -.25]"
33                              "[sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1 ty -.1 csgu]"
34                              "[sc#fff scb#000 acg 12 10 10 10 20 20 5 5 0.1 0 s .05 .05 .05 tx -1.5 ty .3 csgu]"
35                              "[sc#00f ab 5 3 9 tx 2.5 csgs]"
36                              "[[ sc#fff ab 3   1.4 2   tx -2 tz -2 "
37                              "[sc#fff ab 2.1  .7 1.1 ty .5 tx -1.4 tz -1.4 csgs] mz] csgu]");
38        //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 [sc#00f ab 3 1 2 ty .25 tx 1 csgs]]");
39        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");
40        //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 [sc#00f scb#00f ab 2 2 2 tx 1.5]");
41        //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 [sc#00f scb#00f ab 2 2 2 tx 1.5]");
42        //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 [sc#00f scb#00f ab 2 2 2 tx 1.5]");
43        m_gears[2].m1.Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csgu]]");
44        m_gears[3].m1.Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csgs]]");
45        m_gears[4].m1.Compile("[sc#0f0 ab 2 2 2 t .8 .8 .8 rx 20 ry 20 [sc#00f ab 2 2 2 tx 0 csga]]");
46
47        m_angle = 0;
48
49        m_camera = new Camera(vec3(0.f, 600.f, 0.f),
50                              vec3(0.f, 0.f, 0.f),
51                              vec3(0, 1, 0));
52        m_camera->SetPerspective(30.f, 960.f, 600.f, .1f, 1000.f);
53        m_camera->SetTarget(vec3(0.f, -1.f, 0.f));
54        m_camera->SetPosition(vec3(-15.f, 5.f, 0.f));
55        Ticker::Ref(m_camera);
56
57        m_ready = false;
58    }
59
60    ~EasyMeshTutorial()
61    {
62        Ticker::Unref(m_camera);
63    }
64
65    virtual void TickGame(float seconds)
66    {
67        WorldEntity::TickGame(seconds);
68
69        m_angle += seconds * 70.0f;
70        m_mat = mat4::rotate(10.0f, vec3(0, 0, 1))
71                * mat4::rotate(100, vec3(0, 1, 0));
72        //      * mat4::rotate(m_angle, vec3(0, 1, 0));
73
74        m_gears[0].m3 += seconds * 20.0f;
75        m_gears[1].m3 += seconds * 20.0f * -2 / 9;
76        m_gears[2].m3 += seconds * 20.0f * -2 / 3;
77        m_gears[3].m3 += seconds * 20.0f * -2 / 3;
78        m_gears[4].m3 += seconds * 20.0f * -2 / 3;
79
80        m_gears[0].m2 = mat4::translate(vec3(0, -1, 0))
81                      * mat4::rotate(m_gears[0].m3 - 130.0f, vec3(0, 1, 0))
82                      * mat4::rotate(40.0f, vec3(0, 0, 1));
83        m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
84                      * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
85        m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
86                      * mat4::rotate(m_gears[2].m3 - 40.0f, vec3(0, 1, 0))
87                      * mat4::rotate(90.0f, vec3(0, 0, 1));
88        m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
89                      * mat4::rotate(m_gears[3].m3 - 140.0f, vec3(0, 1, 0))
90                      * mat4::rotate(-70.0f, vec3(0, 0, 1));
91        m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
92                      * mat4::rotate(m_gears[4].m3 - 80.0f, vec3(0, 1, 0));
93    }
94
95    virtual void TickDraw(float seconds)
96    {
97        WorldEntity::TickDraw(seconds);
98
99        if (!m_ready)
100        {
101            for (int i = 0; i < m_gears.Count(); i++)
102                m_gears[i].m1.MeshConvert();
103            m_ready = true;
104        }
105
106        Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
107
108        for (int i = 0; i < m_gears.Count(); i++)
109            m_gears[i].m1.Render(m_mat * m_gears[i].m2);
110    }
111
112private:
113    Array<EasyMesh, mat4, float> m_gears;
114    float m_angle;
115    mat4 m_mat;
116    Camera *m_camera;
117
118    bool m_ready;
119};
120
121int main(int argc, char **argv)
122{
123    System::Init(argc, argv);
124
125    Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
126    new EasyMeshTutorial();
127    app.Run();
128
129    return EXIT_SUCCESS;
130}
131
Note: See TracBrowser for help on using the repository browser.