go-gl/gl

Window Access Violation

NewSalman opened this issue · 1 comments

package main

import (
	"fmt"
	"io/ioutil"
	"runtime"

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

func init() {
	// This is needed to arrange that main() runs on main thread.
	// See documentation for functions that are only allowed to be called from the main thread.
	runtime.LockOSThread()
}

func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.True)
	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

	var Vertecies = []float32{
		-.5, -.5, 0.0,
		-.5, .5, 0.0,
		0.0, 0.0, 0.0,
	}

	window, err := glfw.CreateWindow(800, 600, "Minecraft Golang Edition", nil, nil)
	if err != nil {
		glfw.Terminate()
		panic(err)
	}
	//Init Contex
	window.MakeContextCurrent()

	version := gl.GoStr(gl.GetString(gl.VERSION))
	fmt.Println("OpenGL version", version)

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

	//VERTEX SHADER COMPILE
	var VertexShader uint32 = gl.CreateShader(gl.VERTEX_SHADER)

	resultvert, err := ioutil.ReadFile("shader.vert")
	ErrorHandler(err)

	vsource, free := gl.Strs(string(resultvert))
	gl.ShaderSource(VertexShader, 1, vsource, nil)
	gl.CompileShader(VertexShader)
	free()

	//FRAGMENT SHADER COMPILE
	var FragmentShader uint32 = gl.CreateShader(gl.FRAGMENT_SHADER)

	resultFrag, err := ioutil.ReadFile("shader.frag")
	ErrorHandler(err)

	fsource, free := gl.Strs(string(resultFrag))
	gl.ShaderSource(FragmentShader, 1, fsource, nil)
	gl.CompileShader(FragmentShader)
	free()

	//MAKE PROGRAM
	Program := gl.CreateProgram()

	gl.AttachShader(Program, VertexShader)
	gl.AttachShader(Program, FragmentShader)
	gl.LinkProgram(Program)

	gl.DeleteShader(VertexShader)
	gl.DeleteShader(FragmentShader)

	//VAO, VBO, EBO

	var VBO, VAO uint32
	//generate VAO first
	gl.GenVertexArrays(1, &VAO)

	gl.GenBuffers(1, &VBO)
	gl.BindBuffer(gl.ARRAY_BUFFER, VBO)

	gl.BufferData(gl.ARRAY_BUFFER, len(Vertecies), gl.Ptr(Vertecies), gl.STATIC_DRAW)

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

	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
	gl.BindVertexArray(0)

	//Clear Color can maka a access viaolation error
	//gl.ClearColor(1.0, 1.0, 1.0, 1.0)

	for !window.ShouldClose() {
		// Do OpenGL stuff.
		//gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		HandleInput(window)

		gl.UseProgram(Program)
		gl.BindVertexArray(VAO)
		gl.DrawArrays(gl.TRIANGLES, 0, 3)

		window.SwapBuffers()
		glfw.PollEvents()
	}

	gl.DeleteVertexArrays(1, &VAO)
	gl.DeleteBuffers(1, &VBO)
	gl.DeleteProgram(Program)
}

func HandleInput(window *glfw.Window) {
	if window.GetKey(glfw.KeyEscape) == glfw.Press {
		glfw.Terminate()
	}
}

func ErrorHandler(errorMsg error) {
	glfw.Terminate()
	panic(errorMsg.Error())
}

im still a beginner in openGL, when i run it always resulting error message exit status 3221225477, no log about the error
and i find this https://stackoverflow.com/questions/59838685/exit-status-3221225477-in-terminal-in-golang but no answer.

i know this function resulting the error, I have also tried removing this but still the same problem occure

gl.ClearColor(1.0, 1.0, 1.0, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

it just my beginner fault, i forgot to write gl.Init() after making window.MakeContextCurrent