Skip to content

Commit 20aa51b

Browse files
committed
update for go1
1 parent 64a2e78 commit 20aa51b

File tree

22 files changed

+2507
-2535
lines changed

22 files changed

+2507
-2535
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Tutorials for OpenGL in Go
2+
3+
Tutorials translated from "NeHe Productions":http://nehe.gamedev.net/ into Go with SDL.
4+
5+
## Dependencies
6+
7+
First we need to install the dependencies:
8+
9+
go get github.com/banthar/gl github.com/banthar/glu github.com/banthar/Go-SDL/sdl
10+
11+
## Running the examples
12+
13+
You can use `go run`, like this:
14+
15+
cd lesson01
16+
go run lesson01.go
17+
18+
Please note that starting with lesson06, you _have_ to cd into the directory
19+
because we start using external data.

README.textile

Lines changed: 0 additions & 4 deletions
This file was deleted.

lesson01/Makefile

Lines changed: 0 additions & 13 deletions
This file was deleted.

lesson01/lesson01.go

Lines changed: 145 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
11
package main
22

33
import (
4-
"fmt"
5-
"gl"
6-
"math"
7-
"os"
8-
"github.com/banthar/Go-SDL/sdl"
4+
"fmt"
5+
"github.com/banthar/Go-SDL/sdl"
6+
"github.com/banthar/gl"
7+
"math"
8+
"os"
99
)
1010

1111
const (
12-
SCREEN_WIDTH = 1366
13-
SCREEN_HEIGHT = 768
14-
SCREEN_BPP = 32
12+
SCREEN_WIDTH = 1366
13+
SCREEN_HEIGHT = 768
14+
SCREEN_BPP = 32
1515
)
1616

1717
var (
18-
surface *sdl.Surface
18+
surface *sdl.Surface
1919
)
2020

2121
// release/destroy our resources and restoring the old desktop
2222
func Quit(status int) {
23-
// clean up the window
24-
sdl.Quit()
23+
// clean up the window
24+
sdl.Quit()
2525

26-
// and exit appropriately
27-
os.Exit(status)
26+
// and exit appropriately
27+
os.Exit(status)
2828
}
2929

3030
// reset our viewport after a window resize
3131
func resizeWindow(width, height int) {
32-
// protect against a divide by zero
33-
if height == 0 {
34-
height = 1
35-
}
36-
37-
// Setup our viewport
38-
gl.Viewport(0, 0, width, height)
39-
40-
// change to the projection matrix and set our viewing volume.
41-
gl.MatrixMode(gl.PROJECTION)
42-
gl.LoadIdentity()
43-
44-
// aspect ratio
45-
aspect := gl.GLdouble(width / height)
46-
47-
// Set our perspective.
48-
// This code is equivalent to using gluPerspective as in the original tutorial.
49-
var fov, near, far gl.GLdouble
50-
fov = 45.0
51-
near = 0.1
52-
far = 100.0
53-
top := gl.GLdouble(math.Tan(float64(fov*math.Pi/360.0))) * near
54-
bottom := -top
55-
left := aspect * bottom
56-
right := aspect * top
57-
gl.Frustum(float64(left), float64(right), float64(bottom), float64(top), float64(near), float64(far))
58-
59-
// Make sure we're changing the model view and not the projection
60-
gl.MatrixMode(gl.MODELVIEW)
61-
62-
// Reset the view
63-
gl.LoadIdentity()
32+
// protect against a divide by zero
33+
if height == 0 {
34+
height = 1
35+
}
36+
37+
// Setup our viewport
38+
gl.Viewport(0, 0, width, height)
39+
40+
// change to the projection matrix and set our viewing volume.
41+
gl.MatrixMode(gl.PROJECTION)
42+
gl.LoadIdentity()
43+
44+
// aspect ratio
45+
aspect := gl.GLdouble(width / height)
46+
47+
// Set our perspective.
48+
// This code is equivalent to using gluPerspective as in the original tutorial.
49+
var fov, near, far gl.GLdouble
50+
fov = 45.0
51+
near = 0.1
52+
far = 100.0
53+
top := gl.GLdouble(math.Tan(float64(fov*math.Pi/360.0))) * near
54+
bottom := -top
55+
left := aspect * bottom
56+
right := aspect * top
57+
gl.Frustum(float64(left), float64(right), float64(bottom), float64(top), float64(near), float64(far))
58+
59+
// Make sure we're changing the model view and not the projection
60+
gl.MatrixMode(gl.MODELVIEW)
61+
62+
// Reset the view
63+
gl.LoadIdentity()
6464
}
6565

6666
// handle key press events
6767
func handleKeyPress(keysym sdl.Keysym) {
68-
switch keysym.Sym {
69-
case sdl.K_ESCAPE:
70-
Quit(0)
71-
case sdl.K_F1:
72-
sdl.WM_ToggleFullScreen(surface)
73-
}
68+
switch keysym.Sym {
69+
case sdl.K_ESCAPE:
70+
Quit(0)
71+
case sdl.K_F1:
72+
sdl.WM_ToggleFullScreen(surface)
73+
}
7474
}
7575

7676
// general OpenGL initialization
7777
func initGL() {
78-
// enable smooth shading
79-
gl.ShadeModel(gl.SMOOTH)
78+
// enable smooth shading
79+
gl.ShadeModel(gl.SMOOTH)
8080

81-
// Set the background to black
82-
gl.ClearColor(0.0, 0.0, 0.0, 0.0)
81+
// Set the background to black
82+
gl.ClearColor(0.0, 0.0, 0.0, 0.0)
8383

84-
// Depth buffer setup
85-
gl.ClearDepth(1.0)
84+
// Depth buffer setup
85+
gl.ClearDepth(1.0)
8686

87-
// Enable depth testing
88-
gl.Enable(gl.DEPTH_TEST)
87+
// Enable depth testing
88+
gl.Enable(gl.DEPTH_TEST)
8989

90-
// The type of test
91-
gl.DepthFunc(gl.LEQUAL)
90+
// The type of test
91+
gl.DepthFunc(gl.LEQUAL)
9292

93-
// Nicest perspective correction
94-
gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
93+
// Nicest perspective correction
94+
gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
9595
}
9696

9797
// used to calculate fps
@@ -100,90 +100,89 @@ var frames uint32
100100

101101
// Here goes our drawing code
102102
func drawGLScene() {
103-
// Clear the screen and depth buffer
104-
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
105-
106-
// reset the view
107-
gl.LoadIdentity()
108-
109-
// Draw to the screen
110-
sdl.GL_SwapBuffers()
111-
112-
// Gather our frames per second
113-
frames++
114-
t := sdl.GetTicks()
115-
if t-t0 >= 5000 {
116-
seconds := (t - t0) / 1000.0
117-
fps := frames / seconds
118-
fmt.Println(frames, "frames in", seconds, "seconds =", fps, "FPS")
119-
t0 = t
120-
frames = 0
121-
}
103+
// Clear the screen and depth buffer
104+
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
105+
106+
// reset the view
107+
gl.LoadIdentity()
108+
109+
// Draw to the screen
110+
sdl.GL_SwapBuffers()
111+
112+
// Gather our frames per second
113+
frames++
114+
t := sdl.GetTicks()
115+
if t-t0 >= 5000 {
116+
seconds := (t - t0) / 1000.0
117+
fps := frames / seconds
118+
fmt.Println(frames, "frames in", seconds, "seconds =", fps, "FPS")
119+
t0 = t
120+
frames = 0
121+
}
122122
}
123123

124124
func main() {
125-
// Initialize SDL
126-
if sdl.Init(sdl.INIT_VIDEO) < 0 {
127-
panic("Video initialization failed: " + sdl.GetError())
128-
}
129-
130-
// flags to pass to sdl.SetVideoMode
131-
videoFlags := sdl.OPENGL // Enable OpenGL in SDL
132-
videoFlags |= sdl.DOUBLEBUF // Enable double buffering
133-
videoFlags |= sdl.HWPALETTE // Store the palette in hardware
134-
// FIXME: this causes segfault.
135-
// videoFlags |= sdl.RESIZABLE // Enable window resizing
136-
137-
// get a SDL surface
138-
surface = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, uint32(videoFlags))
139-
140-
// verify there is a surface
141-
if surface == nil {
142-
panic("Video mode set failed: " + sdl.GetError())
143-
Quit(1)
144-
}
145-
146-
// When this function is finished, clean up and exit.
147-
defer Quit(0)
148-
149-
// Sets up OpenGL double buffering
150-
sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
151-
152-
// Execute everything needed for OpenGL
153-
initGL()
154-
155-
// Resize the initial window
156-
resizeWindow(SCREEN_WIDTH, SCREEN_HEIGHT)
157-
158-
// wait for events
159-
running := true
160-
isActive := true
161-
for running {
162-
for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
163-
switch e := ev.(type) {
164-
case *sdl.ActiveEvent:
165-
isActive = e.Gain != 0
166-
case *sdl.ResizeEvent:
167-
width, height := int(e.W), int(e.H)
168-
surface = sdl.SetVideoMode(width, height, SCREEN_BPP, uint32(videoFlags))
169-
170-
if surface == nil {
171-
fmt.Println("Could not get a surface after resize:", sdl.GetError())
172-
Quit(1)
173-
}
174-
resizeWindow(width, height)
175-
case *sdl.KeyboardEvent:
176-
if (e.Type == sdl.KEYDOWN) {
177-
handleKeyPress(e.Keysym)
178-
}
179-
case *sdl.QuitEvent:
180-
running = false
181-
}
182-
}
183-
184-
// draw the scene
185-
if isActive {
186-
drawGLScene()
187-
}
188-
}
125+
// Initialize SDL
126+
if sdl.Init(sdl.INIT_VIDEO) < 0 {
127+
panic("Video initialization failed: " + sdl.GetError())
128+
}
129+
130+
// flags to pass to sdl.SetVideoMode
131+
videoFlags := sdl.OPENGL // Enable OpenGL in SDL
132+
videoFlags |= sdl.DOUBLEBUF // Enable double buffering
133+
videoFlags |= sdl.HWPALETTE // Store the palette in hardware
134+
videoFlags |= sdl.RESIZABLE // Enable window resizing
135+
136+
// get a SDL surface
137+
surface = sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, uint32(videoFlags))
138+
139+
// verify there is a surface
140+
if surface == nil {
141+
panic("Video mode set failed: " + sdl.GetError())
142+
Quit(1)
143+
}
144+
145+
// When this function is finished, clean up and exit.
146+
defer Quit(0)
147+
148+
// Sets up OpenGL double buffering
149+
sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
150+
151+
// Execute everything needed for OpenGL
152+
initGL()
153+
154+
// Resize the initial window
155+
resizeWindow(SCREEN_WIDTH, SCREEN_HEIGHT)
156+
157+
// wait for events
158+
running := true
159+
isActive := true
160+
for running {
161+
for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
162+
switch e := ev.(type) {
163+
case *sdl.ActiveEvent:
164+
isActive = e.Gain != 0
165+
case *sdl.ResizeEvent:
166+
width, height := int(e.W), int(e.H)
167+
surface = sdl.SetVideoMode(width, height, SCREEN_BPP, uint32(videoFlags))
168+
169+
if surface == nil {
170+
fmt.Println("Could not get a surface after resize:", sdl.GetError())
171+
Quit(1)
172+
}
173+
resizeWindow(width, height)
174+
case *sdl.KeyboardEvent:
175+
if e.Type == sdl.KEYDOWN {
176+
handleKeyPress(e.Keysym)
177+
}
178+
case *sdl.QuitEvent:
179+
running = false
180+
}
181+
}
182+
183+
// draw the scene
184+
if isActive {
185+
drawGLScene()
186+
}
187+
}
189188
}

lesson02/Makefile

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)