runtime error: invalid memory address or nil pointer dereference

Posted by Klink on Game Development See other posts from Game Development or by Klink
Published on 2012-10-18T01:58:18Z Indexed on 2012/10/18 5:24 UTC
Read the original article Hit count: 358

Filed under:
|
|

I want to learn OpenGL 3.0 with golang. But when i try to compile some code, i get many errors.

package main

import (
    "os"
    //"errors"
    "fmt"
    //gl "github.com/chsc/gogl/gl33"
    //"github.com/jteeuwen/glfw"
    "github.com/go-gl/gl"
    "github.com/go-gl/glfw"
    "runtime"
    "time"
)

var (
    width  int = 640
    height int = 480
)

var (
    points = []float32{0.0, 0.8, -0.8, -0.8, 0.8, -0.8}

)
func initScene() {

    gl.Init()
    gl.ClearColor(0.0, 0.5, 1.0, 1.0)
    gl.Enable(gl.CULL_FACE)
    gl.Viewport(0, 0, 800, 600)
}

func glfwInitWindowContext() {
    if err := glfw.Init(); err != nil {
        fmt.Fprintf(os.Stderr, "glfw_Init: %s\n", err)
        glfw.Terminate()
    }

    glfw.OpenWindowHint(glfw.FsaaSamples, 1)
    glfw.OpenWindowHint(glfw.WindowNoResize, 1)

    if err := glfw.OpenWindow(width, height, 0, 0, 0, 0, 32, 0, glfw.Windowed); err != nil {
        fmt.Fprintf(os.Stderr, "glfw_Window: %s\n", err)
        glfw.CloseWindow()
    }

    glfw.SetSwapInterval(1)
    glfw.SetWindowTitle("Title")    
}

func drawScene() {
    for glfw.WindowParam(glfw.Opened) == 1 {
        gl.Clear(gl.COLOR_BUFFER_BIT)
        vertexShaderSrc := `#version 120
    attribute vec2 coord2d;
    void main(void) {
      gl_Position = vec4(coord2d, 0.0, 1.0);
    }`

    vertexShader := gl.CreateShader(gl.VERTEX_SHADER)
    vertexShader.Source(vertexShaderSrc)
    vertexShader.Compile()

    fragmentShaderSrc := `#version 120
    void main(void) {
      gl_FragColor[0] = 0.0;
      gl_FragColor[1] = 0.0;
      gl_FragColor[2] = 1.0;
    }`

    fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
    fragmentShader.Source(fragmentShaderSrc)
    fragmentShader.Compile()

    program := gl.CreateProgram()
    program.AttachShader(vertexShader)
    program.AttachShader(fragmentShader)
    program.Link()
    attribute_coord2d := program.GetAttribLocation("coord2d")
        program.Use()
        //attribute_coord2d.AttribPointer(size, typ, normalized, stride, pointer)
        attribute_coord2d.EnableArray()
        attribute_coord2d.AttribPointer(0, 3, false,  0, &(points[0]))

        //gl.DrawArrays(gl.TRIANGLES, 0, len(points))
        gl.DrawArrays(gl.TRIANGLES, 0, 3)

        glfw.SwapBuffers()
        inputHandler()
        time.Sleep(100 * time.Millisecond)
    }

}

func inputHandler() {
    glfw.Enable(glfw.StickyKeys)

    if glfw.Key(glfw.KeyEsc) == glfw.KeyPress {
        //gl.DeleteBuffers(2, &uiVBO[0])
        glfw.Terminate()
    }
    if glfw.Key(glfw.KeyF2) == glfw.KeyPress {
        glfw.SetWindowTitle("Title2")
        fmt.Println("Changed to 'Title2'")
        fmt.Println(len(points))
    }
    if glfw.Key(glfw.KeyF1) == glfw.KeyPress {
        glfw.SetWindowTitle("Title1")
        fmt.Println("Changed to 'Title1'")
    }
}

func main() {
    runtime.LockOSThread()
    glfwInitWindowContext()
    initScene()
    drawScene()


}

And after that:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x41bc6f74]

goroutine 1 [syscall]:
github.com/go-gl/gl._Cfunc_glDrawArrays(0x4, 0x7f8500000003)
    /tmp/go-build463568685/github.com/go-gl/gl/_obj/_cgo_defun.c:610 +0x2f
github.com/go-gl/gl.DrawArrays(0x4, 0x3, 0x0, 0x45bd70)
    /tmp/go-build463568685/github.com/go-gl/gl/_obj/gl.cgo1.go:1922 +0x33
main.drawScene()
    /home/klink/Dev/Go/gogl/gopher/exper.go:85 +0x1e6
main.main()
    /home/klink/Dev/Go/gogl/gopher/exper.go:116 +0x27

goroutine 2 [syscall]:
created by runtime.main
    /build/buildd/golang-1/src/pkg/runtime/proc.c:221
exit status 2

© Game Development or respective owner

Related posts about opengl

Related posts about programming