source: trunk/doc/tutorial/05_easymesh.cpp @ 3788

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

build: move sample and tutorial files around.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 6.1 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 HAVE_CONFIG_H
13#   include "config.h"
14#endif
15
16#include <lol/engine.h>
17
18using namespace lol;
19
20#define USE_CUSTOM_SHADER 1
21#if USE_CUSTOM_SHADER
22LOLFX_RESOURCE_DECLARE(shiny);
23#endif //USE_CUSTOM_SHADER
24
25class EasyMeshTutorial : public WorldEntity
26{
27public:
28    EasyMeshTutorial()
29    {
30        m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
31        m_gears.Push(EasyMesh(), mat4(1.0f), 0.0f);
32        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
33        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
34        m_gears.Push(EasyMesh(), mat4(1.0f), 180.0f / 18);
35
36        m_gears[0].m1.Compile("[sc#00f ab 8 1 8 ty -.25]"
37                              "[sc#f9f scb#f9f acg 12 10 5 5 20 20 5 5 0.1 0 s .1 .1 .1 ty -.1 csgu]"
38                              "[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]"
39                              "[sc#00f ab 5 3 9 tx 2.5 csgs]"
40                              "[[ sc#fff ab 3   1.4 2   tx -2 tz -2 "
41                              "[sc#fff ab 2.1  .7 1.1 ty .5 tx -1.4 tz -1.4 csgs] mz] csgu]");
42        //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]]");
43        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");
44        //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]");
45        //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]");
46        //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]");
47        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]]");
48        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]]");
49        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]]");
50
51        m_angle = 0;
52
53        m_camera = new Camera();
54        m_camera->SetProjection(mat4::perspective(30.f, 960.f, 600.f, .1f, 1000.f));
55        m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f),
56                                       vec3(0.f, -1.f, 0.f),
57                                       vec3(0.f, 1.f, 0.f)));
58        g_scene->PushCamera(m_camera);
59
60        /* Add a white directional light */
61        m_light1 = new Light();
62        m_light1->SetPosition(vec3(0.2f, 0.2f, 0.f));
63        m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
64        m_light1->SetType(LightType::Directional);
65        Ticker::Ref(m_light1);
66
67        /* Add an orangeish point light */
68        m_light2 = new Light();
69        m_light2->SetPosition(vec3(-15.f, 15.f, 15.f));
70        m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
71        m_light2->SetType(LightType::Point);
72        Ticker::Ref(m_light2);
73
74        m_ready = false;
75    }
76
77    ~EasyMeshTutorial()
78    {
79        g_scene->PopCamera(m_camera);
80        Ticker::Unref(m_light1);
81        Ticker::Unref(m_light2);
82    }
83
84    virtual void TickGame(float seconds)
85    {
86        WorldEntity::TickGame(seconds);
87
88        m_angle += seconds * 70.0f;
89        m_mat = mat4::rotate(10.0f, vec3(0, 0, 1))
90                * mat4::rotate(100, vec3(0, 1, 0));
91        //      * mat4::rotate(m_angle, vec3(0, 1, 0));
92
93        m_gears[0].m3 += seconds * 20.0f;
94        m_gears[1].m3 += seconds * 20.0f * -2 / 9;
95        m_gears[2].m3 += seconds * 20.0f * -2 / 3;
96        m_gears[3].m3 += seconds * 20.0f * -2 / 3;
97        m_gears[4].m3 += seconds * 20.0f * -2 / 3;
98
99        m_gears[0].m2 = mat4::translate(vec3(0, -1, 0))
100                      * mat4::rotate(m_gears[0].m3 - 130.0f, vec3(0, 1, 0))
101                      * mat4::rotate(40.0f, vec3(0, 0, 1));
102        m_gears[1].m2 = mat4::translate(vec3(0, 0, 0))
103                      * mat4::rotate(m_gears[1].m3, vec3(0, 1, 0));
104        m_gears[2].m2 = mat4::translate(vec3(0, 0, 5.5f))
105                      * mat4::rotate(m_gears[2].m3 - 40.0f, vec3(0, 1, 0))
106                      * mat4::rotate(90.0f, vec3(0, 0, 1));
107        m_gears[3].m2 = mat4::translate(vec3(5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
108                      * mat4::rotate(m_gears[3].m3 - 140.0f, vec3(0, 1, 0))
109                      * mat4::rotate(-70.0f, vec3(0, 0, 1));
110        m_gears[4].m2 = mat4::translate(vec3(-5.5f * lol::sqrt(3.f) * 0.5f, 0, -5.5f * 0.5f))
111                      * mat4::rotate(m_gears[4].m3 - 80.0f, vec3(0, 1, 0));
112    }
113
114    virtual void TickDraw(float seconds, Scene &scene)
115    {
116        WorldEntity::TickDraw(seconds, scene);
117
118        if (!m_ready)
119        {
120            g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
121
122            /* Upload vertex data to GPU */
123            for (int i = 0; i < m_gears.Count(); i++)
124                m_gears[i].m1.MeshConvert();
125
126#if USE_CUSTOM_SHADER
127            /* Custom Shader: Init the shader */
128            Shader *custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(shiny));
129            // any other shader stuf here (Get uniform, mostly, and set texture)
130
131            for (int i = 0; i < m_gears.Count(); i++)
132                m_gears[i].m1.SetMaterial(custom_shader);
133#endif
134
135            m_ready = true;
136        }
137
138        for (int i = 0; i < m_gears.Count(); i++)
139        {
140            scene.AddPrimitive(m_gears[i].m1, m_mat * m_gears[i].m2);
141        }
142    }
143
144private:
145    Shader* m_custom_shader;
146    array<EasyMesh, mat4, float> m_gears;
147    float m_angle;
148    mat4 m_mat;
149    Camera *m_camera;
150    Light *m_light1, *m_light2;
151
152    bool m_ready;
153};
154
155int main(int argc, char **argv)
156{
157    System::Init(argc, argv);
158
159    Application app("Tutorial 5: EasyMesh", ivec2(960, 600), 60.0f);
160    new EasyMeshTutorial();
161    app.Run();
162
163    return EXIT_SUCCESS;
164}
165
Note: See TracBrowser for help on using the repository browser.