Skip to content

Commit cb85482

Browse files
committed
Added ColorTriangle example.
1 parent 805206e commit cb85482

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{-
2+
ColorTriangle.hs (adapted from triangles.cpp which is (c) The Red Book
3+
Authors.)
4+
Copyright (c) Sven Panne 2014 <[email protected]>
5+
This file is part of HOpenGL and distributed under a BSD-style license
6+
See the file GLUT/LICENSE
7+
8+
A variation of Triangles.hs, adding colors to vertices via interleaved vertex
9+
attributes.
10+
-}
11+
12+
import Control.Monad
13+
import Foreign.Marshal.Array
14+
import Foreign.Ptr
15+
import Foreign.Storable
16+
import Graphics.UI.GLUT
17+
import Prelude hiding ( init )
18+
import LoadShaders
19+
20+
bufferOffset :: Integral a => a -> Ptr b
21+
bufferOffset = plusPtr nullPtr . fromIntegral
22+
23+
data Descriptor = Descriptor VertexArrayObject ArrayIndex NumArrayIndices
24+
25+
data ColoredVertex = ColoredVertex (Vertex2 GLfloat) (Color3 GLfloat)
26+
27+
instance Storable ColoredVertex where
28+
sizeOf ~(ColoredVertex v c) = sizeOf v + sizeOf c
29+
alignment ~(ColoredVertex v _) = alignment v
30+
peek ptr = do v <- peek (castPtr ptr)
31+
c <- peekByteOff (castPtr ptr) (sizeOf v)
32+
return $ ColoredVertex v c
33+
poke ptr (ColoredVertex v c) = do poke (castPtr ptr) v
34+
pokeByteOff (castPtr ptr) (sizeOf v) c
35+
36+
init :: IO Descriptor
37+
init = do
38+
triangles <- genObjectName
39+
bindVertexArrayObject $= Just triangles
40+
41+
let vertices = [
42+
-- Triangle 1
43+
ColoredVertex (Vertex2 (-0.90) (-0.90)) (Color3 1 0 0),
44+
ColoredVertex (Vertex2 0.85 (-0.90)) (Color3 0 1 0),
45+
ColoredVertex (Vertex2 (-0.90) 0.85 ) (Color3 0 0 1),
46+
-- Triangle 2
47+
ColoredVertex (Vertex2 0.90 (-0.85)) (Color3 0 1 1),
48+
ColoredVertex (Vertex2 0.90 0.90 ) (Color3 1 0 1),
49+
ColoredVertex (Vertex2 (-0.85) 0.90 ) (Color3 1 1 0)]
50+
numVertices = length vertices
51+
vertexSize = sizeOf (head vertices)
52+
53+
arrayBuffer <- genObjectName
54+
bindBuffer ArrayBuffer $= Just arrayBuffer
55+
withArray vertices $ \ptr -> do
56+
let size = fromIntegral (numVertices * vertexSize)
57+
bufferData ArrayBuffer $= (size, ptr, StaticDraw)
58+
59+
program <- loadShaders [
60+
ShaderInfo VertexShader (FileSource "color_triangles.vert"),
61+
ShaderInfo FragmentShader (FileSource "color_triangles.frac")]
62+
currentProgram $= Just program
63+
64+
let firstIndex = 0
65+
vPosition = AttribLocation 0
66+
vColor = AttribLocation 1
67+
vertexAttribPointer vPosition $=
68+
(ToFloat,
69+
VertexArrayDescriptor 2 Float (fromIntegral vertexSize)
70+
(bufferOffset (firstIndex * vertexSize)))
71+
vertexAttribArray vPosition $= Enabled
72+
let colorOffset = case head vertices of ~(ColoredVertex v _) -> sizeOf v
73+
vertexAttribPointer vColor $=
74+
(ToFloat,
75+
VertexArrayDescriptor 3 Float (fromIntegral vertexSize)
76+
(bufferOffset ((firstIndex * vertexSize) +
77+
fromIntegral colorOffset)))
78+
vertexAttribArray vColor $= Enabled
79+
80+
return $
81+
Descriptor triangles (fromIntegral firstIndex) (fromIntegral numVertices)
82+
83+
display :: Descriptor -> DisplayCallback
84+
display (Descriptor triangles firstIndex numVertices) = do
85+
clear [ ColorBuffer ]
86+
bindVertexArrayObject $= Just triangles
87+
drawArrays Triangles firstIndex numVertices
88+
flush
89+
90+
main :: IO ()
91+
main = do
92+
(progName, _args) <- getArgsAndInitialize
93+
initialDisplayMode $= [ RGBAMode ]
94+
initialWindowSize $= Size 512 512
95+
initialContextVersion $= (4, 3)
96+
initialContextFlags $= [ DebugContext ]
97+
initialContextProfile $= [ CoreProfile ]
98+
_ <- createWindow progName
99+
descriptor <- init
100+
displayCallback $= display descriptor
101+
mainLoop

examples/Misc/ColorTriangle/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
HC_SEARCH_PATHS := ../../RedBook8/common/
2+
include ../../examples.mk
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 430 core
2+
3+
in vec4 color;
4+
out vec4 fColor;
5+
6+
void
7+
main()
8+
{
9+
fColor = color;
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#version 430 core
2+
3+
layout(location = 0) in vec4 vPosition;
4+
layout(location = 1) in vec4 vColor;
5+
out vec4 color;
6+
7+
void
8+
main()
9+
{
10+
gl_Position = vPosition;
11+
color = vColor;
12+
}

examples/Misc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
SUBDIRS := ColorTriangle
12
include ../examples.mk

0 commit comments

Comments
 (0)