8000 dry shader and buffer creation. explore dry sketches · raedatoui/learn-opengl-golang@c95ec73 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit c95ec73

Browse files
author
HUGE | Raed Atoui
committed
dry shader and buffer creation. explore dry sketches
1 parent 7f5e2b5 commit c95ec73

File tree

11 files changed

+385
-720
lines changed

11 files changed

+385
-720
lines changed

sections/base.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Slide interface {
2525
GetHeader() string
2626
GetSubHeader() string
2727
SetName(s string)
28+
GetName() string
2829
GetColorHex() string
2930
HandleKeyboard(k glfw.Key, s int, a glfw.Action, m glfw.ModifierKey, keys map[glfw.Key]bool)
3031
HandleMousePosition(xpos, ypos float64)

sections/getstarted/3.shaders.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,24 @@ func (hs *ShaderEx4) InitGL() error {
169169

170170
return nil
171171
}
172+
173+
func (hs *ShaderEx4) createBuffers() {
174+
var vertices = []float32{
175+
// Positions // Colors
176+
0.5, -0.5, 0.0, 1.0, 0.0, 0.0, // Bottom Right
177+
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, // Bottom Left
178+
0.0, 0.5, 0.0, 0.0, 0.0, 1.0, // Top
179+
}
180+
attr := glutils.NewAttributesMap()
181+
attr.Add(hs.shader.Attributes["position"], 3, 0)
182+
183+
hs.va = glutils.VertexArray{
184+
Data: vertices,
185+
Stride: 6,
186+
Normalized: false,
187+
DrawMode: gl.STATIC_DRAW,
188+
Attributes: attr,
189+
}
190+
191+
hs.va.Setup()
192+
}

sections/getstarted/4.textures.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@ func (ht *HelloTextures) createBuffers(vertices []float32) {
3434
0, 1, 3, // First Triangle
3535
1, 2, 3, // Second Triangle
3636
}
37-
38-
attr := make(glutils.AttributesMap)
37+
attr := glutils.NewAttributesMap()
3938
attr.Add(ht.shader.Attributes["position"], 3, 0)
40-
attr.Add(ht.shader.Attributes["color"], 3, 3)
39+
attr.Add(ht.shader.Attributes["color"], 3, 2)
4140
attr.Add(ht.shader.Attributes["texCoord"], 2, 6)
4241

43-
v := glutils.VertexArray{
42+
ht.va = glutils.VertexArray{
4443
Data: vertices,
4544
Indices: indices,
4645
Stride: 8,
4746
Normalized: false,
4847
DrawMode: gl.STATIC_DRAW,
4948
Attributes: attr,
5049
}
51-
v.Setup()
50+
ht.va.Setup()
5251
}
5352

5453
func (ht *HelloTextures) InitGL() error {
@@ -88,7 +87,6 @@ func (ht *HelloTextures) Draw() {
8887
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
8988
gl.ClearColor(ht.Color32.R, ht.Color32.G, ht.Color32.B, ht.Color32.A)
9089

91-
9290
// Bind Textures using texture units
9391
gl.ActiveTexture(gl.TEXTURE0)
9492
gl.BindTexture(gl.TEXTURE_2D, ht.texture1)

sections/getstarted/5.transformations.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func (ht *HelloTransformations) InitGL() error {
3939
-0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left
4040
}
4141

42-
attr := make(glutils.AttributesMap)
43-
attr[ht.shader.Attributes["position"]] = [2]int{3, 0}
44-
attr[ht.shader.Attributes["texCoord"]] = [2]int{2, 6}
42+
attr := glutils.NewAttributesMap()
43+
attr.Add(ht.shader.Attributes["position"], 3, 0)
44+
attr.Add(ht.shader.Attributes["texCoord"], 2, 6)
4545

4646
indices := []uint32{ // Note that we start from 0!
4747
0, 1, 3, // First Triangle

sections/getstarted/6.coordinates.go

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@ import (
1111
type HelloCoordinates struct {
1212
sections.BaseSketch
1313
shader glutils.Shader
14-
va glutils.VertexArray
14+
va glutils.VertexArray
1515
texture1, texture2 uint32
1616
transform mgl32.Mat4
1717
cubePositions []mgl32.Mat4
1818
rotationAxis mgl32.Vec3
1919
}
2020

21-
func (hc *HelloCoordinates) InitGL() error {
22-
hc.Name = "6. Coordinate Systems"
21+
func (hc *HelloCoordinates) GetHeader() string {
22+
return "6. Coordinate Systems"
23+
}
2324

25+
func (hc *HelloCoordinates) createShader() error {
2426
var err error
2527
hc.shader, err = glutils.NewShader(
2628
"_assets/getting_started/6.coordinates/coordinate.vs",
2729
"_assets/getting_started/6.coordinates/coordinate.frag", "")
2830
if err != nil {
2931
return err
3032
}
31-
33+
return nil
34+
}
35+
func (hc *HelloCoordinates) createBuffers() error {
3236
vertices := []float32{
3337
-0.5, -0.5, -0.5, 0.0, 0.0,
3438
0.5, -0.5, -0.5, 1.0, 0.0,
@@ -72,18 +76,22 @@ func (hc *HelloCoordinates) InitGL() error {
7276
-0.5, 0.5, 0.5, 0.0, 0.0,
7377
-0.5, 0.5, -0.5, 0.0, 1.0,
7478
}
75-
attr := make(glutils.AttributesMap)
76-
attr[hc.shader.Attributes["position"]] = [2]int{0, 3}
77-
attr[hc.shader.Attributes["texCoord"]] = [2]int{3, 2}
79+
80+
attr := glutils.NewAttributesMap()
81+
attr.Add(hc.shader.Attributes["position"], 3, 0)
82+
attr.Add(hc.shader.Attributes["texCoord"], 2, 3)
7883

7984
hc.va = glutils.VertexArray{
80-
Data: vertices,
81-
Stride: 5,
82-
DrawMode: gl.STATIC_DRAW,
85+
Data: vertices,
86+
Stride: 5,
87+
DrawMode: gl.STATIC_DRAW,
8388
Normalized: false,
8489
Attributes: attr,
8590
}
91+
hc.va.Setup()
92+
8693
hc.rotationAxis = mgl32.Vec3{1.0, 0.3, 0.5}.Normalize()
94+
8795
hc.cubePositions = []mgl32.Mat4{
8896
mgl32.Translate3D(0.0, 0.0, 0.0),
8997
mgl32.Translate3D(2.0, 5.0, -15.0),
@@ -96,8 +104,10 @@ func (hc *HelloCoordinates) InitGL() error {
96104
mgl32.Translate3D(1.5, 0.2, -1.5),
97105
mgl32.Translate3D(-1.3, 1.0, -1.5),
98106
}
107+
return nil
108+
}
99109

100-
110+
func (hc *HelloCoordinates) createTextures() error {
101111
// Texture 1
102112
if tex, err := glutils.NewTexture(gl.REPEAT, gl.REPEAT, gl.LINEAR, gl.LINEAR, "_assets/images/container.png"); err != nil {
103113
return err
@@ -111,56 +121,70 @@ func (hc *HelloCoordinates) InitGL() error {
111121
} else {
112122
hc.texture2 = tex
113123
}
124+
return nil
125+
}
114126

127+
func (hc *HelloCoordinates) InitGL() error {
128+
if err := hc.createShader(); err != nil {
129+
return err
130+
}
131+
if err := hc.createBuffers(); err != nil {
132+
return err
133+
}
134+
if err := hc.createTextures(); err != nil {
135+
return err
136+
}
115137
return nil
116138
}
117139

118-
func (hc *HelloCoordinates) Draw() {
140+
func (hc *HelloCoordinates) clear() {
119141
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
120142
gl.ClearColor(hc.Color32.R, hc.Color32.G, hc.Color32.B, hc.Color32.A)
121-
143+
}
144+
func (hc *HelloCoordinates) setTextures() {
122145
// Bind Textures using texture units
123146
gl.ActiveTexture(gl.TEXTURE0)
124147
gl.BindTexture(gl.TEXTURE_2D, hc.texture1)
125-
loc1 := gl.GetUniformLocation(hc.shader, gl.Str("ourTexture1\x00"))
126-
gl.Uniform1i(loc1, 0)
148+
gl.Uniform1i(hc.shader.Uniforms["ourTexture1"], 0)
127149

128150
gl.ActiveTexture(gl.TEXTURE1)
129151
gl.BindTexture(gl.TEXTURE_2D, hc.texture2)
130-
loc2 := gl.GetUniformLocation(hc.shader, gl.Str("ourTexture2\x00"))
131-
gl.Uniform1i(loc2, 1)
132-
133-
// Activate shader
134-
gl.UseProgram(hc.shader)
135-
152+
gl.Uniform1i(hc.shader.Uniforms["ourTexture2"], 1)
153+
}
154+
func (hc *HelloCoordinates) setTransformations() {
136155
// Create transformations
137156
view := mgl32.Translate3D(0.0, 0.0, -3.0)
138157
projection := mgl32.Perspective(45.0, sections.RATIO, 0.1, 100.0)
139-
// Get their uniform location
140-
modelLoc := gl.GetUniformLocation(hc.shader, gl.Str("model\x00"))
141-
viewLoc := gl.GetUniformLocation(hc.shader, gl.Str("view\x00"))
142-
projLoc := gl.GetUniformLocation(hc.shader, gl.Str("projection\x00"))
143158
// Pass the matrices to the shader
144-
gl.UniformMatrix4fv(viewLoc, 1, false, &view[0])
159+
gl.UniformMatrix4fv(hc.shader.Uniforms["view"], 1, false, &view[0])
145160
// Note: currently we set the projection matrix each frame,
146161
// but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
147-
gl.UniformMatrix4fv(projLoc, 1, false, &projection[0])
162+
gl.UniformMatrix4fv(hc.shader.Uniforms["projection"], 1, false, &projection[0])
163+
}
148164

165+
func (hc *HelloCoordinates) renderVertexArray() {
149166
// Draw container
150-
gl.BindVertexArray(hc.vao)
151-
167+
gl.BindVertexArray(hc.va.Vao)
152168
for i := 0; i < 10; i++ {
153169
// Calculate the model matrix for each object and pass it to shader before drawing
154170
model := hc.cubePositions[i]
155171

156172
angle := float32(glfw.GetTime()) * float32(i+1)
157173

158174
model = model.Mul4(mgl32.HomogRotate3D(angle, hc.rotationAxis))
159-
gl.UniformMatrix4fv(modelLoc, 1, false, &model[0])
175+
gl.UniformMatrix4fv(hc.shader.Uniforms["model"], 1, false, &model[0])
160176
gl.DrawArrays(gl.TRIANGLES, 0, 36)
161177
}
162178
gl.BindVertexArray(0)
163179
}
180+
func (hc *HelloCoordinates) Draw() {
181+
hc.clear()
182+
// Activate shader
183+
gl.UseProgram(hc.shader.Program)
184+
hc.setTextures()
185+
hc.setTransformations()
186+
hc.renderVertexArray()
187+
}
164188

165189
func (hc *HelloCoordinates) Close() {
166190
hc.shader.Delete()

0 commit comments

Comments
 (0)
0