Implemented cylinder shape, also fixed the coordinate system (again). I fucking hate...
[fail.git] / app / startup.lua
blobffad92e4d15e4ffc1e9621e7445d44e204aa2b1b
1 --
2 -- Simple test application.
3 --
5 sg = awful.scenegraph
7 function CreateTestScene()
9 -- Create cube frame
10 cubeframe = sg.Frame()
12 -- Create vertex buffer
13 vb = sg.VertexBuffer()
14 vb:addAttribute( sg.VertexBuffer.at_Vector3f, sg.VertexBuffer.a_Position )
15 vb:resize( 8 )
17 vec3 = awful.math.Vector3f
18 vb:setVector3fAttribute( 0, 0, vec3( -1, -1, -1 ) )
19 vb:setVector3fAttribute( 1, 0, vec3( -1, -1, 1 ) )
20 vb:setVector3fAttribute( 2, 0, vec3( -1, 1, -1 ) )
21 vb:setVector3fAttribute( 3, 0, vec3( -1, 1, 1 ) )
22 vb:setVector3fAttribute( 4, 0, vec3( 1, -1, -1 ) )
23 vb:setVector3fAttribute( 5, 0, vec3( 1, -1, 1 ) )
24 vb:setVector3fAttribute( 6, 0, vec3( 1, 1, -1 ) )
25 vb:setVector3fAttribute( 7, 0, vec3( 1, 1, 1 ) )
27 -- Create index buffer
28 ib = sg.IndexBuffer16( 24 )
29 ib:setIndex( 0, 0 )
30 ib:setIndex( 1, 1 )
31 ib:setIndex( 2, 3 )
32 ib:setIndex( 3, 2 )
34 ib:setIndex( 4, 4 )
35 ib:setIndex( 5, 6 )
36 ib:setIndex( 6, 7 )
37 ib:setIndex( 7, 5 )
39 ib:setIndex( 8, 0 )
40 ib:setIndex( 9, 4 )
41 ib:setIndex( 10, 5 )
42 ib:setIndex( 11, 1 )
44 ib:setIndex( 12, 2 )
45 ib:setIndex( 13, 3 )
46 ib:setIndex( 14, 7 )
47 ib:setIndex( 15, 6 )
49 ib:setIndex( 16, 0 )
50 ib:setIndex( 17, 2 )
51 ib:setIndex( 18, 6 )
52 ib:setIndex( 19, 4 )
54 ib:setIndex( 20, 1 )
55 ib:setIndex( 21, 5 )
56 ib:setIndex( 22, 7 )
57 ib:setIndex( 23, 3 )
59 -- Create geometries
60 geom1 = sg.Geometry( vb, ib )
61 geom1:addPrimitive( sg.Primitive.t_Quads, 0, 8 )
62 geom2 = sg.Geometry( vb, ib )
63 geom2:addPrimitive( sg.Primitive.t_Quads, 8, 8 )
64 geom3 = sg.Geometry( vb, ib )
65 geom3:addPrimitive( sg.Primitive.t_Quads, 16, 8 )
67 -- Create materials
68 mat1 = sg.Material()
69 mat1.Emission.value = awful.math.Vector4f( 1, 0, 0, 1 )
70 mat1.Specular.value = awful.math.Vector4f( 0, 0, 0, 1 )
71 mat2 = sg.Material()
72 mat2.Emission.value = awful.math.Vector4f( 0, 1, 0, 1 )
73 mat2.Specular.value = awful.math.Vector4f( 0, 0, 0, 1 )
74 mat3 = sg.Material()
75 mat3.Emission.value = awful.math.Vector4f( 0, 0, 1, 1 )
76 mat3.Specular.value = awful.math.Vector4f( 0, 0, 0, 1 )
78 -- Create drawables
79 drawable1 = sg.Drawable( geom1, mat1, cubeframe )
80 drawable2 = sg.Drawable( geom2, mat2, cubeframe )
81 drawable3 = sg.Drawable( geom3, mat3, cubeframe )
83 -- Create group holding all this stuff together
84 cubegroup = sg.Group()
85 cubegroup:add( drawable1 )
86 cubegroup:add( drawable2 )
87 cubegroup:add( drawable3 )
88 cubegroup.pFrame = cubeframe
90 -- Save the cube into a file, for the lulz
91 --f = awful.io.FileOutputStream( "lulz.asg" )
92 --awful.abf.Writer.Save( f, cubegroup )
94 mat1.Diffuse.value = awful.math.Vector4f( 1, 0, 0, 1 )
95 mat1.Emission.value = awful.math.Vector4f( 0, 0, 0, 1 )
96 mat1.Ambient.value = awful.math.Vector4f( 0.2, 0.2, 0.2, 1 )
98 mat1.bBackfaceCulling = true
100 shape = awful.shapes.Cylinder( mat1, cubeframe )
102 f = awful.io.FileOutputStream( "lulz.asg" )
103 awful.abf.Writer.Save( f, shape )
106 return cubegroup --cubegroup
110 -- Initialize SDL application object. It just use some arbitrary defaults for now.
111 app = awful.sdl.App.GetInstance()
113 scene = CreateTestScene()
114 collectgarbage()
115 f = awful.io.FileInputStream( "lulz.asg" )
116 scene = awful.abf.Reader.Load( f )
118 -- Create camera
119 camframe = sg.Frame()
120 camera = sg.Camera( camframe )
121 camframe.LocalToParent.position.y = -7
122 camframe:dirty()
124 proj = sg.PerspectiveProjection()
126 -- Create viewport
127 viewport = sg.ViewPort( proj, camera )
128 viewport.Rect = awful.math.Rectu32( 0, 0, 800, 600 )
131 -- Create a font object.
132 font = sg.Font( "FreeSans.ttf", 16 )
133 --rc.Font = font
135 -- Create a theme.
136 theme = awful.gui.BoringTheme( font )
138 -- Create a widget container.
139 guiroot = awful.gui.SimpleContainer( theme )
141 -- Create a window containing a layout group
142 window = awful.gui.Window( guiroot )
143 window.Rect = awful.math.Rectf( 50, 70, 0, 0 )
144 vg = awful.gui.VGroup( window )
145 awful.gui.Button( vg, "Tubgirl" )
146 awful.gui.Button( vg, "Goatse" )
148 hg = awful.gui.HGroup( vg )
149 awful.gui.Button( hg, "eat" );
150 awful.gui.Button( hg, "shit" );
151 awful.gui.Button( hg, "Lemon party" );
153 awful.gui.Button( vg, "lulz" )
155 guiroot:buildSceneGraph()
156 guiroot:calcMinMax()
157 guiroot:layout()
159 -- Setup the gui viewport
160 guiproj = sg.PixelProjection()
161 guiviewport = sg.ViewPort( guiproj )
162 guiviewport.Rect = awful.math.Rectu32( 0, 0, 800, 600 )
163 guiviewport.bClearColorBuffer = false
165 -- Setup the frame update function.
167 eventmanager = awful.gui.EventManager.GetInstance()
169 -- TODO: implement some way to directly connect a native function to a signal to avoid
170 -- going through the lua interpreter for nothing
171 app.MouseMotionEvent:connect(
172 function( position, rel )
173 eventmanager:mouseMotionEvent( position, rel )
174 end
177 app.MouseButtonEvent:connect(
178 function( position, butcode, pressed )
179 eventmanager:mouseButtonEvent( position, butcode, pressed )
183 eventmanager.DefaultHandler = guiroot
185 spin = 0
187 app.FrameUpdate:connect(
189 function( deltatime )
191 sg.RenderContext.ClearAll()
193 guicontext = sg.RenderContext( guiviewport )
194 guicontext:addToRenderList()
196 guiroot.pRenderable:evaluate( guicontext )
198 context = sg.RenderContext( viewport )
199 context:addToRenderList()
201 spin = spin + 50 * deltatime
202 mtx = awful.math.Matrix44f()
203 mtx:rotation( spin, awful.math.Vector3f( 1, 0, 0.5 ) )
204 scene.pFrame.LocalToParent = mtx
205 --scene.pFrame.LocalToParent.position.x = 0
206 --scene.pFrame.LocalToParent.position.y = 0
207 --scene.pFrame.LocalToParent.position.z = 0
208 --scene.pFrame:dirty()
210 --mtx:rotation( spin, awful.math.Vector3f( 1, 0, 0 ) )
211 --camframe.LocalToParent = mtx
212 -- camframe.LocalToParent.position.x = 0
213 -- camframe.LocalToParent.position.y = -7
214 -- camframe.LocalToParent.position.z = 2
215 --camframe:dirty()
217 scene:evaluate( context )
219 sg.RenderContext.RenderAll()
221 app:needSwap()
227 -- Start the main loop.
228 app:run()