GithubHelp home page GithubHelp logo

example's People

Contributors

antichris avatar depy avatar dertseha avatar dmitshur avatar errcw avatar hajimehoshi avatar pwaller avatar segfault88 avatar tapir avatar tdryer avatar userab1236872 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

example's Issues

Specify permissive license.

We should make it clear that anyone can copy/paste code from examples and not feel bad or uncertain about being able to do so. That's one of the reasons the examples exist.

What's a good fit? MIT is permissive, but it makes people have to preserve license and copyright notice, which is too much effort. Public domain perhaps? Or is there a better solution?

Proposal: Rename repository to "example".

This repo contains examples of usage, and I'd like it to set the best possible example.

Yet the repo's name is deviating slightly from idiomatic Go naming patterns. It should be singular "example" rather than "examples", so that the import path "example/name-of-example" reads more cleanly, and for consistency.

See https://dmitri.shuralyov.com/idiomatic-go#use-singular-form-for-collection-repo-folder-name for rationale.

If there are no objections, I'd like to rename it to follow idiomatic Go style and set a better example. GitHub will setup redirects from old repo name, so it should be fairly harmless.

/cc @tapir @slimsag

More information in examples

I'd like to try and render a particle system in gl with golang, but it's difficult to understand what is happening even with the example cube. Could you provide a simple example in working with particles, or provide a basic tutorial, or annotate the code?

Error when binding in loop

I tried making something combining learnopengl and the 4.1 example and the following code works, however when i uncomment any the of the commented out function calls in the render loop my program will crash with a weird error.

package main

import (
	"log"
	"strings"

	"github.com/go-gl/gl/v3.3-core/gl"
	"github.com/go-gl/glfw/v3.3/glfw"
)

func main() {
	// init glfw
	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	// init window
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	// tutorial says required for mac
	glfw.WindowHint(glfw.OpenGLForwardCompatible, gl.TRUE)

	window, err := glfw.CreateWindow(800, 600, "LearnOpenGL in Go", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	// init gl (Glow)
	if err := gl.Init(); err != nil {
		panic(err)
	}

	// setup gl
	gl.Viewport(0, 0, 800, 600)

	// handle window resizing
	window.SetFramebufferSizeCallback(SizeChanged)

	// shaders
	vertexSource := `
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
	gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}` + "\x00"

	fragmentSource := `
#version 330 core
out vec4 FragColor;
	
void main()
{
	FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}` + "\x00"
	var vertexShader uint32
	vertexShader = gl.CreateShader(gl.VERTEX_SHADER)
	csources, free := gl.Strs(vertexSource)
	gl.ShaderSource(vertexShader, 1, csources, nil)
	free()
	gl.CompileShader(vertexShader)

	checkShader(vertexShader)

	var fragmentShader uint32
	fragmentShader = gl.CreateShader(gl.FRAGMENT_SHADER)
	csources, free = gl.Strs(fragmentSource)
	gl.ShaderSource(fragmentShader, 1, csources, nil)
	free()
	gl.CompileShader(fragmentShader)

	checkShader(fragmentShader)

	// combine shaders
	var shaderProg uint32
	shaderProg = gl.CreateProgram()
	gl.AttachShader(shaderProg, vertexShader)
	gl.AttachShader(shaderProg, fragmentShader)
	gl.LinkProgram(shaderProg)

	// delete shaders
	gl.DeleteShader(vertexShader)
	gl.DeleteShader(fragmentShader)

	vertices := []float32{
		-0.5, -0.5, 0.0,
		0.5, -0.5, 0.0,
		0.0, 0.5, 0.0,
	}

	var vbo, vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.GenBuffers(1, &vbo)

	gl.BindVertexArray(vao)

	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	gl.VertexAttribPointerWithOffset(0, 3, gl.FLOAT, false, 3*4, 0)
	gl.EnableVertexAttribArray(0)

	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
	//gl.BindVertexArray(0)
	gl.UseProgram(shaderProg)
	// frame loop
	for !window.ShouldClose() {
		processInput(window)

		gl.ClearColor(0.2, 0.3, 0.3, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)

		//gl.UseProgram(shaderProg)
		//gl.BindVertexArray(vao)
		gl.DrawArrays(gl.TRIANGLES, 0, 3)

		window.SwapBuffers()
		glfw.PollEvents()
	}
}

func SizeChanged(w *glfw.Window, width, height int) {
	gl.Viewport(0, 0, int32(width), int32(height))
}

func processInput(w *glfw.Window) {
	if w.GetKey(glfw.KeyEscape) == glfw.Press {
		w.SetShouldClose(true)
	}
}

func checkShader(shader uint32) {
	var status int32
	gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
	if status == gl.FALSE {
		var logLength int32
		gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)

		log := strings.Repeat("\x00", int(logLength+1))
		gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
		print(log)
		panic("")
	}
}

The error

Exception 0xc0000005 0x0 0xe90 0x7ffcf4e9d1e9
PC=0x7ffcf4e9d1e9
signal arrived during external code execution

runtime.cgocall(0x7ff6fa6ecc4b, 0xc00011bce0)
	C:/Program Files/Go/src/runtime/cgocall.go:157 +0x4a fp=0xc00011bcb0 sp=0xc00011bc78 pc=0x7ff6fa5e48ea
github.com/go-gl/gl/v3.3-core/gl._Cfunc_glowUseProgram(0x7ffcf4cbd820, 0x3)
	_cgo_gotypes.go:22058 +0x4e fp=0xc00011bce0 sp=0xc00011bcb0 pc=0x7ff6fa69e16e
github.com/go-gl/gl/v3.3-core/gl.UseProgram(0x3)
	C:/Users/*****/go/pkg/mod/github.com/go-gl/[email protected]/v3.3-core/gl/package.go:13033 +0x2c fp=0xc00011bd00 sp=0xc00011bce0 pc=0x7ff6fa69fecc
main.main()
	c:/Projects/Go/LearnOpenGL/main.go:116 +0x7d8 fp=0xc00011bf80 sp=0xc00011bd00 pc=0x7ff6fa6c2698
runtime.main()
	C:/Program Files/Go/src/runtime/proc.go:250 +0x1be fp=0xc00011bfe0 sp=0xc00011bf80 pc=0x7ff6fa61e93e
runtime.goexit()
	C:/Program Files/Go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc00011bfe8 sp=0xc00011bfe0 pc=0x7ff6fa648321

goroutine 2 [force gc (idle)]:
runtime.gopark(0x7ff6fa75aad8, 0x7ff6fa80d750, 0x11, 0x14, 0x1)
	C:/Program Files/Go/src/runtime/proc.go:381 +0xfd fp=0xc000055f88 sp=0xc000055f58 pc=0x7ff6fa61edbd
runtime.goparkunlock(0x0?, 0x0?, 0x0?, 0x0?)
	C:/Program Files/Go/src/runtime/proc.go:387 +0x2a fp=0xc000055fb8 sp=0xc000055f88 pc=0x7ff6fa61ee4a
runtime.forcegchelper()
	C:/Program Files/Go/src/runtime/proc.go:305 +0xb0 fp=0xc000055fe0 sp=0xc000055fb8 pc=0x7ff6fa61eb90
runtime.goexit()
	C:/Program Files/Go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000055fe8 sp=0xc000055fe0 pc=0x7ff6fa648321
created by runtime.init.6
	C:/Program Files/Go/src/runtime/proc.go:293 +0x25

goroutine 3 [GC sweep wait]:
runtime.gopark(0x7ff6fa75aad8, 0x7ff6fa80d8a0, 0xc, 0x14, 0x1)
	C:/Program Files/Go/src/runtime/proc.go:381 +0xfd fp=0xc000057f58 sp=0xc000057f28 pc=0x7ff6fa61edbd
runtime.goparkunlock(0x0?, 0x0?, 0x0?, 0x0?)
	C:/Program Files/Go/src/runtime/proc.go:387 +0x2a fp=0xc000057f88 sp=0xc000057f58 pc=0x7ff6fa61ee4a
runtime.bgsweep(0x0?)
	C:/Program Files/Go/src/runtime/mgcsweep.go:278 +0x98 fp=0xc000057fc8 sp=0xc000057f88 pc=0x7ff6fa606ef8
runtime.gcenable.func1()
	C:/Program Files/Go/src/runtime/mgc.go:178 +0x26 fp=0xc000057fe0 sp=0xc000057fc8 pc=0x7ff6fa5fb4e6
runtime.goexit()
	C:/Program Files/Go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000057fe8 sp=0xc000057fe0 pc=0x7ff6fa648321
created by runtime.gcenable
	C:/Program Files/Go/src/runtime/mgc.go:178 +0x6b

goroutine 4 [GC scavenge wait]:
runtime.gopark(0x7ff6fa75aad8, 0x7ff6fa80d9a0, 0xd, 0x14, 0x2)
	C:/Program Files/Go/src/runtime/proc.go:381 +0xfd fp=0xc000065f48 sp=0xc000065f18 pc=0x7ff6fa61edbd
runtime.goparkunlock(0x7ff6fa75f420?, 0x1?, 0x0?, 0x0?)
	C:/Program Files/Go/src/runtime/proc.go:387 +0x2a fp=0xc000065f78 sp=0xc000065f48 pc=0x7ff6fa61ee4a
runtime.(*scavengerState).park(0x7ff6fa80d9a0)
	C:/Program Files/Go/src/runtime/mgcscavenge.go:400 +0x4b fp=0xc000065fa0 sp=0xc000065f78 pc=0x7ff6fa6049ab
runtime.bgscavenge(0x0?)
	C:/Program Files/Go/src/runtime/mgcscavenge.go:628 +0x45 fp=0xc000065fc8 sp=0xc000065fa0 pc=0x7ff6fa604f85
runtime.gcenable.func2()
	C:/Program Files/Go/src/runtime/mgc.go:179 +0x26 fp=0xc000065fe0 sp=0xc000065fc8 pc=0x7ff6fa5fb486
runtime.goexit()
	C:/Program Files/Go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000065fe8 sp=0xc000065fe0 pc=0x7ff6fa648321
created by runtime.gcenable
	C:/Program Files/Go/src/runtime/mgc.go:179 +0xaa

goroutine 5 [finalizer wait]:
runtime.gopark(0x7ff6fa75a978, 0x7ff6fa862580, 0x10, 0x14, 0x1)
	C:/Program Files/Go/src/runtime/proc.go:381 +0xfd fp=0xc000059e28 sp=0xc000059df8 pc=0x7ff6fa61edbd
runtime.runfinq()
	C:/Program Files/Go/src/runtime/mfinal.go:193 +0x107 fp=0xc000059fe0 sp=0xc000059e28 pc=0x7ff6fa5fa547
runtime.goexit()
	C:/Program Files/Go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000059fe8 sp=0xc000059fe0 pc=0x7ff6fa648321
created by runtime.createfing
	C:/Program Files/Go/src/runtime/mfinal.go:163 +0x50
rax     0x0
rbx     0xc00011bce0
rcx     0x3
rdi     0xc00011bce0
rsi     0xc000052820
rbp     0x36737ff5f0
rsp     0x36737ff5c8
r8      0xc00000cc00
r9      0x0
r10     0xa201879b5e5a7b70
r11     0x246
r12     0xc00011bac8
r13     0x0
r14     0xc000052000
r15     0x2030
rip     0x7ffcf4e9d1e9
rflags  0x10202
cs      0x33
fs      0x53
gs      0x2b

Go 1.6 -msan option error (memory sanitizer)

I just tried to run gl21 example with the new -msan option and it gives error (other projects give the same error)


==18024==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7a2877 (/tmp/go-build517668219/command-line-arguments/_obj/exe/cube+0x7a2877)
#1 0x793e3c (/tmp/go-build517668219/command-line-arguments/_obj/exe/cube+0x793e3c)
#2 0x78a7ff (/tmp/go-build517668219/command-line-arguments/_obj/exe/cube+0x78a7ff)
#3 0x51f3cf (/tmp/go-build517668219/command-line-arguments/_obj/exe/cube+0x51f3cf)


The command was:
$ CC=clang-3.8 go run -v -msan cube.go

go 1.6
clang 3.8
llvm 3.8
ubuntu 16.04

Should it even work or not?
I have (almost) no other problems with these bindings. Just curious.

Cannot get VBO to draw?

I'm not sure if I'm doing everything correctly here. Some help would be greatly appreciated:

I have a window setup using glfw and I have glortho setup. I have had luck drawing with a more "raw" open gl library. But for some reason I cannot do it with this library. My code is as follows(it is not full code but only opengl parts):

    vars:
    idxs = [...]uint32{0, 1, 2}

verts = [...]int32{10, 10, 0, 20, 20, 0, 40, 20, 0}


buf := GenBuffer()
BufferData(ARRAY_BUFFER, 9*4, verts[:], STATIC_READ)
buf.Bind(ARRAY_BUFFER)

bufel := GenBuffer()
BufferData(ELEMENT_ARRAY_BUFFER, 3*4, idxs[:], STATIC_READ)
bufel.Bind(ELEMENT_ARRAY_BUFFER)

buf.Bind(ARRAY_BUFFER)
EnableClientState(VERTEX_ARRAY)
VertexPointer(3, INT, 0, verts[:])
bufel.Bind(ELEMENT_ARRAY_BUFFER)
DrawElements(TRIANGLE_STRIP, 3, UNSIGNED_INT, idxs[:])
if GetError() != NO_ERROR {
    t.Error("slice failed")
}

DisableClientState(VERTEX_ARRAY)

I cannot get this to draw on my screen for some reason. Any tips?

Cel Shader Example

I like how simple this project is go-gl isn't trying to be a full engine just control over gl, which is nice because I want to build a 3D scene to be streamed.

Any recommendations on how to implement custom shaders? Itd be nice to be able to switch between them, it would create a mechanic.

gl.GenTextures

Hello,
In according to that line we create the texture

gl.GenTextures(1, &texture)

In according to example in documentation on page https://docs.gl/gl2/glGenTextures

glGenTextures(1, &texture_id);

glBindTexture(GL_TEXTURE_2D, texture_id);
....
glBindTexture(GL_TEXTURE_2D, 0);

I think the last line of function shall be
gl.BindTexture(GL_TEXTURE_2D, 0)

Thanks)

Handle leak on Windows at SetSize?

Just calling SetSize seems to cause handle leak. For example,

diff --git a/gl21-cube/cube.go b/gl21-cube/cube.go
index 5a84ebc..d7ff7a7 100644
--- a/gl21-cube/cube.go
+++ b/gl21-cube/cube.go
@@ -51,8 +51,13 @@ func main() {
        texture = newTexture("square.png")
        defer gl.DeleteTextures(1, &texture)

+       i := 0
        setupScene()
        for !window.ShouldClose() {
+               i++
+               if i % 60 == 0 {
+                       window.SetSize(800 + (i / 60), 600)
+               }
                drawScene()
                window.SwapBuffers()
                glfw.PollEvents()

keeps increasing the handle count on the task manager on Windows.

I tested this with glfw 3.2.

fatal error: checkptr: unsafe pointer arithmetic on Go 1.14rc1

Testing Go 1.14rc1 and building the examples with -race panics as below. This is caused by a new checker in 1.14 as described in https://tip.golang.org/doc/go1.14#compiler.

$ # checkout repo
$ cd gl41core-cube
$ go1.14rc1 build -race
$ ./gl41core-cube 
OpenGL version 4.1 INTEL-12.10.16
fatal error: checkptr: unsafe pointer arithmetic
goroutine 1 [running, locked to thread]:
runtime.throw(0x423c365, 0x23)
	/Users/me/sdk/go1.14rc1/src/runtime/panic.go:1112 +0x72 fp=0xc00014fc60 sp=0xc00014fc30 pc=0x4031f32
runtime.checkptrArithmetic(0xc, 0x0, 0x0, 0x0)
	/Users/me/sdk/go1.14rc1/src/runtime/checkptr.go:24 +0xce fp=0xc00014fc90 sp=0xc00014fc60 pc=0x40085ee
github.com/go-gl/gl/v4.1-core/gl.PtrOffset(...)
	/Users/me/go/src/github.com/go-gl/gl/v4.1-core/gl/conversions.go:55
main.main()
	/tmp/go-gl/example/gl41core-cube/cube.go:103 +0x110a fp=0xc00014ff88 sp=0xc00014fc90 pc=0x41656da
runtime.main()
	/Users/me/sdk/go1.14rc1/src/runtime/proc.go:203 +0x212 fp=0xc00014ffe0 sp=0xc00014ff88 pc=0x4034582
runtime.goexit()
	/Users/me/sdk/go1.14rc1/src/runtime/asm_amd64.s:1375 +0x1 fp=0xc00014ffe8 sp=0xc00014ffe0 pc=0x40606f1

gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))

I only had a quick glance at the pointer arithmetic but at a glance it wasn't obvious to me what the fix should be.

Orginally posted in the Gophers Slack channel. cc @dmitshur

c:\go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1

not even sure where this issue lies. go 1.19. windows 10 64-bit using winlibs-x86_64-posix-seh-gcc-12.1.0-llvm-14.0.6-mingw-w64ucrt-10.0.0-r3 from winlibs.com

confirmed 64-bit gcc.exe, c11.exe & go.exe. go and gcc are defined in path and i've confirmed this and am running these commands in a new cmd window with an up to date path. mingw and go paths have no spaces and aren't long

tried deleting appdata\local\go-build, tried using a brand new module, set GO111MODULE=on (suggested online), reinstalling go 1.19, trying 1.14, nothin

after downloading all packages:

C:\Users\Hoffry\go\pkg\mod\github.com\go-gl\[email protected]\gl41core-cube>go install -v github.com/go-gl/example/gl41core-cube
golang.org/x/image/math/f32
github.com/go-gl/mathgl/mgl32
github.com/go-gl/glfw/v3.3/glfw
github.com/go-gl/gl/v4.1-core/gl
github.com/go-gl/example/gl41core-cube
# github.com/go-gl/example/gl41core-cube
c:\go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
c:/portableapps/cpp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Hoffry\AppData\Local\Temp\go-link-1216772332\000026.o: in function `_cgo_preinit_init':
\\_\_\runtime\cgo/gcc_libinit_windows.c:40: undefined reference to `__imp___iob_func'
c:/portableapps/cpp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Hoffry\AppData\Local\Temp\go-link-1216772332\000026.o: in function `x_cgo_notify_runtime_init_done':
\\_\_\runtime\cgo/gcc_libinit_windows.c:105: undefined reference to `__imp___iob_func'
c:/portableapps/cpp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Hoffry\AppData\Local\Temp\go-link-1216772332\000026.o: in function `_cgo_beginthread':
\\_\_\runtime\cgo/gcc_libinit_windows.c:149: undefined reference to `__imp___iob_func'
c:/portableapps/cpp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Hoffry\AppData\Local\Temp\go-link-1216772332\000027.o: in function `x_cgo_thread_start':
\\_\_\runtime\cgo/gcc_util.c:18: undefined reference to `__imp___iob_func'
collect2.exe: error: ld returned 1 exit status

same result for go get -u github.com/go-gl/example/gl41core-cube && cd <path with cube.go> && go build cube.go

Examples are broken

The examples contain references to external resources (e.g. "github.com/go-gl/examples/data/bitmap_font.png"), which do not exist.

Edit: Sorry, I intended to put the issue in the gltext repo. But I guess this repo is part of the issue. However the gltext examples are broken!

Tiny stutters

I get tiny (perhaps 2-3 frame) stutters in this demo code on OS X El Capitan regardless of whether I compile / run with GOGC=off (so it's not GC pauses.) Is this normal? Can you help me narrow down what it could be, otherwise?

go version is go version go1.5.3 darwin/amd64 which as we know has concurrent GC anyway, but perhaps this is disabled by the requirement to lock OS thread.

Improve unfriendly requirement that examples must be run in their package directory.

Currently, both examples require loading some assets from disk, e.g.:

// Load the texture
texture, err := newTexture("square.png")
if err != nil {
    panic(err)
}

https://github.com/go-gl/examples/blob/45f0380c29532eac123c04e239943a4125955f8a/glfw31-gl41core-cube/cube.go#L82-L85

This means they'll work only if the working directory is the same as the Go package, otherwise there'll be a panic like:

panic: open square.png: no such file or directory

(huge stack trace)

This can be improved in many ways, and it's worth doing because this knowledge is hard to have when first looking at the examples. It's not described in any README, and even if it were, it's just unfriendly design.

Simple Enhancement

The panic(err) can be replaced with a more helpful error message, e.g.:

if err != nil {
    log.Fatalln("must be running in directory where the Go package is:", err)
}

And some notes can be added to README.

Advanced Enhancement

Assuming a proper Go installation and the source present in $GOPATH, it's possible to make the binary auto-detect where the assets folder is, and use that. For example:

func importPathToDir(importPath string) string {
    p, err := build.Import(importPath, "", build.FindOnly)
    if err != nil {
        log.Fatalln(err)
    }
    return p.Dir
}

var baseDir = importPathToDir("github.com/go-gl/examples/glfw31-gl41core-cube")

texture, err := newTexture(filepath.Join(baseDir, "square.png"))

This issue is extracted from #49 (comment).

how to convert glm to mgl32?

//------------------cpp glm code--------------------------------
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleTransform.vertexshader", "SingleColor.fragmentshader" );

// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");

// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
// Or, for an ortho camera :
//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates

// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around

Use a $PWD independent method to determine the location of the data directory

For testing purposes it will be desirable to run for example:

go run ${GOPATH}/src/github.com/go-gl/examples/nehe08/*.go

.. without being concerned about the run directory. Currently, I get [e] Failed to load texture: ../data/Glass.tga.

This package provides what looks to me to be a reasonable way of figuring out package-relative paths - I use it in glh to determine a font path.

frame rate drops proportionally to number of process instances

Hello!
I added SwapInterval(1) to example/gl21-cube and it gives me a nice 60 fps. But if i run a second cube.exe at the same time they both show only 60/2=30 fps. Third instance drops fps to 60/3 = 20 and so on.
CPU and memory usage is under 30%.
What is the reason, and how can i fix it?

(my system: AMD Athlon II X4, Win 7, Radeon HD 7800 series)

Errors in examples/glh/textureatlas

When running "$ go run main.go" in examples/glh/textureatlas, you get the following errors:

command-line-arguments

./main.go:49: not enough arguments in call to atlas.Bind
./main.go:60: not enough arguments in call to atlas.Unbind
./main.go:112: not enough arguments in call to a.Commit

It can be fixed with the following patch:

From 84adce516b2c5108dcc4c422509e0ac41771fea6 Mon Sep 17 00:00:00 2001
From: Andrew Appleby <[email protected]>
Date: Sat, 15 Dec 2012 17:18:51 +0100
Subject: [PATCH] Fixes textureatlas example

---
 glh/textureatlas/main.go |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/glh/textureatlas/main.go b/glh/textureatlas/main.go
index 68d4df4..a0f4e7a 100644
--- a/glh/textureatlas/main.go
+++ b/glh/textureatlas/main.go
@@ -46,7 +46,7 @@ func main() {
        gl.Clear(gl.COLOR_BUFFER_BIT)

        // Bind the atlas texture and render a quad with it.
-       atlas.Bind()
+       atlas.Bind(gl.TEXTURE_2D)
        gl.Begin(gl.QUADS)
        gl.TexCoord2f(0, 0)
        gl.Vertex2f(0, 0)
@@ -57,7 +57,7 @@ func main() {
        gl.TexCoord2f(0, 1)
        gl.Vertex2f(0, AtlasSize)
        gl.End()
-       atlas.Unbind()
+       atlas.Unbind(gl.TEXTURE_2D)

        glfw.SwapBuffers()
    }
@@ -109,7 +109,7 @@ func fillAtlas(a *glh.TextureAtlas) {
    //atlas.Save("atlas.png")

    // This creates the texture from the atlas pixel data.
-   a.Commit()
+   a.Commit(gl.TEXTURE_2D)
 }

 // initGL initializes GLFW and OpenGL.
-- 
1.7.9.5

Odd choppiness to animation?

I was checking out the demo (41-cube) and noticed some chopping/lag... I wonder if this is a Go-GL issue or just something with the way to program was written.

multiple contexts

this is more of a question than an issue, and apologies if this isn't the correct place to ask the question.
is it possible to create 2 contexts and 2 windows using glfw ? i managed to create 2 distinct windows but the gl commands dont have a concept of the current context and I ended up getting garbage in the 2nd window.
My idea is to write a single go program that launches 2 or more windows and uses channels to communicate between the windows.
any pointers would be greatly appreciated

[Question] Unable to run examples, only background windows showing in GL window

Hi I'm running into an issue trying to get the examples to run and running into a bit of a road block. I've not be able to get either 2.1, 4.1 examples or the code below. I'm using Ubuntu 14.04. Do you have any suggestions on where I could look to debug this issue?

The OpenGL version reported on my system with the code below's openGL version query: OpenGL version 3.3 (Core Profile) Mesa 10.1.3

Result of app running: http://imgur.com/a/jKTcz

package main

import (
    "fmt"
    _ "image/png"
    "log"
    "runtime"

    "github.com/go-gl/gl/v3.3-core/gl"
    "github.com/go-gl/glfw/v3.2/glfw"
)

const windowWidth = 800
const windowHeight = 600

func init() {
    // GLFW event handling must run on the main OS thread
    runtime.LockOSThread()
}

func main() {
    if err := glfw.Init(); err != nil {
        log.Fatalln("failed to initialize glfw:", err)
    }
    defer glfw.Terminate()

    glfw.WindowHint(glfw.Resizable, glfw.False)
    glfw.WindowHint(glfw.ContextVersionMajor, 3)
    glfw.WindowHint(glfw.ContextVersionMinor, 3)
    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
    glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
    window, err := glfw.CreateWindow(windowWidth, windowHeight, "Cube", nil, nil)
    if err != nil {
        panic(err)
    }
    window.MakeContextCurrent()

    // Initialize Glow
    if err := gl.Init(); err != nil {
        panic(err)
    }

    program, err := newProgram(vertexShader, fragmentShader)
    if err != nil {
        panic(err)
    }

    scene := Scene{
        Entities: []Entity{
            {Mesh: triMesh, Color: triColor, DrawMode: gl.TRIANGLES},
            {Mesh: cubeMesh, Color: cubeColor, DrawMode: gl.TRIANGLE_STRIP},
        },
        Program: program,
    }

    scene.Setup()

    gl.UseProgram(program)

    gl.ClearColor(0, 0, 0, 1)
    for !window.ShouldClose() {
        scene.Render()

        // Maintenance
        window.SwapBuffers()
        glfw.PollEvents()
    }

    scene.Cleanup()
}

type Scene struct {
    Program  uint32
    Entities []Entity
}

func (s *Scene) Setup() {
    posAttr := uint32(gl.GetAttribLocation(s.Program, gl.Str("inPosition\x00")))
    colorAttr := uint32(gl.GetAttribLocation(s.Program, gl.Str("inColor\x00")))

    gl.BindFragDataLocation(s.Program, 0, gl.Str("outColor\x00"))

    for i := 0; i < len(s.Entities); i++ {
        s.Entities[i].Setup()
        s.Entities[i].posAttr = posAttr
        s.Entities[i].colorAttr = colorAttr
    }

    fmt.Println("scene setup", gl.GetError())
}

func (s *Scene) Render() {
    gl.ClearColor(0, 0, 0, 1)
    gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

    for i := 0; i < len(s.Entities); i++ {
        s.Entities[i].Render()
    }

    fmt.Println("scene render", gl.GetError())
}

func (s *Scene) Cleanup() {
    for i := 0; i < len(s.Entities); i++ {
        gl.DeleteBuffers(2, &s.Entities[i].vbo[0])
    }
    gl.DeleteProgram(s.Program)
}

type Entity struct {
    Mesh     []float32
    Color    []float32
    DrawMode uint32

    posAttr, colorAttr uint32

    numPoints int32
    vao       uint32
    vbo       [2]uint32
}

func (e *Entity) Setup() {
    e.numPoints = int32(len(e.Mesh) / 3)

    gl.GenVertexArrays(1, &e.vao)
    gl.GenBuffers(int32(len(e.vbo)), &e.vbo[0])

    gl.BindVertexArray(e.vao)

    // Mesh
    gl.BindBuffer(gl.ARRAY_BUFFER, e.vbo[0])
    gl.BufferData(gl.ARRAY_BUFFER, len(e.Mesh)*4, gl.Ptr(e.Mesh), gl.STATIC_DRAW)
    gl.EnableVertexAttribArray(e.posAttr)
    gl.VertexAttribPointer(e.posAttr, 3, gl.FLOAT, false, 0, gl.PtrOffset(0))

    // Color
    gl.BindBuffer(gl.ARRAY_BUFFER, e.vbo[1])
    gl.BufferData(gl.ARRAY_BUFFER, len(e.Color)*4, gl.Ptr(e.Color), gl.STATIC_DRAW)
    gl.EnableVertexAttribArray(e.colorAttr)
    gl.VertexAttribPointer(e.colorAttr, 3, gl.FLOAT, false, 0, gl.PtrOffset(0))

    fmt.Println("entity setup", gl.GetError())
}

func (e *Entity) Render() {
    gl.BindVertexArray(e.vao)
    gl.DrawArrays(e.DrawMode, 0, e.numPoints)

    fmt.Println("entity render", gl.GetError())
}

func (e *Entity) Cleanup() {
    gl.DeleteVertexArrays(1, &e.vao)
    gl.DeleteBuffers(int32(len(e.vbo)), &e.vbo[0])
}

var (
    triMesh = []float32{
        -0.4, 0.1, 0,
        0.4, 0.1, 0,
        0, 0.7, 0,
    }
    triColor = []float32{
        1, 0, 0,
        0, 1, 0,
        0, 0, 1,
    }
    cubeMesh = []float32{
        -0.2, -0.1, 0,
        -0.2, -0.6, 0,
        0.2, -0.1, 0,
        0.2, -0.6, 0,
    }
    cubeColor = []float32{
        1, 0, 0,
        0, 1, 0,
        0, 0, 1,
        1, 1, 0,
    }
)

const vertexShader = `#version 330

attribute vec3 inPosition;
attribute vec3 inColor;

smooth out vec3 theColor;

void main() {
    gl_Position = vec4(inPosition, 1.0);
    theColor = inColor;
}`

const fragmentShader = `#version 330

smooth in vec3 theColor;
out vec4 outColor;

void main() {
    outColor = vec4(theColor, 1.0);
}`

func newProgram(vertexShaderSource, fragmentShaderSource string) (uint32, error) {
    vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
    if err != nil {
        return 0, err
    }

    fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
    if err != nil {
        return 0, err
    }

    program := gl.CreateProgram()

    gl.AttachShader(program, vertexShader)
    gl.AttachShader(program, fragmentShader)
    gl.LinkProgram(program)

    var status int32
    gl.GetProgramiv(program, gl.LINK_STATUS, &status)
    if status == gl.FALSE {
        var logLength int32
        gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

        log := strings.Repeat("\x00", int(logLength+1))
        gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

        return 0, fmt.Errorf("failed to link program: %v", log)
    }

    gl.DeleteShader(vertexShader)
    gl.DeleteShader(fragmentShader)

    return program, nil
}

func compileShader(source string, shaderType uint32) (uint32, error) {
    shader := gl.CreateShader(shaderType)

    csources, free := gl.Strs(source)
    gl.ShaderSource(shader, 1, csources, nil)
    free()
    gl.CompileShader(shader)

    var status int32
    gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
    if status == gl.FALSE {
        var logLength int32
        gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)

        log := strings.Repeat("\x00", int(logLength+1))
        gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))

        return 0, fmt.Errorf("failed to compile %v: %v", source, log)
    }

    return shader, nil
}

Re: github.com/go-gl/example/gl41core-cube/cube.go

Hi Go-gl team,

I found this line in the code

gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00"))

I don't know what is does, and I don't think I ever came across that function before. The documentation says:

glBindFragDataLocation — bind a user-defined varying out variable to a fragment shader color number.

Anyway, I though I'd comment it out, and see if it still runs. And it did!

Can anyone explain if that line should be there, and if so, what does it do?

Cheers,
Andre

Cannot go get - barrage of missing deps

Trying to use go get to grab these examples, I am encountering an endless barrage of "NO".

I'm on Mac OSX mountain lion fwiw

➜  ~  go version
go version go1.1.2 darwin/amd64

Your dependencies list gas, so I did a go get on that and had no problem with it, but then when I did

go get github.com/go-gl/examples

I got in response:

# pkg-config --cflags sdl
Package sdl was not found in the pkg-config search path.
Perhaps you should add the directory containing `sdl.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sdl' found
exit status 1
# github.com/go-gl/glfw
In file included from callback.go:7:
callback.h:12:21: error: GL/glfw.h: No such file or directory

So I installed SDL (with homebrew) and then tried again:

➜  ~GOPATH  go get github.com/go-gl/examples
# github.com/go-gl/glfw
In file included from callback.go:7:
callback.h:12:21: error: GL/glfw.h: No such file or directory
# github.com/banthar/Go-SDL/sdl
sdl.go:15:24: error: SDL_image.h: No such file or directory

➜  ~GOPATH  sudo find / -name sdl.pc 
Password:
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/usr/local/Cellar/sdl/1.2.15/lib/pkgconfig/sdl.pc
/usr/local/lib/pkgconfig/sdl.pc

So I have one version installed by homebrew and one that probably already came when I did make on GLEW.

I don't want to make anyone read the humongous barrage of errors I got, but, eventually I realized I also needed to seperately install SDL_Image and started thinking that maybe Go isn't getting the dependencies of these examples (Go-SDL I was seeing) so I started trying something like this:

➜  ~GOPATH  go get github.com/banthar/Go-SDL/sdl
# github.com/banthar/Go-SDL/sdl
ld: warning: unexpected dylib (/System/Library/Frameworks//Cocoa.framework/Cocoa) on link line
➜  ~GOPATH  go get github.com/go-gl/examples    
# github.com/go-gl/glfw
In file included from callback.go:7:
callback.h:12:21: error: GL/glfw.h: No such file or directory

Additionally throughout, I had gotten errors about "glu" (not "GLEW"):

➜  ~GOPATH  pkg-config glew --libs --cflags
Package glu was not found in the pkg-config search path.
Perhaps you should add the directory containing `glu.pc'
to the PKG_CONFIG_PATH environment variable
Package 'glu', required by 'glew', not found
➜  ~GOPATH  sudo find / -name glu.pc 
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory

Anyone have any idea what I'm doing wrong here? I attempted to add a PKG_CONFIG_PATH environment var to my .zshrc

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

For local and usr lib (usr/lib is glew, usr/local/lib is brew version of SDL and SDL Image).

Thanks a ton, would love to get these operational.

I have a creeping suspicion that it has something to do with the fact that I need to manually go into each example and install dependencies for that project, but I am honestly not sure (a little bit of a Go noob here but with a C++ and Nodejs background, didn't think I would run into trouble like this getting OpenGL working)

Tests fail on MacOS

Firstly, thanks for the work in writing OpenGL bindings for go.

I'm developing on a mac (10.8.6 - Lion), and have been using the go-gl package with success.

However, I have noticed sporadic crashes and other unexplained behaviour even simple test programs supplied in this examples repository.

At the moment running go test from examples/gl produces the segfault below

I'm happy to investigate the cause/fix myself, I just wanted to check that these test suites are still relevant and if there are any common 'gotchas' that I should be aware of in regards to my GL/GLUT/GLFW/GLEW installation.

SIGSEGV: segmentation violation
PC=0x0
signal arrived during cgo execution

github.com/go-gl/gl._Cfunc_glTexImage3D(0x806f, 0x200001908, 0x200000002, 0x190800000000, 0xc200001404, ...)
    github.com/go-gl/gl/_obj/_cgo_defun.c:3484 +0x2f
github.com/go-gl/gl.TexImage3D(0x806f, 0x0, 0x1908, 0x2, 0x2, ...)
    github.com/go-gl/gl/_obj/attriblocation.cgo1.go:4416 +0x9f
github.com/go-gl/examples/gl.func·005()
    /Users/glen/Development/golib/src/github.com/go-gl/examples/gl/gl_test.go:87 +0x94
github.com/go-gl/testutils.func·001()
    /Users/glen/Development/golib/src/github.com/go-gl/testutils/gltest.go:53 +0x254
created by github.com/go-gl/testutils.StartOpenGL
    /Users/glen/Development/golib/src/github.com/go-gl/testutils/gltest.go:58 +0x1f

goroutine 1 [chan receive]:
testing.RunTests(0x168388, 0x22c8e0, 0x14, 0x14, 0x1, ...)
    /usr/local/go/src/pkg/testing/testing.go:434 +0x88e
testing.Main(0x168388, 0x22c8e0, 0x14, 0x14, 0x22f360, ...)
    /usr/local/go/src/pkg/testing/testing.go:365 +0x8a
main.main()
    github.com/go-gl/examples/gl/_test/_testmain.go:81 +0x9a

goroutine 2 [syscall]:

goroutine 7 [chan send]:
github.com/go-gl/testutils.OnTheMainThread(0xc200069360, 0x1682b8)
    /Users/glen/Development/golib/src/github.com/go-gl/testutils/gltest.go:27 +0x7a
github.com/go-gl/examples/gl.TestTexImage3D(0xc20009f120)
    /Users/glen/Development/golib/src/github.com/go-gl/examples/gl/gl_test.go:105 +0x6c
testing.tRunner(0xc20009f120, 0x22c910)
    /usr/local/go/src/pkg/testing/testing.go:353 +0x8a
created by testing.RunTests
    /usr/local/go/src/pkg/testing/testing.go:433 +0x86b
rax     0x806f
rbx     0x1404
rcx     0x2
rdx     0x1908
rdi     0x806f
rsi     0x0
rbp     0xb0103e40
rsp     0xb0103e08
r8      0x2
r9      0x2
r10     0x2cc098
r11     0x1908
r12     0x0
r13     0x502b98270d8
r14     0x1326f7d7164fe600
r15     0xbb14a
rip     0x0
rflags  0x10202
cs      0x2b
fs      0x0
gs      0x0
exit status 2
FAIL    github.com/go-gl/examples/gl    0.894s

build error on OSX, go1.6

what's more, the example glfw31-gl41core-cube

~/gopath/src/github.com/go-gl/gl $ go install github.com/go-gl/examples/glfw31-gl41core-cube
# github.com/go-gl/examples/glfw31-gl41core-cube
../examples/glfw31-gl41core-cube/cube.go:180: undefined: gl.Strs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.