Skip to content

Commit 61d9b83

Browse files
author
rui.zheng
committed
7.1,7.2 camera
1 parent 46a590f commit 61d9b83

File tree

9 files changed

+599
-6
lines changed

9 files changed

+599
-6
lines changed

1.getting_started/6.2.coordinate_systems_depth/coordinate_systems_depth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func main() {
176176
log.Fatal(err)
177177
}
178178
projection := mgl32.Perspective(mgl32.DegToRad(45.0), float32(screenWidth)/float32(screenHeight), 0.1, 100.0)
179-
// projection = mgl32.Ortho(0.0, 800.0, 0.0, 600.0, 0.1, 100.0)
179+
// projection = mgl32.Ortho(-5.0, 5.0, -4.0, 3.0, 0.1, 10.0)
180180
if err := shader.SetUniformMatrixName("projection", false, projection); err != nil {
181181
log.Fatal(err)
182182
}

1.getting_started/6.3.coordinate_systems_multiple/coordinate_systems_multiple.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,14 @@ func main() {
210210
gl.ActiveTexture(gl.TEXTURE1)
211211
texture2.Use()
212212

213-
for _, pos := range cubePositions {
214-
model := mgl32.HomogRotate3D(float32(glfw.GetTime()), mgl32.Vec3{0.5, 1.0, 0.0})
213+
for i, pos := range cubePositions {
214+
angle := 20.0 * float32(i)
215+
216+
// exercise 3
217+
// if i%3 == 0 {
218+
// angle = float32(glfw.GetTime() * 25.0)
219+
// }
220+
model := mgl32.HomogRotate3D(float32(mgl32.DegToRad(angle)), mgl32.Vec3{0.5, 1.0, 0.0})
215221
model = mgl32.Translate3D(pos.Elem()).Mul4(model)
216222
if err := shader.SetUniformMatrixName("model", false, model); err != nil {
217223
log.Fatal(err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
4+
in vec2 TexCoord;
5+
6+
// texture samplers
7+
uniform sampler2D texture1;
8+
uniform sampler2D texture2;
9+
10+
void main()
11+
{
12+
// linearly interpolate between both textures (80% container, 20% awesomeface)
13+
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
layout (location = 1) in vec2 aTexCoord;
4+
5+
out vec2 TexCoord;
6+
7+
uniform mat4 model, view, projection;
8+
9+
void main()
10+
{
11+
gl_Position = projection * view * model * vec4(aPos, 1.0);
12+
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"math"
6+
"runtime"
7+
8+
"github.com/ginuerzh/learnopengl/utils/shader"
9+
"github.com/ginuerzh/learnopengl/utils/texture"
10+
11+
"github.com/go-gl/gl/v3.3-core/gl"
12+
"github.com/go-gl/glfw/v3.2/glfw"
13+
"github.com/go-gl/mathgl/mgl32"
14+
)
15+
16+
const (
17+
screenWidth = 800
18+
screenHeight = 600
19+
)
20+
21+
var (
22+
vertices = []float32{
23+
-0.5, -0.5, -0.5, 0.0, 0.0,
24+
0.5, -0.5, -0.5, 1.0, 0.0,
25+
0.5, 0.5, -0.5, 1.0, 1.0,
26+
0.5, 0.5, -0.5, 1.0, 1.0,
27+
-0.5, 0.5, -0.5, 0.0, 1.0,
28+
-0.5, -0.5, -0.5, 0.0, 0.0,
29+
30+
-0.5, -0.5, 0.5, 0.0, 0.0,
31+
0.5, -0.5, 0.5, 1.0, 0.0,
32+
0.5, 0.5, 0.5, 1.0, 1.0,
33+
0.5, 0.5, 0.5, 1.0, 1.0,
34+
-0.5, 0.5, 0.5, 0.0, 1.0,
35+
-0.5, -0.5, 0.5, 0.0, 0.0,
36+
37+
-0.5, 0.5, 0.5, 1.0, 0.0,
38+
-0.5, 0.5, -0.5, 1.0, 1.0,
39+
-0.5, -0.5, -0.5, 0.0, 1.0,
40+
-0.5, -0.5, -0.5, 0.0, 1.0,
41+
-0.5, -0.5, 0.5, 0.0, 0.0,
42+
-0.5, 0.5, 0.5, 1.0, 0.0,
43+
44+
0.5, 0.5, 0.5, 1.0, 0.0,
45+
0.5, 0.5, -0.5, 1.0, 1.0,
46+
0.5, -0.5, -0.5, 0.0, 1.0,
47+
0.5, -0.5, -0.5, 0.0, 1.0,
48+
0.5, -0.5, 0.5, 0.0, 0.0,
49+
0.5, 0.5, 0.5, 1.0, 0.0,
50+
51+
-0.5, -0.5, -0.5, 0.0, 1.0,
52+
0.5, -0.5, -0.5, 1.0, 1.0,
53+
0.5, -0.5, 0.5, 1.0, 0.0,
54+
0.5, -0.5, 0.5, 1.0, 0.0,
55+
-0.5, -0.5, 0.5, 0.0, 0.0,
56+
-0.5, -0.5, -0.5, 0.0, 1.0,
57+
58+
-0.5, 0.5, -0.5, 0.0, 1.0,
59+
0.5, 0.5, -0.5, 1.0, 1.0,
60+
0.5, 0.5, 0.5, 1.0, 0.0,
61+
0.5, 0.5, 0.5, 1.0, 0.0,
62+
-0.5, 0.5, 0.5, 0.0, 0.0,
63+
-0.5, 0.5, -0.5, 0.0, 1.0,
64+
}
65+
cubePositions = []mgl32.Vec3{
66+
{0.0, 0.0, 0.0},
67+
{2.0, 5.0, -15.0},
68+
{-1.5, -2.2, -2.5},
69+
{-3.8, -2.0, -12.3},
70+
{2.4, -0.4, -3.5},
71+
{-1.7, 3.0, -7.5},
72+
{1.3, -2.0, -2.5},
73+
{1.5, 2.0, -2.5},
74+
{1.5, 0.2, -1.5},
75+
{-1.3, 1.0, -1.5},
76+
}
77+
)
78+
79+
func init() {
80+
runtime.LockOSThread()
81+
log.SetFlags(log.Lshortfile | log.LstdFlags)
82+
}
83+
84+
func main() {
85+
// glfw: initialize
86+
if err := glfw.Init(); err != nil {
87+
log.Fatal(err)
88+
}
89+
// glfw: terminate, clearing all previously allocated GLFW resources.
90+
defer glfw.Terminate()
91+
92+
// glfw: configure
93+
glfw.WindowHint(glfw.ContextVersionMajor, 3)
94+
glfw.WindowHint(glfw.ContextVersionMinor, 3)
95+
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
96+
// there is no need to explicitly create and bind a VAO when using the compatibility profile
97+
// see https://www.opengl.org/discussion_boards/showthread.php/199916-vertex-array-and-buffer-objects?p=1288280&viewfull=1#post1288280
98+
// glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCompatProfile)
99+
100+
// glfw window creation
101+
window, err := glfw.CreateWindow(screenWidth, screenHeight, "LearnOpenGL", nil, nil)
102+
if err != nil {
103+
log.Fatal(err)
104+
}
105+
defer window.Destroy()
106+
107+
window.MakeContextCurrent()
108+
window.SetFramebufferSizeCallback(frameBufferSizeCallback)
109+
110+
// gl: initializes the OpenGL bindings by loading the function pointers
111+
// (for each OpenGL function) from the active OpenGL context.
112+
if err := gl.Init(); err != nil {
113+
log.Fatal(err)
114+
}
115+
116+
shader, err := shader.NewShader("7.1.camera.vs", "7.1.camera.fs")
117+
if err != nil {
118+
log.Fatal(err)
119+
}
120+
121+
var vao, vbo uint32
122+
gl.GenVertexArrays(1, &vao)
123+
defer gl.DeleteVertexArrays(1, &vao)
124+
125+
gl.GenBuffers(1, &vbo)
126+
defer gl.DeleteBuffers(1, &vbo)
127+
128+
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
129+
gl.BindVertexArray(vao)
130+
131+
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
132+
gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)
133+
134+
// position attribute
135+
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))
136+
gl.EnableVertexAttribArray(0)
137+
138+
// texture coord attribute
139+
gl.VertexAttribPointer(1, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))
140+
gl.EnableVertexAttribArray(1)
141+
142+
// texture 1
143+
texture1 := texture.NewTexture2D()
144+
texture1.Use()
145+
// set the texture wrapping parameters
146+
texture1.SetParameter(gl.TEXTURE_WRAP_S, gl.REPEAT)
147+
texture1.SetParameter(gl.TEXTURE_WRAP_T, gl.REPEAT)
148+
// set texture filtering parameters
149+
texture1.SetParameter(gl.TEXTURE_MIN_FILTER, gl.LINEAR)
150+
texture1.SetParameter(gl.TEXTURE_MAG_FILTER, gl.LINEAR)
151+
// load image, create texture and generate mipmaps
152+
image, err := texture1.Load("../../resources/textures/container.jpg", false, false)
153+
if err != nil {
154+
log.Fatal(err)
155+
}
156+
log.Println("container.jpg", image.Rect, image.Stride, len(image.Pix))
157+
158+
texture2 := texture.NewTexture2D()
159+
texture2.Use()
160+
texture2.SetParameter(gl.TEXTURE_WRAP_S, gl.REPEAT)
161+
texture2.SetParameter(gl.TEXTURE_WRAP_T, gl.REPEAT)
162+
texture2.SetParameter(gl.TEXTURE_MIN_FILTER, gl.LINEAR)
163+
texture2.SetParameter(gl.TEXTURE_MAG_FILTER, gl.LINEAR)
164+
image, err = texture2.Load("../../resources/textures/awesomeface.jpg", false, true)
165+
if err != nil {
166+
log.Fatal(err)
167+
}
168+
log.Println("awesomeface.jpg", image.Rect, image.Stride, len(image.Pix))
169+
170+
// note that this is allowed,
171+
// the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object
172+
// so afterwards we can safely unbind
173+
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
174+
175+
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
176+
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
177+
// gl.BindVertexArray(0)
178+
179+
shader.Use()
180+
if err := shader.SetUniformName("texture1", 0); err != nil {
181+
log.Fatal(err)
182+
}
183+
if err := shader.SetUniformName("texture2", 1); err != nil {
184+
log.Fatal(err)
185+
}
186+
187+
projection := mgl32.Perspective(mgl32.DegToRad(45.0), float32(screenWidth)/float32(screenHeight), 0.1, 100.0)
188+
// projection = mgl32.Ortho(0.0, 800.0, 0.0, 600.0, 0.1, 100.0)
189+
if err := shader.SetUniformMatrixName("projection", false, projection); err != nil {
190+
log.Fatal(err)
191+
}
192+
193+
gl.Enable(gl.DEPTH_TEST)
194+
195+
// render loop
196+
for !window.ShouldClose() {
197+
// input
198+
processInput(window)
199+
200+
// render
201+
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
202+
gl.Clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT)
203+
204+
// bind textures on corresponding texture units
205+
gl.ActiveTexture(gl.TEXTURE0)
206+
texture1.Use()
207+
gl.ActiveTexture(gl.TEXTURE1)
208+
texture2.Use()
209+
210+
radius := 10.0
211+
camX := math.Sin(glfw.GetTime()) * radius
212+
camZ := math.Cos(glfw.GetTime()) * radius
213+
view := mgl32.LookAtV(
214+
mgl32.Vec3{float32(camX), 0, float32(camZ)},
215+
mgl32.Vec3{},
216+
mgl32.Vec3{0, 1, 0},
217+
)
218+
if err := shader.SetUniformMatrixName("view", false, view); err != nil {
219+
log.Fatal(err)
220+
}
221+
222+
for i, pos := range cubePositions {
223+
angle := 20.0 * float32(i)
224+
225+
model := mgl32.HomogRotate3D(float32(mgl32.DegToRad(angle)), mgl32.Vec3{0.5, 1.0, 0.0})
226+
model = mgl32.Translate3D(pos.Elem()).Mul4(model)
227+
if err := shader.SetUniformMatrixName("model", false, model); err != nil {
228+
log.Fatal(err)
229+
}
230+
gl.DrawArrays(gl.TRIANGLES, 0, 36)
231+
}
232+
233+
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
234+
window.SwapBuffers()
235+
glfw.PollEvents()
236+
}
237+
}
238+
239+
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
240+
func processInput(w *glfw.Window) {
241+
if w.GetKey(glfw.KeyEscape) == glfw.Press {
242+
w.SetShouldClose(true)
243+
}
244+
}
245+
246+
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
247+
func frameBufferSizeCallback(w *glfw.Window, width int, height int) {
248+
// make sure the viewport matches the new window dimensions; note that width and
249+
// height will be significantly larger than specified on retina displays.
250+
gl.Viewport(0, 0, int32(width), int32(height))
251+
// log.Printf("frameBufferSizeCallback (%d, %d)", width, height)
252+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
4+
in vec2 TexCoord;
5+
6+
// texture samplers
7+
uniform sampler2D texture1;
8+
uniform sampler2D texture2;
9+
10+
void main()
11+
{
12+
// linearly interpolate between both textures (80% container, 20% awesomeface)
13+
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos;
3+
layout (location = 1) in vec2 aTexCoord;
4+
5+
out vec2 TexCoord;
6+
7+
uniform mat4 model, view, projection;
8+
9+
void main()
10+
{
11+
gl_Position = projection * view * model * vec4(aPos, 1.0);
12+
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
13+
}

0 commit comments

Comments
 (0)