| 1 |
-------------------------------------------------------------------------------- |
| 2 |
-- | |
| 3 |
-- Module : Graphics.UI.GLUT.Objects |
| 4 |
-- Copyright : (c) Sven Panne 2002-2018 |
| 5 |
-- License : BSD3 |
| 6 |
-- |
| 7 |
-- Maintainer : Sven Panne <[email protected]> |
| 8 |
-- Stability : stable |
| 9 |
-- Portability : portable |
| 10 |
-- |
| 11 |
-- GLUT includes a number of routines for generating easily recognizable 3D |
| 12 |
-- geometric objects. These routines reflect functionality available in the |
| 13 |
-- @aux@ toolkit described in the /OpenGL Programmer\'s Guide/ and are included |
| 14 |
-- in GLUT to allow the construction of simple GLUT programs that render |
| 15 |
-- recognizable objects. These routines can be implemented as pure OpenGL |
| 16 |
-- rendering routines. The routines do not generate display lists for the |
| 17 |
-- objects they create. The routines generate normals appropriate for lighting |
| 18 |
-- but do not generate texture coordinates (except for the solid teapot, teacup |
| 19 |
-- and teaspoon). If VBOs should be used instead of the fixed function pipeline, |
| 20 |
-- specify at least one of the attribute locations |
| 21 |
-- 'Graphics.UI.GLUT.State.vertexAttribCoord3' or |
| 22 |
-- 'Graphics.UI.GLUT.State.vertexAttribNormal'. |
| 23 |
-- |
| 24 |
-------------------------------------------------------------------------------- |
| 25 |
|
| 26 |
module Graphics.UI.GLUT.Objects ( |
| 27 |
-- * Rendering flavour |
| 28 |
Flavour(..), |
| 29 |
|
| 30 |
-- * Object description |
| 31 |
Object(..), |
| 32 |
|
| 33 |
-- * Type synonyms |
| 34 |
Sides, Rings, NumLevels, |
| 35 |
|
| 36 |
-- * Rendering |
| 37 |
renderObject |
| 38 |
) where |
| 39 |
|
| 40 |
import Control.Monad.IO.Class ( MonadIO(..) ) |
| 41 |
import Foreign.C.Types ( CInt ) |
| 42 |
import Foreign.Marshal.Utils ( with ) |
| 43 |
import Foreign.Ptr ( Ptr, castPtr ) |
| 44 |
import Graphics.Rendering.OpenGL ( |
| 45 |
Height, Radius, Slices, Stacks, Vertex3(..), GLdouble, GLint ) |
| 46 |
|
| 47 |
import Graphics.UI.GLUT.Raw |
| 48 |
|
| 49 |
-------------------------------------------------------------------------------- |
| 50 |
|
| 51 |
-- | Flavour of object rendering |
| 52 |
|
| 53 |
data Flavour |
| 54 |
= -- | Object is rendered as a solid with shading and surface normals. |
| 55 |
Solid |
| 56 |
| -- | Object is rendered as a wireframe without surface normals. |
| 57 |
Wireframe |
| 58 |
deriving ( Eq, Ord, Show ) |
| 59 |
|
| 60 |
-------------------------------------------------------------------------------- |
| 61 |
|
| 62 |
-- | GLUT offers five types of objects: |
| 63 |
-- |
| 64 |
-- * The five Platonic solids, see |
| 65 |
-- <http://mathworld.wolfram.com/PlatonicSolid.html>. |
| 66 |
-- |
| 67 |
-- * A rhombic dodecahedron, see |
| 68 |
-- <http://mathworld.wolfram.com/RhombicDodecahedron.html>. |
| 69 |
-- |
| 70 |
-- * Approximations to rounded objects. |
| 71 |
-- |
| 72 |
-- * The classic teaset modeled by Martin Newell in 1975. Both surface normals |
| 73 |
-- and texture coordinates for the teaset are generated. |
| 74 |
-- |
| 75 |
-- * A Sierpinski sponge, see |
| 76 |
-- <http://mathworld.wolfram.com/Tetrix.html>. |
| 77 |
|
| 78 |
data Object |
| 79 |
= -- | A cube centered at the modeling coordinates origin with sides of the |
| 80 |
-- given length. |
| 81 |
Cube Height |
| 82 |
| -- | A dodecahedron (12-sided regular solid) centered at the modeling |
| 83 |
-- coordinates origin with a radius of @sqrt 3@. |
| 84 |
Dodecahedron |
| 85 |
| -- | A icosahedron (20-sided regular solid) centered at the modeling |
| 86 |
-- coordinates origin with a radius of 1.0. |
| 87 |
Icosahedron |
| 88 |
| -- | Render a solid octahedron (8-sided regular solid) centered at the |
| 89 |
-- modeling coordinates origin with a radius of 1.0. |
| 90 |
Octahedron |
| 91 |
| -- | Render a solid tetrahedron (4-sided regular solid) centered at the |
| 92 |
-- modeling coordinates origin with a radius of @sqrt 3@. |
| 93 |
Tetrahedron |
| 94 |
| -- | (/freeglut only/) A rhombic dodecahedron whose corners are at most a |
| 95 |
-- distance of one from the origin. The rhombic dodecahedron has faces |
| 96 |
-- which are identical rhombi, but which have some vertices at which three |
| 97 |
-- faces meet and some vertices at which four faces meet. The length of |
| 98 |
-- each side is @(sqrt 3)\/2@. Vertices at which four faces meet are found |
| 99 |
-- at @(0, 0, +\/-1)@ and @(+\/-(sqrt 2)\/2, +\/-(sqrt 2)\/2, 0)@. |
| 100 |
RhombicDodecahedron |
| 101 |
| -- | A sphere centered at the modeling coordinates origin of the specified |
| 102 |
-- radius. The sphere is subdivided around the Z axis into slices |
| 103 |
-- (similar to lines of longitude) and along the Z axis into stacks |
| 104 |
-- (similar to lines of latitude). |
| 105 |
Sphere' Radius Slices Stacks |
| 106 |
| -- | A cone oriented along the Z axis. The base of the cone is placed at Z |
| 107 |
-- = 0, and the top at Z = the given height. The cone is subdivided |
| 108 |
-- around the Z axis into slices, and along the Z axis into stacks. |
| 109 |
Cone Radius Height Slices Stacks |
| 110 |
| -- |(/freeglut only/) A cylinder oriented along the Z axis. The base of the |
| 111 |
-- cylinder is placed at Z = 0, and the top at Z = the given height. The |
| 112 |
-- cylinder is subdivided around the Z axis into slices, and along the Z |
| 113 |
-- axis into stacks. |
| 114 |
Cylinder' Radius Height Slices Stacks |
| 115 |
| -- | A torus (doughnut) centered at the modeling coordinates origin |
| 116 |
-- whose axis is aligned with the Z axis. The torus is described by its |
| 117 |
-- inner and outer radius, the number of sides for each radial section, |
| 118 |
-- and the number of radial divisions (rings). |
| 119 |
Torus Radius Radius Sides Rings |
| 120 |
| -- | A teapot with a given relative size. |
| 121 |
Teapot Height |
| 122 |
| -- |(/freeglut only/) A teacup with a given relative size. |
| 123 |
Teacup Height |
| 124 |
| -- |(/freeglut only/) A teaspoon with a given relative size. |
| 125 |
Teaspoon Height |
| 126 |
| -- |(/freeglut only/) A Sierpinski sponge of a given level, where a level |
| 127 |
-- 0 sponge is the same as a 'Tetrahedron'. |
| 128 |
SierpinskiSponge NumLevels |
| 129 |
deriving ( Eq, Ord, Show ) |
| 130 |
|
| 131 |
-------------------------------------------------------------------------------- |
| 132 |
|
| 133 |
type Sides = GLint |
| 134 |
type Rings = GLint |
| 135 |
type NumLevels = GLint |
| 136 |
|
| 137 |
-------------------------------------------------------------------------------- |
| 138 |
|
| 139 |
-- | Render an object in the given flavour. |
| 140 |
|
| 141 |
renderObject :: MonadIO m => Flavour -> Object -> m () |
| 142 |
renderObject Solid (Cube h) = glutSolidCube h |
| 143 |
renderObject Wireframe (Cube h) = glutWireCube h |
| 144 |
renderObject Solid Dodecahedron = glutSolidDodecahedron |
| 145 |
renderObject Wireframe Dodecahedron = glutWireDodecahedron |
| 146 |
renderObject Solid Icosahedron = glutSolidIcosahedron |
| 147 |
renderObject Wireframe Icosahedron = glutWireIcosahedron |
| 148 |
renderObject Solid Octahedron = glutSolidOctahedron |
| 149 |
renderObject Wireframe Octahedron = glutWireOctahedron |
| 150 |
renderObject Solid Tetrahedron = glutSolidTetrahedron |
| 151 |
renderObject Wireframe Tetrahedron = glutWireTetrahedron |
| 152 |
renderObject Solid RhombicDodecahedron = glutSolidRhombicDodecahedron |
| 153 |
renderObject Wireframe RhombicDodecahedron = glutWireRhombicDodecahedron |
| 154 |
renderObject Solid (Sphere' r s t) = glutSolidSphere r s t |
| 155 |
renderObject Wireframe (Sphere' r s t) = glutWireSphere r s t |
| 156 |
renderObject Solid (Cone r h s t) = glutSolidCone r h s t |
| 157 |
renderObject Wireframe (Cone r h s t) = glutWireCone r h s t |
| 158 |
renderObject Solid (Cylinder' r h s t) = glutSolidCylinder r h s t |
| 159 |
renderObject Wireframe (Cylinder' r h s t) = glutWireCylinder r h s t |
| 160 |
renderObject Solid (Torus i o s r) = glutSolidTorus i o s r |
| 161 |
renderObject Wireframe (Torus i o s r) = glutWireTorus i o s r |
| 162 |
renderObject Solid (Teapot h) = glutSolidTeapot h |
| 163 |
renderObject Wireframe (Teapot h) = glutWireTeapot h |
| 164 |
renderObject Solid (Teacup h) = glutSolidTeacup h |
| 165 |
renderObject Wireframe (Teacup h) = glutWireTeacup h |
| 166 |
renderObject Solid (Teaspoon h) = glutSolidTeaspoon h |
| 167 |
renderObject Wireframe (Teaspoon h) = glutWireTeaspoon h |
| 168 |
renderObject Solid (SierpinskiSponge n) = solidSierpinskiSponge n |
| 169 |
renderObject Wireframe (SierpinskiSponge n) = wireSierpinskiSponge n |
| 170 |
|
| 171 |
-------------------------------------------------------------------------------- |
| 172 |
|
| 173 |
solidSierpinskiSponge :: MonadIO m => NumLevels -> m () |
| 174 |
solidSierpinskiSponge = sierpinskiSponge glutSolidSierpinskiSponge |
| 175 |
|
| 176 |
wireSierpinskiSponge :: MonadIO m => NumLevels -> m () |
| 177 |
wireSierpinskiSponge = sierpinskiSponge glutWireSierpinskiSponge |
| 178 |
|
| 179 |
-- for consistency, we hide the offset and scale on the Haskell side |
| 180 |
sierpinskiSponge :: MonadIO m => (CInt -> Ptr GLdouble -> Height -> IO ()) -> NumLevels -> m () |
| 181 |
sierpinskiSponge f n = liftIO $ |
| 182 |
with (Vertex3 0 0 0) $ \offsetBuf -> |
| 183 |
f (fromIntegral n) ((castPtr :: Ptr (Vertex3 GLdouble) -> Ptr GLdouble) offsetBuf) 1 |