OpenGL: Difference between revisions

→‎{{header|Go}}: Updated example so it now works - at least on Ubuntu.
(→‎{{header|Go}}: Updated example so it now works - at least on Ubuntu.)
Line 1,000:
 
=={{header|Go}}==
{{libheader|GoGL}}
{{broken|Go}} (See talk page)
{{libheader|GLFW 3.2 for Go}}
 
{{works with|Ubuntu 16.04}}
The win package used in this example is a simple wrapper around [http://www.glfw.org/ GLFW].
<lang go>package main
 
<lang go>// triangle displays a smooth shaded triangle with OpenGL.
package main
 
import (
gl "github.com/chsc/gogl/gl21"
"log"
"github.com/mewmewgo-gl/glfw/winv3.2/glfw"
"runtime"
"log"
"time"
"runtime"
 
gl "github.com/chsc/gogl/gl21"
"github.com/mewmew/glfw/win"
)
 
// Window dimensions.
const (
Width = 640
Height = 480
)
 
func check(err error) {
if err != nil {
log.Fatal(err)
}
 
func main() {
// OpenGL requires a dedicated OS thread.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
 
err := glfw.Init()
// Open window with the specified dimensions.
check(err)
err := win.Open(Width, Height)
defer glfw.Terminate()
if err != nil {
log.Fatalln(err)
}
defer win.Close()
 
// Open window with the specified dimensions.
// Initiate viewport.
resize window, err := glfw.CreateWindow(Width, Height, "Triangle", nil, nil)
check(err)
 
window.MakeContextCurrent()
// Register that we are interested in receiving close and resize events.
win.EnableCloseChan()
win.EnableResizeChan()
 
err = gl.Init()
// 60 frames per second.
check(err)
c := time.Tick(time.Second / 60)
 
// Initiate viewport.
// Event loop.
err := win.Open resize(Width, Height)
for {
 
select {
// Register that we are interested in receiving close and resize events.
case <-win.CloseChan:
window.SetCloseCallback(func(w *glfw.Window) {
return
case e := <-win.ResizeChan:
})
resize(e.Width, e.Height)
window.SetSizeCallback(func(w *glfw.Window, width, height int) {
case <-c:
resize(width, height)
draw()
})
}
 
}
for !window.ShouldClose() {
draw()
win window.SwapBuffers()
glfw.PollEvents()
}
}
 
// resize resizes the window to the specified dimensions.
func resize(width, height int) {
gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0)
gl.MatrixMode(gl.MODELVIEW)
}
 
// draw draws the triangle.
func draw() {
gl.ClearColor(0.3, 0.3, 0.3, 0.0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
 
gl.ShadeModel(gl.SMOOTH)
 
gl.LoadIdentity()
gl.Translatef(-15.0, -15.0, 0.0)
 
gl.Begin(gl.TRIANGLES)
 
gl.Color3f(1.0, 0.0, 0.0)
gl.Vertex2f(0.0, 0.0)
 
gl.Color3f(0.0, 1.0, 0.0)
gl.Vertex2f(30.0, 0.0)
 
gl.Color3f(0.0, 0.0, 1.0)
gl.Vertex2f(0.0, 30.0)
 
gl.End()
 
gl.Flush()
win.SwapBuffers()
}</lang>
</lang>
 
=={{header|Haskell}}==
9,490

edits