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

Last change on this file since 3864 was 3864, checked in by touky, 7 years ago

Easymesh test tuto works with lua, but EzMesh seems broken.

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