Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jasdel opened this issue Aug 12, 2016 · 4 comments
Labels

Comments

@jasdel
Copy link

jasdel commented Aug 12, 2016

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
}
@dmitshur
Copy link
Member

dmitshur commented Aug 12, 2016

What does go version print?

What does go env print?

Did you do go get -u -v github.com/go-gl/examples/gl21-cube? Do you see any output while doing that?

What's your resolution?

What's your video card?

Is this running inside a virtual machine or a physical machine?

@jasdel
Copy link
Author

jasdel commented Aug 12, 2016

Thanks for getting back to me. I should also note that I am able to run the gomobile examples without issue. They use openGL ES 2.0, i think. I get the same libEGL warning: failed to create a pipe screen for i965 for all these regular openGL code examples and the gomobile examples. But the gomobiles render the scene. The regular openGL code will display a window, and the screen that was behind the window when it was created. I can move then window around and the window doesn't update.

It seems I'm getting an issue where nothing is being drawn, but having a rough time trying to figure out what is causing that. I have the same issue with both 2.1 and 4.1 go-gl/examples.

I'm running this nativity on my laptop, not in a virtual machine.

$  go version
go version go1.7rc2 linux/amd64

I switched back to Go 1.6.3 and have the same issue.

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jasdel/Workspace/golang"
GORACE=""
GOROOT="/home/jasdel/Sources/go"
GOTOOLDIR="/home/jasdel/Sources/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build356810928=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

The output of the go get:

$ go get -u -v github.com/go-gl/examples/gl21-cube
github.com/go-gl/examples (download)
github.com/go-gl/gl (download)
github.com/go-gl/glfw (download)
github.com/go-gl/gl/v2.1/gl
github.com/go-gl/examples/gl21-cube

$ cd $GOPATH/src/github.com/go-gl/examples/gl21-cube
$ go build
$ ./gl21-cube
libEGL warning: failed to create a pipe screen for i965

My resolution is 1920x1080, and video card is a Intel® Iris™ Pro Graphics 5200

$ sudo lshw -C video

  *-display               
       description: VGA compatible controller
       product: Crystal Well Integrated Graphics Controller
       vendor: Intel Corporation
       physical id: 2
       bus info: pci@0000:00:02.0
       version: 08
       width: 64 bits
       clock: 33MHz
       capabilities: msi pm vga_controller bus_master cap_list rom
       configuration: driver=i915 latency=0
       resources: irq:46 memory:f7800000-f7bfffff memory:e0000000-efffffff ioport:f000(size=64)

@jasdel
Copy link
Author

jasdel commented Aug 13, 2016

It turns out my Graphics drivers were pretty out of date. When I updated my system the examples started working again. Thanks for your assistance!

@jasdel jasdel closed this as completed Aug 13, 2016
@dmitshur
Copy link
Member

Glad to hear that, thanks for following up! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants