September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,3 +1,4 @@
In this task, the goal is to display a smooth shaded triangle with OpenGL.
[[image:Triangle.png|right|thumb|150px|Triangle created using C example compiled with [[GCC]] 4.1.2 and [[freeglut3]].]]
<br><br>

View file

@ -1,87 +1,91 @@
// triangle displays a smooth shaded triangle with OpenGL.
package main
import (
"log"
"runtime"
"time"
gl "github.com/chsc/gogl/gl21"
"github.com/mewmew/glfw/win"
gl "github.com/chsc/gogl/gl21"
"github.com/go-gl/glfw/v3.2/glfw"
"log"
"runtime"
)
// Window dimensions.
const (
Width = 640
Height = 480
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()
// OpenGL requires a dedicated OS thread.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Open window with the specified dimensions.
err := win.Open(Width, Height)
if err != nil {
log.Fatalln(err)
}
defer win.Close()
err := glfw.Init()
check(err)
defer glfw.Terminate()
// Initiate viewport.
resize(Width, Height)
// Open window with the specified dimensions.
window, err := glfw.CreateWindow(Width, Height, "Triangle", nil, nil)
check(err)
// Register that we are interested in receiving close and resize events.
win.EnableCloseChan()
win.EnableResizeChan()
window.MakeContextCurrent()
// 60 frames per second.
c := time.Tick(time.Second / 60)
err = gl.Init()
check(err) /* may need to comment out this line for this program to work on Windows 10 */
// Event loop.
for {
select {
case <-win.CloseChan:
return
case e := <-win.ResizeChan:
resize(e.Width, e.Height)
case <-c:
draw()
}
}
// Initiate viewport.
resize(Width, Height)
// Register that we are interested in receiving close and resize events.
window.SetCloseCallback(func(w *glfw.Window) {
return
})
window.SetSizeCallback(func(w *glfw.Window, width, height int) {
resize(width, height)
})
for !window.ShouldClose() {
draw()
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)
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.ClearColor(0.3, 0.3, 0.3, 0.0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.ShadeModel(gl.SMOOTH)
gl.ShadeModel(gl.SMOOTH)
gl.LoadIdentity()
gl.Translatef(-15.0, -15.0, 0.0)
gl.LoadIdentity()
gl.Translatef(-15.0, -15.0, 0.0)
gl.Begin(gl.TRIANGLES)
gl.Begin(gl.TRIANGLES)
gl.Color3f(1.0, 0.0, 0.0)
gl.Vertex2f(0.0, 0.0)
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, 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.Color3f(0.0, 0.0, 1.0)
gl.Vertex2f(0.0, 30.0)
gl.End()
gl.End()
win.SwapBuffers()
gl.Flush()
}

View file

@ -0,0 +1,3 @@
using Makie
mesh([(0.0, 0.0), (0.5, 1.0), (1.0, 0.0)], color = [:red, :green, :blue], shading = false)

27
Task/OpenGL/Ol/opengl.ol Normal file
View file

@ -0,0 +1,27 @@
(import (lib gl))
(gl:set-window-title "Rosettacode OpenGL example")
(import (OpenGL version-1-0))
(glShadeModel GL_SMOOTH)
(glClearColor 0.3 0.3 0.3 1)
(glMatrixMode GL_PROJECTION)
(glLoadIdentity)
(glOrtho -30.0 30.0 -30.0 30.0 -30.0 30.0)
(gl:set-renderer (lambda (mouse)
(glClear GL_COLOR_BUFFER_BIT)
(glMatrixMode GL_MODELVIEW)
(glLoadIdentity)
(glTranslatef -15.0 -15.0 0.0)
(glBegin GL_TRIANGLES)
(glColor3f 1.0 0.0 0.0)
(glVertex2f 0.0 0.0)
(glColor3f 0.0 1.0 0.0)
(glVertex2f 30.0 0.0)
(glColor3f 0.0 0.0 1.0)
(glVertex2f 0.0 30.0)
(glEnd)
))