title: Go Lang OpenGL simple shape - blank screen
body: <p>I need some assistance into why this piece of code produces a blank green window.  I made this by combining examples from <a href="https://github.com/Jragonmiris/mathgl/blob/master/examples/opengl-tutorial/tutorial02/main.go" rel="nofollow">https://github.com/Jragonmiris/mathgl/blob/master/examples/opengl-tutorial/tutorial02/main.go</a> and <a href="https://github.com/veandco/go-sdl2/blob/master/examples/opengl3.go" rel="nofollow">https://github.com/veandco/go-sdl2/blob/master/examples/opengl3.go</a>.  I guess i'm not sure if this is a bug with the GoLang sdl/gl framework or an issue with my OpenGL understanding.  All this should draw is a cube.</p>
  
  <p>My code is:</p>
  
  <p>package main</p>
  
  <pre><code>import (
      "fmt"
      // gl "github.com/chsc/gogl/gl33"
      "github.com/veandco/go-sdl2/sdl"
      // "math"
      "github.com/Jragonmiris/mathgl"
      "github.com/go-gl/gl"
      "runtime"
      "time"
  )
  
  // var program gl.Program = 0
  // var buffer gl.Buffer = 0
  
  func MakeProgram(vert, frag string) gl.Program {
  
      vertShader, fragShader := gl.CreateShader(gl.VERTEX_SHADER), gl.CreateShader(gl.FRAGMENT_SHADER)
      vertShader.Source(vert)
      fragShader.Source(frag)
  
      vertShader.Compile()
      fragShader.Compile()
  
      prog := gl.CreateProgram()
  
      prog.AttachShader(vertShader)
      prog.AttachShader(fragShader)
      prog.Link()
      prog.Validate()
      fmt.Println(prog.GetInfoLog())
  
      return prog
  }
  
  func main() {
      var window *sdl.Window
      var context sdl.GLContext
      var event sdl.Event
      var running bool
      var err error
  
      runtime.LockOSThread()
  
      if 0 != sdl.Init(sdl.INIT_EVERYTHING) {
          panic(sdl.GetError())
      }
      window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED,
          sdl.WINDOWPOS_UNDEFINED,
          winWidth, winHeight, sdl.WINDOW_OPENGL)
      if err != nil {
          panic(err)
      }
      if window == nil {
          panic(sdl.GetError())
      }
      context = sdl.GL_CreateContext(window)
      if context == nil {
          panic(sdl.GetError())
      }
  
      if gl.Init() != 0 {
          panic("gl error")
      }
  
      gl.ClearColor(1.0, 1.0, 1.0, .5)
      gl.Viewport(0, 0, winWidth, winHeight)
  
      program := MakeProgram(vertexShaderSource, fragmentShaderSource)
      defer program.Delete()
  
      matrixID := program.GetUniformLocation("MVP")
      Projection := mathgl.Perspective(45.0, 4.0/3.0, 0.1, 100.0)
      View := mathgl.LookAt(4.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
      Model := mathgl.Ident4f()
      MVP := Projection.Mul4(View).Mul4(Model) 
  
      gl.Enable(gl.DEPTH_TEST)
      gl.DepthFunc(gl.LESS)
      gl.Enable(gl.BLEND)
      gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
  
      vertexArray := gl.GenVertexArray()
      defer vertexArray.Delete()
      vertexArray.Bind()
  
      buffer := gl.GenBuffer()
      defer buffer.Delete()
      buffer.Bind(gl.ARRAY_BUFFER)
      gl.BufferData(gl.ARRAY_BUFFER, len(triangle_vertices)*4, &amp;triangle_vertices, gl.STATIC_DRAW)
  
      running = true
      for running {
          for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
              switch t := event.(type) {
              case *sdl.QuitEvent:
                  running = false
              case *sdl.MouseMotionEvent:
  
                  fmt.Printf(string(t.Timestamp))
              }
          }
  
          gl.Clear(gl.COLOR_BUFFER_BIT) // | gl.DEPTH_BUFFER_BIT)
          program.Use()
          matrixID.UniformMatrix4fv(false, MVP)
          attribLoc := gl.AttribLocation(0)
          attribLoc.EnableArray()
          buffer.Bind(gl.ARRAY_BUFFER)
          attribLoc.AttribPointer(3, gl.FLOAT, false, 0, nil)
  
          gl.DrawArrays(gl.TRIANGLES, 0, 3)
  
          attribLoc.DisableArray()
  
          time.Sleep(50 * time.Millisecond)
  
          sdl.GL_SwapWindow(window)
      }
  
      sdl.GL_DeleteContext(context)
      window.Destroy()
      sdl.Quit()
  }
  
  
  
  const (
      winTitle           = "OpenGL Shader"
      winWidth           = 640
      winHeight          = 480
      vertexShaderSource = `
  #version 330 core
  
  // Input vertex data, different for all executions of this shader.
  layout(location = 0) in vec3 vertexPosition_modelspace;
  // Values that stay constant for the whole mesh.
  uniform mat4 MVP;
  void main(){
  
      gl_Position = MVP * vec4 (vertexPosition_modelspace,1.0);
  
  }
  `
      fragmentShaderSource = `
  #version 330 core
  
  // Ouput data
  out vec3 color;
  
  void main()
  {
  
      // Output color = red 
      color = vec3(1,0,0);
  
  }
  `
  )
  
  var triangle_vertices = []float32{
      -.5, -.5, -.5,
      .5, -.5, -.5,
      0.0, 0.5, -.5,
  }
  </code></pre>
  
  <p>So I'm still having trouble drawing a simple shape on the screen. I made a few changes such as simplifying my shape (a triangle).  I created coordinates so they would be more towards the -z axis so I would be able to see them but that has not worked.  I then set the MVP matrix (moving the camera back some) just to make sure.  My shaders are simple as I am only passing in a vec3 vertex position and mat4 MVP matrix so believe shaders are working correctly? Sorry for all the confusion, i think i maybe missing something here.</p>
  
  <p>Update:
  I also ran the version commands for opengl:</p>
  
  <pre><code>fmt.Println(gl.GetString(gl.VERSION))
  fmt.Println(gl.GetString(gl.VENDOR))
  fmt.Println(gl.GetString(gl.RENDERER))
  </code></pre>
  
  <p>for which the output was:</p>
  
  <p>4.5.0 NVIDIA 347.09
  NVIDIA Corporation
  GeForce GTX 650 Ti/PCIe/SSE2</p>
  
  <p>Not sure if this has any impact?</p>
  
  <p>Update:
  I have looked at some more examples and decided to try and add some sdl attributes but still no luck:</p>
  
  <pre><code>sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
  sdl.GL_SetAttribute(sdl.GL_RED_SIZE, 8)
  sdl.GL_SetAttribute(sdl.GL_GREEN_SIZE, 8)
  sdl.GL_SetAttribute(sdl.GL_BLUE_SIZE, 8)
  sdl.GL_SetAttribute(sdl.GL_ALPHA_SIZE, 8)
  </code></pre>
  
  <p>Update:</p>
  
  <p>I modified this post to just include more recent code to not scare people away from TLDR.</p>