Praktikum BKSPP: [2ex] Blatt 3 · 2014-11-21 · Praktikum BKSPP: Blatt 3 { WS 2014/15 { D. Sabel...
Transcript of Praktikum BKSPP: [2ex] Blatt 3 · 2014-11-21 · Praktikum BKSPP: Blatt 3 { WS 2014/15 { D. Sabel...
OpenGL & GLUT 2D-Turtle 3D-Turtle
OpenGL & GLUT in Haskell
OpenGL = Open Graphics Library
GLUT = OpenGL Utility Toolkit
Auf hackage.haskell.org gibt esHaskell-Anbindungen an beide Bibliotheken
Haskell Platform enthalt sie bereits
Installation: Meistens notwendig: GLUT installieren (Linksdazu auf Aufgabenblatt)
Ausfuhrung im GHCi: Gibt oft segmentation fault, besserkompilierenghc --make -o output.exe Input.hs
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 2/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
OpenGL & GLUT in Haskell
Sehr gute Einfuhrung in OpenGL-Programmierung in Haskell:
Tutorial von Sven Eric Panitz
http://www.cs.hs-rm.de/∼panitz/hopengl
Weiteres:
http://www.haskell.org/haskellwiki/Opengl
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 3/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Grundgerust fur ein OpenGL-Programm in Haskell
import Graphics.UI.GLUT
import Graphics.Rendering.OpenGL
main = do
(prgName,_) <- getArgsAndInitialize
createWindow prgName
windowSize $= Size 800 800
displayCallback $= mainDisplay
mainLoop
mainDisplay = ...
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 4/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Objekte zeichnen
type Coord = (GLfloat,GLfloat,GLfloat)
type ColoredCoord = (GLfloat,GLfloat,GLfloat,Color4 GLfloat)
beispielPunkte :: [Coord]
beispielPunkte = [(-0.25, 0.25, 0.0), (0.75, 0.35, 0.0),
(0.75, -0.15, 0.0), (-0.75, -0.25, 0.0)]
green = Color4 0 1 0 1
displayPoints points =
renderPrimitive Points $ mapM_ (\(x, y, z)-> vertex $ Vertex3 x y z) points
displayLines points =
renderPrimitive Lines $ mapM_ (\(x, y, z)-> vertex $ Vertex3 x y z) points
mainDisplay = do
clearColor $= Color4 1 1 1 1 -- Hintergrundfarbe festlegen
clear [ColorBuffer] -- Fensterinhalt l"oschen
currentColor $= Color4 0 0 0 1 -- Farbe auf Schwarz setzen
displayPoints beispielPunkte -- -- Punkte anzeigen
currentColor $= green
displayLines beispielPunkte-- Linien zeichnen
flush -- Alle Buffer flushen
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 5/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Grundgerust
Ist im CVS als Basis.hs eingecheckt.
Fur 3D ware es besser, wenn man interaktiv die Sichtverandern kann
In Basis3D.hs ist ein Grundgerust dafur
Zunachst aber egal, da erstmal nur 2D-Grafiken gezeichnetwerden
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 6/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Grundgerust
Ist im CVS als Basis.hs eingecheckt.
Fur 3D ware es besser, wenn man interaktiv die Sichtverandern kann
In Basis3D.hs ist ein Grundgerust dafur
Zunachst aber egal, da erstmal nur 2D-Grafiken gezeichnetwerden
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 6/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Grundgerust
Ist im CVS als Basis.hs eingecheckt.
Fur 3D ware es besser, wenn man interaktiv die Sichtverandern kann
In Basis3D.hs ist ein Grundgerust dafur
Zunachst aber egal, da erstmal nur 2D-Grafiken gezeichnetwerden
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 6/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Einstiegsaufgabe: Funktionsgraphen zeichnen
Aufgabe
plot :: GLfloat -> (GLfloat -> GLfloat) -> GLfloat -> GLfloat -> [Coord]
plot delta f start stop = ...
Berechne Funktionswerte (bzw. Koordinaten) furf start, f (start+delta), f (start+2*delta), . . . , f stop
Implementiere zur besseren Darstellung:bestScale :: [Coord] -> [Coord]
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 7/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
2D Turtle-Grafiken
Zustand einer Schildkrote (in Record-Syntax):
data Turtle = Turtle
{ xpos :: GLfloat,
ypos :: GLfloat,
angle :: GLfloat,
painting :: Bool
tcolor :: Color4 GLfloat
}
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 8/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
2D Turtle-Grafiken
Befehle fur die Schildkrote
data TurtleAction =
Move GLfloat
| Turn GLfloat
| FlipPaint
| SetColor (Color4 GLfloat)
Schildkrotenprogramme sind Listen vom Typ [TurtleAction].
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 9/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Befehle abarbeiten
Aufgabe
Implementieren Sie:applyAction :: TurtleAction -> Turtle -> Turtle
Nur Move ist nicht ganz leicht:
winkelImGradmaß = (winkelImBogenmaß/π) · 180
α
a
b
c
•sin(α) = a/hcos(α) = b/h
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 10/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Turtle-Programme ausfuhren
type TurtleProgram = [TurtleAction]
Aufgabe
Implementieren Sie in Haskell ein Programm, dass eine initialeSchildkrote und ein Schildkrotenprogramm erwartet, und die vonder Schildkrote bei Ausfuhrung des Schildkrotenprogrammserzeugte Grafik mittels OpenGL darstellt.
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 11/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Turtleprogramme schreiben
‘Haus vom Nikolaus“
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 12/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Turtleprogramme schreiben
nEck
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 13/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Turtleprogramme schreiben
nnEck
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 14/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
Turtleprogramme schreiben
Pythagoras-Baum
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 15/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
3D-Turtle
Unser Haskell-Datentyp wird dementsprechend erweitert in:
data Turtle = Turtle
{ xpos :: GLfloat,
ypos :: GLfloat,
angle :: GLfloat,
painting :: Bool,
tcolor :: Color4 GLfloat,
-- neu:
zpos :: GLfloat,
angleXZ :: GLfloat
}
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 16/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
3D-Turtle: Move-Operation
x
y
z
α
β r = Lange des Vektors (blau)Projektionen auf die einzelnen Achsen:
x = r · cosα · sinβy = r · sinα · sinβz = r · cosβ
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 17/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
3D-Turtle: Befehle
data TurtleAction =
Move GLfloat
| Turn GLfloat
| FlipPaint
| SetColor (Color4 GLfloat)
| TurnXZ GLfloat
Aufgabe
Anzeige von 3-Turtle-Grafiken implementieren. (applyActionmodifizieren) etc.
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 18/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
3D-Turtleprogramme schreiben
wuerfel :: GLfloat -> [TurtleProgram]
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 19/21
OpenGL & GLUT 2D-Turtle 3D-Turtle
3D-Turtleprogramme schreiben
Stufenpyramide aus Wurfeln
Praktikum BKSPP: Blatt 3 – WS 2014/15 – D. Sabel 20/21