|
| | | | | | | |
----------------------------------------------------
---- Project: Tutorial 4
---- Description: objects add mesh,
---- create mesh, setup camera, add
---- object to scene.
----
---- Created by: Roswell_r, 2008 for ApocylX Engine
----------------------------------------------------
---- Modules ----
---- This is used for our group of functions starting with callback.
callback = {}
---- 0--------1 This will be our square face just like in the previous
---- | | example, except this time we wont create a backface for it.
---- | | It will just be a square face made up of 2 polygons which we
---- | | are going to clone 6 times, position them and create a cube.
---- 3--------2
---- Note:- This is not the best way to create the cube, to create a cube
---- properly it should be done out of vertices. and added into one
---- single mesh.
cubeFace = { }
---- Contains our vertex data, specifying each point in space
cubeFace.vertices = { 0.5, 0.5, 0,
-0.5, 0.5, 0,
-0.5, -0.5, 0,
0.5, -0.5, 0 }
cubeFace.normals = { }
---- Note mappings similar to last example to give it a face.
cubeFace.mappings = { 0,1, 1,1, 1,0, 0,0 }
---- Only 2 faces this time and no back face because we dont want to see inside
---- the cube only its outside.
cubeFace.faces = { 2, 1, 0,
0, 3, 2 }
function callback.init()
---- We set the title of our render window. We then also set the size
---- of our window. This is part of the WIN object.
setTitle("Tutorial 4: A primitive Cube")
setDimension(800, 600)
---- empty() and emptyOverlay() empty the 3D scene objects and 2D
---- scene objects. These methods are part of the WORLD object.
empty()
emptyOverlay()
---- This removes the partial help menu located on the left hand side
---- during rendering. With the following command it will simply
---- display "F1 Show/Hide Help". If you want to disable the help menu
---- completely use the command hideHelp(). These methods are part of
---- the WIN object.
showHelpReduced()
---- This sets the blanking colour of the scene. This is colour
---- fills the screen after each frame. Part of the WORLD object.
setClear(0.0,0.0,0.0)
---- The following loads a image file from hard disk, in this case we
---- are loading a PNG logo of LUA.
cubeImage = Image("powered-by-128.png")
---- We now have the image data we're going to now turn it into a
---- texture for skinning a object. Reason we do this is so the engine
---- loads that texture into OpenGL and optimises it for use.
cubeTexture = Texture(cubeImage, false, true)
---- We create a default material like we did for the previous example
---- but this time we're going to set the DiffuseTexture and give it
---- reference to our texture we've just loaded. That means anything
---- that has our squareMaterial applied to it will be textured.
cubeMaterial = Material()
cubeMaterial:setDiffuseTexture(cubeTexture)
---- This creates a new shape based on our array of vertices, normals
---- which are blank, our texture mappings which are also blank and
---- our array of faces.
cubeFaceShape = Shape(cubeFace.vertices, cubeFace.normals, cubeFace.mappings, cubeFace.faces)
---- We've only created a shape which is made up of our vertices and
---- faces we now have to add that shape to our mesh. We can use this
---- shape later for another mesh or we can manipulate the vertex data
---- at a later time.
----
---- Here we specify our squareMaterial which contains our texture to
---- be applied to our square face polygons.
cubeFaceMesh = Mesh(cubeFaceShape, cubeMaterial)
---- Here we clone our mesh object, rotate it and move it about to
---- form the 6 sides of the cube. The cube in the end is 1x1x1 units
---- in size.
faceFront = cubeFaceMesh:clone()
faceFront:moveForward(-0.5)
faceRight = cubeFaceMesh:clone()
faceRight:yaw(math.rad(90))
faceRight:moveForward(-0.5)
faceLeft = cubeFaceMesh:clone()
faceLeft:yaw(math.rad(-90))
faceLeft:moveForward(-0.5)
faceBack = cubeFaceMesh:clone()
faceBack:yaw(math.rad(180))
faceBack:moveForward(-0.5)
faceTop = cubeFaceMesh:clone()
faceTop:pitch(math.rad(90))
faceTop:moveForward(-0.5)
faceBottom = cubeFaceMesh:clone()
faceBottom:pitch(math.rad(-90))
faceBottom:moveForward(-0.5)
---- Now that we have our 6 sides of the cube positioned in the
---- correct places it would be silly to rotate them 1 at a time and
---- would look quiet strange, instead we can create a OBJECTS class
---- which will hold our 6 sides and rotate the OBJECTS class instead.
theCube = Objects()
---- Add each of the 6 sides to theCube object array.
theCube:add(faceFront)
theCube:add(faceRight)
theCube:add(faceLeft)
theCube:add(faceBack)
theCube:add(faceTop)
theCube:add(faceBottom)
---- Here we set the perspective of the camera. Part of WORLD object.
setPerspective(60, 1, 1000)
---- This obtains the default camera for our world. Also WORLD object.
local camera = getCamera()
---- Here we reset the camera's position and rotation. Part of the
---- REFERENCE class.
camera:reset()
---- Because the triangle is positioned at 0,0,0 we need to take a
---- step back before we will actually see anything on the screen. So
---- we move the camera back 3 units so we have a nice view of the
---- mesh. This is part of the REFERENCE class.
camera:moveForward(-3)
---- Add our mesh to our world. This adds the mesh at its current
---- position 0,0,0 and will be rendered in our scene. This is part of
---- WORLD object.
addObject(theCube)
end
function callback.update()
---- Here we could move out objects around and take input
---- This is where we rotate theCube objects array
theCube:rotStanding(0.5 * getTimeStep())
end
function callback.final()
---- empty() and emptyOverlay() empty the 3d scene objects and 2d
---- scene objects. This tutorial has no objects but it is always
---- good practice to clear the scene. These again part of the WORLD
---- object.
emptyOverlay()
empty()
end
----SCENE SETUP----
---- This is where the engine actually gets initilised. The render
---- loop is controlled by ApocylX and you gain control back through
---- CallBack functions.
---- What this means is when ApocylX Engine starts it runs our function
---- callback.init specified in the first parameter
---- Before the engine renders its next frame to the screen it also runs our
---- function callback.update specified in the second parameter.
----
---- When you close the program or end the scene the engine runs our function
---- callback.final specified in the third parameter. This is used to free up
---- any resources or you might want to save some data.
setScene(Scene(callback.init,callback.update,callback.final))
---- Scene is a construct method of Scene object and is used for setScene which
---- is part of the WIN object.
| | | | | | | | |
|