Skip to content

Commit 8f092fd

Browse files
committed
opengl lights
1 parent 07a03cd commit 8f092fd

File tree

6 files changed

+161
-9
lines changed

6 files changed

+161
-9
lines changed

gogl/camera.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package gogl
2+
3+
import (
4+
"github.com/go-gl/mathgl/mgl32"
5+
"math"
6+
)
7+
8+
type Direction int
9+
10+
const (
11+
Forward Direction = iota
12+
Backward
13+
Left
14+
Right
15+
Nowhere
16+
)
17+
18+
type Camera struct {
19+
Position mgl32.Vec3
20+
Front mgl32.Vec3
21+
Up mgl32.Vec3
22+
Right mgl32.Vec3
23+
24+
WorldUp mgl32.Vec3
25+
26+
Yaw float32
27+
Pitch float32
28+
MovementSpeed float32
29+
MouseSens float32
30+
Zoom float32
31+
}
32+
33+
func NewCamera(position mgl32.Vec3, worldUp mgl32.Vec3, yaw, pitch, speed, sens float32) *Camera {
34+
camera := Camera{}
35+
camera.Position = position
36+
camera.WorldUp = worldUp
37+
camera.Yaw = yaw
38+
camera.Pitch = pitch
39+
camera.MovementSpeed = speed
40+
camera.MouseSens = sens
41+
camera.updateVectors()
42+
return &camera
43+
44+
}
45+
46+
func (camera *Camera) UpdateCamera(direction Direction, deltaT, xoffset, yoffset float32) {
47+
magnitude := camera.MovementSpeed * deltaT
48+
//fmt.Println("mag:", magnitude)
49+
switch direction {
50+
case Forward:
51+
camera.Position = camera.Position.Add(camera.Front.Mul(magnitude))
52+
case Backward:
53+
camera.Position = camera.Position.Sub(camera.Front.Mul(magnitude))
54+
case Left:
55+
camera.Position = camera.Position.Sub(camera.Right.Mul(magnitude))
56+
case Right:
57+
camera.Position = camera.Position.Add(camera.Right.Mul(magnitude))
58+
case Nowhere:
59+
}
60+
61+
xoffset *= camera.MouseSens
62+
yoffset *= camera.MouseSens
63+
64+
camera.Yaw += xoffset
65+
camera.Pitch -= yoffset
66+
67+
if camera.Pitch > 90.0 {
68+
camera.Pitch = 90
69+
}
70+
if camera.Pitch < -90.0 {
71+
camera.Pitch = -90
72+
}
73+
74+
//fmt.Println("xoffset:", xoffset, "yoffset:", yoffset, "yaw:", camera.Yaw, "pitch:", camera.Pitch)
75+
76+
camera.updateVectors()
77+
78+
}
79+
80+
func (camera *Camera) GetViewMatrix() mgl32.Mat4 {
81+
center := camera.Position.Add(camera.Front)
82+
return mgl32.LookAt(camera.Position.X(), camera.Position.Y(), camera.Position.Z(), center.X(), center.Y(), center.Z(), camera.Up.X(), camera.Up.Y(), camera.Up.Z())
83+
}
84+
85+
func (camera *Camera) updateVectors() {
86+
front := mgl32.Vec3{float32(math.Cos(float64(mgl32.DegToRad(camera.Yaw))) * math.Cos(float64(mgl32.DegToRad(camera.Pitch)))),
87+
float32(math.Sin(float64(mgl32.DegToRad(camera.Pitch)))),
88+
float32(math.Sin(float64(mgl32.DegToRad(camera.Yaw))) * math.Cos(float64(mgl32.DegToRad(camera.Pitch))))}
89+
90+
camera.Front = front.Normalize()
91+
camera.Right = camera.Front.Cross(camera.WorldUp).Normalize()
92+
camera.Up = camera.Right.Cross(camera.Front).Normalize()
93+
94+
}

gogl/gogl.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,9 @@ func UnbindVertexArray() {
182182
func UseProgram(programID ProgramID) {
183183
gl.UseProgram(uint32(programID))
184184
}
185+
186+
func TriangleNormal(p1, p2, p3 mgl32.Vec3) mgl32.Vec3 {
187+
U := p2.Sub(p1)
188+
V := p3.Sub(p1)
189+
return U.Cross(V).Normalize()
190+
}

gogl/shader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ func (shader *Shader) SetMat4(name string, mat mgl32.Mat4) {
5050
gl.UniformMatrix4fv(location, 1, false, &m4[0])
5151
}
5252

53+
func (shader *Shader) SetVec3(name string, v mgl32.Vec3) {
54+
name_cstr := gl.Str(name + "\x00")
55+
location := gl.GetUniformLocation(uint32(shader.id), name_cstr)
56+
v3 := [3]float32(v)
57+
gl.Uniform3fv(location, 1, &v3[0])
58+
}
59+
5360
func getModifiedTime(filePath string) (time.Time, error) {
5461

5562
file, err := os.Stat(filePath)

hellotriangle/hellotriangle.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Make shader hotloading fail gracefully and retry
2-
// Make an attribpointer helper function
3-
// Make a texture
4-
51
package main
62

73
import (
@@ -13,7 +9,7 @@ import (
139
"time"
1410
)
1511

16-
const winWidth = 1280
12+
const winWidth = 720
1713
const winHeight = 720
1814

1915
func main() {
@@ -91,19 +87,45 @@ func main() {
9187
-0.5, 0.5, 0.5, 0.0, 0.0,
9288
-0.5, 0.5, -0.5, 0.0, 1.0}
9389

90+
normals := make([]float32, 36*3)
91+
for tri := 0; tri < 12; tri++ {
92+
index := tri * 15
93+
p1 := mgl32.Vec3{vertices[index], vertices[index+1], vertices[index+2]}
94+
index += 5
95+
p2 := mgl32.Vec3{vertices[index], vertices[index+1], vertices[index+2]}
96+
index += 5
97+
p3 := mgl32.Vec3{vertices[index], vertices[index+1], vertices[index+2]}
98+
normal := gogl.TriangleNormal(p1, p2, p3)
99+
normals[tri*9] = normal.X()
100+
normals[tri*9+1] = normal.Y()
101+
normals[tri*9+2] = normal.Z()
102+
103+
normals[tri*9+3] = normal.X()
104+
normals[tri*9+4] = normal.Y()
105+
normals[tri*9+5] = normal.Z()
106+
107+
normals[tri*9+6] = normal.X()
108+
normals[tri*9+7] = normal.Y()
109+
normals[tri*9+8] = normal.Z()
110+
111+
}
112+
94113
cubePositions := []mgl32.Vec3{
95114
mgl32.Vec3{0.0, 0.0, 0.0},
96115
mgl32.Vec3{2.0, 5.0, -10.0},
97116
mgl32.Vec3{1.0, -5.0, 1.0}}
98117

99-
gogl.GenBindBuffer(gl.ARRAY_BUFFER)
100118
VAO := gogl.GenBindVertexArray()
119+
gogl.GenBindBuffer(gl.ARRAY_BUFFER)
101120
gogl.BufferDataFloat(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)
102-
103121
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 5*4, nil)
104122
gl.EnableVertexAttribArray(0)
105123
gl.VertexAttribPointer(1, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))
106124
gl.EnableVertexAttribArray(1)
125+
gogl.GenBindBuffer(gl.ARRAY_BUFFER)
126+
gogl.BufferDataFloat(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW)
127+
gl.VertexAttribPointer(2, 3, gl.FLOAT, false, 3*4, nil)
128+
gl.EnableVertexAttribArray(2)
107129
gogl.UnbindVertexArray()
108130

109131
keyboardState := sdl.GetKeyboardState()
@@ -149,6 +171,8 @@ func main() {
149171
viewMatrix := camera.GetViewMatrix()
150172
shaderProgram.SetMat4("projection", projectionMatrix)
151173
shaderProgram.SetMat4("view", viewMatrix)
174+
shaderProgram.SetVec3("lightPos", mgl32.Vec3{2.0, 5.0, 5.0})
175+
shaderProgram.SetVec3("lightColor", mgl32.Vec3{1.0, 0.0, 0.0})
152176
gogl.BindTexture(texture)
153177
gogl.BindVertexArray(VAO)
154178
for i, pos := range cubePositions {

hellotriangle/shaders/hello.vert

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#version 330 core
22
layout (location = 0) in vec3 aPos;
33
layout (location = 1) in vec2 aTexCoord;
4+
layout (location = 2) in vec3 aNormal;
45

56
out vec2 TexCoord;
67

8+
out vec3 FragPos;
9+
out vec3 Normal;
10+
711
uniform mat4 model;
812
uniform mat4 view;
913
uniform mat4 projection;
1014

1115
void main()
1216
{
13-
gl_Position = projection * view * model * vec4(aPos,1.0f);
17+
Normal = aNormal;
18+
FragPos = vec3(model * vec4(aPos,1.0));
19+
gl_Position = projection * view * vec4(FragPos,1.0f);
1420
TexCoord = vec2(aTexCoord.x,1.0 - aTexCoord.y);
1521
}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
#version 330 core
22
out vec4 FragColor;
3+
4+
35
in vec2 TexCoord;
6+
in vec3 Normal;
7+
in vec3 FragPos;
8+
9+
410

511
uniform sampler2D texture1;
12+
uniform vec3 lightPos;
13+
uniform vec3 lightColor; //color/brightness
14+
615

716
void main() {
8-
FragColor = texture(texture1,TexCoord);
17+
18+
// diffuse
19+
vec3 lightDir = normalize(lightPos - FragPos);
20+
float diff = max(dot(Normal,lightDir),0.0);
21+
vec3 diffuse = diff * lightColor;
22+
vec3 ambient = lightColor * 0.3;
23+
FragColor = vec4(ambient + diffuse,1.0) * texture(texture1,TexCoord);
924
}

0 commit comments

Comments
 (0)