Ejemplo n.º 1
0
func main() {

	width := 1024
	height := 768
	title := "Simple Triangle"

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

	glfw.WindowHint(glfw.Samples, 4)             // 4x antialiasing
	glfw.WindowHint(glfw.ContextVersionMajor, 3) // We want OpenGL 3.3
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)    // To make MacOS happy; should not be needed
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) //We don't want the old OpenGL
	window, err := glfw.CreateWindow(width, height, title, nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}
	window.SetKeyCallback(keypress)
	window.SetMouseButtonCallback(mousepress)
	gl.Viewport(0, 0, 1024, 768)
	for !window.ShouldClose() {
		draw()
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Ejemplo n.º 2
0
Archivo: grid.go Proyecto: vinzBad/grid
func Run(g *Game) error {
	if g.Scene == nil {
		return fmt.Errorf("Scene property of given Game struct is empty")
	}

	// GLFW event handling must run on the main OS thread
	runtime.LockOSThread()
	if err := glfw.Init(); err != nil {
		return fmt.Errorf("glfw.Init failed: %s", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.ContextVersionMajor, OpenGLVerMinor)
	glfw.WindowHint(glfw.ContextVersionMinor, OpenGLVerMinor)

	glfw.WindowHint(glfw.Resizable, glfw.False)
	if g.Resizable {
		glfw.WindowHint(glfw.Resizable, glfw.True)
	}

	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

	window, err := glfw.CreateWindow(int(g.Width), int(g.Height), g.Title, nil, nil)
	if err != nil {
		return fmt.Errorf("glfw.CreateWindow failed: %s", err)
	}
	window.MakeContextCurrent()

	// Initialize Glow
	if err := gl.Init(); err != nil {
		return fmt.Errorf("gl.Init failed:", err)
	}

	version := gl.GoStr(gl.GetString(gl.VERSION))
	log.Println("gl.Init successful, OpenGL version:", version)

	var previousTime, deltaTime, time float32
	previousTime = float32(glfw.GetTime()) - 1.0/60.0

	g.Scene.Setup()

	for !window.ShouldClose() {
		time = float32(glfw.GetTime())
		deltaTime = time - previousTime

		glfw.PollEvents()

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

		g.Scene.Update(deltaTime)
		g.Scene.Draw(deltaTime)
		previousTime = time

		window.SwapBuffers()
	}

	return nil
}
Ejemplo n.º 3
0
// InitGraphics creates an OpenGL window and initializes the required graphics libraries.
// It will either succeed or panic.
func (app *ExampleApp) InitGraphics(title string, w int, h int) {
	err := glfw.Init()
	if err != nil {
		panic("Can't init glfw! " + err.Error())
	}

	// request a OpenGL 3.3 core context
	glfw.WindowHint(glfw.Samples, 0)
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

	// do the actual window creation
	app.MainWindow, err = glfw.CreateWindow(w, h, title, nil, nil)
	if err != nil {
		panic("Failed to create the main window! " + err.Error())
	}
	app.MainWindow.MakeContextCurrent()
	glfw.SwapInterval(0)

	// make sure that all of the GL functions are initialized
	err = gl.Init()
	if err != nil {
		panic("Failed to initialize GL! " + err.Error())
	}

	// set the app window dimensions
	app.Width = w
	app.Height = h

	gl.FrontFace(gl.CCW)
	gl.CullFace(gl.BACK)
	gl.Enable(gl.CULL_FACE)
}
Ejemplo n.º 4
0
func Start() {
	// call init right after creating context
	if err := gl.Init(); err != nil {
		fmt.Println("Cannot initialize OGL: " + err.Error())
	}

	texUnits = int(Probe().MaxTextureImageUnits)
	textures = make([]uint32, texUnits)
}
Ejemplo n.º 5
0
func initWindow(sys system.System, width int, height int) {
	sys.CreateWindow(10, 10, width, height)
	sys.EnableVSync(true)
	err := gl.Init()
	if err != nil {
		panic(err)
	}
	// gl.Ortho(gl.Double(0), gl.Double(width), gl.Double(0), gl.Double(height),
	// 	1000, -1000)
}
Ejemplo n.º 6
0
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.True)
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)

	window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()

	window.MakeContextCurrent()

	glfw.SwapInterval(1)

	window.SetSizeCallback(func(w *glfw.Window, width int, height int) {
		fmt.Printf("resize: %d, %d\n", width, height)
		gl.Viewport(0, 0, int32(width), int32(height))
	})
	window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) {
		fmt.Printf("mouse: %f, %f\n", xpos, ypos)
	})
	window.SetFocusCallback(func(w *glfw.Window, focused bool) {
		fmt.Printf("focus: %t\n", focused)
	})
	window.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
		if key == glfw.KeyEscape {
			window.SetShouldClose(true)
		}
		fmt.Printf("key\n")
	})
	window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
		fmt.Printf("mouse\n")
	})
	window.SetCharCallback(func(w *glfw.Window, char rune) {
		fmt.Printf("char: '%c'\n", char)
	})

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

	for !window.ShouldClose() {
		draw()
		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Ejemplo n.º 7
0
// createWindow initializes the graphics libraries and creates the main window for the game.
// it pulls the settings for the window from the options structure passed in.
func createWindow(options *Options) (*glfw.Window, error) {
	// GLFW must be initialized before it's called
	err := glfw.Init()
	if err != nil {
		return nil, fmt.Errorf("Can't init glfw! %v", err)
	}

	// request a OpenGL 3.3 core context
	glfw.WindowHint(glfw.Samples, 0)
	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)

	// do the actual window creation
	mainWindow, err := glfw.CreateWindow(options.WindowWidth, options.WindowHeight, appWindowName, nil, nil)
	if err != nil {
		return nil, fmt.Errorf("Failed to create the main window! %v", err)
	}

	mainWindow.MakeContextCurrent()

	// set vsync options
	if options.VSyncEnabled {
		glfw.SwapInterval(1)
	} else {
		glfw.SwapInterval(0)
	}

	// make sure that all of the GL functions are initialized
	err = gl.Init()
	if err != nil {
		return nil, fmt.Errorf("Can't init gl! %v", err)
	}

	// write out the gl version for debug purposes
	version := gl.GoStr(gl.GetString(gl.VERSION))
	groggy.Log("INFO", "OpenGL version ", version)

	return mainWindow, nil
}
Ejemplo n.º 8
0
func (c *Context) CreateWindow(w, h int, name string) (err error) {
	c.w = w
	c.h = h
	c.name = name
	c.createWindow()
	c.SetCursor(c.cursor)
	gl.Init()
	if e := gl.GetError(); e != 0 {
		if e != gl.INVALID_ENUM {
			err = fmt.Errorf("OpenGL glInit error: %X\n", e)
			return
		}
	}
	c.OpenGLVersion = glfw.GetVersionString()
	c.ShaderVersion = gl.GoStr(gl.GetString(gl.SHADING_LANGUAGE_VERSION))
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
	gl.Disable(gl.CULL_FACE)
	glfw.SwapInterval(1)
	return
}
Ejemplo n.º 9
0
// Initializing the game and set of assets.
func initialize() (*glfw.Window, *Assets, error) {
	if err := glfw.Init(); err != nil {
		return nil, nil, err
	}

	// Setting up a bunch of windowing stuff with GLFW.
	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)

	// Creating GLFW.
	window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil)
	if err != nil {
		return nil, nil, err
	}

	// Creating OpenGL.
	window.MakeContextCurrent()
	if err := gl.Init(); err != nil {
		return nil, nil, err
	}
	gl.ClearColor(0.3, 0.3, 0.3, 1.0)

	if config.DebugMode {
		version := gl.GoStr(gl.GetString(gl.VERSION))
		fmt.Println("Running on OpenGL: " + version)
	}

	// Loading assets.
	assets, err := NewAssets([]string{"res/shaders/texrenderer"}, []string{"res/textures/texture.jpg"})
	if err != nil {
		return nil, nil, err
	}

	return window, assets, nil
}
Ejemplo n.º 10
0
func (c *Context) CreateWindow(w, h int, name string) (err error) {
	c.w = w
	c.h = h
	c.name = name
	var monitor *glfw.Monitor
	if c.fullscreen == true {
		monitor = glfw.GetPrimaryMonitor()
	}
	if c.window, err = glfw.CreateWindow(c.w, c.h, c.name, monitor, nil); err != nil {
		return
	}
	if c.cursor == false {
		c.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
	}
	c.window.MakeContextCurrent()
	gl.Init()
	if e := gl.GetError(); e != 0 {
		if e == gl.INVALID_ENUM {
			fmt.Printf("GL_INVALID_ENUM when calling glInit\n")
		} else {
			err = fmt.Errorf("OpenGL glInit error: %X\n", e)
			return
		}
	}
	c.OpenGLVersion = glfw.GetVersionString()
	c.ShaderVersion = gl.GoStr(gl.GetString(gl.SHADING_LANGUAGE_VERSION))
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
	gl.Disable(gl.CULL_FACE)
	glfw.SwapInterval(1)
	if c.VAO, err = CreateVAO(); err != nil {
		return
	}
	gl.BindVertexArray(c.VAO)
	c.Events = NewEventHandler(c.window)
	return
}
Ejemplo n.º 11
0
func main() {
	//initializing glfw and OpenGL
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()
	window, err := glfw.CreateWindow(graphics.WIDTH, graphics.HEIGHT, "Testing", nil, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()
	if err := gl.Init(); err != nil {
		panic(err)
	}
	gl.Viewport(0, 0, graphics.WIDTH, graphics.HEIGHT)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
	//game.Loop(window)

	game.EngineLoop(window)

}
Ejemplo n.º 12
0
func Init() error {
	if err := gl.Init(); err != nil {
		return err
	}

	log.Printf("OpenGL version: %s", gl.GoStr(gl.GetString(gl.VERSION)))

	vs, err := asset.String("shader.vert")
	if err != nil {
		return err
	}

	fs, err := asset.String("shader.frag")
	if err != nil {
		return err
	}

	program, err := createProgram(vs, fs)
	if err != nil {
		return err
	}
	gl.UseProgram(program)

	var shaderErr error
	uniform := func(name string) int32 {
		var loc int32
		loc, shaderErr = getUniformLocation(program, name)
		return loc
	}

	projectionViewMatrixUniform = uniform("u_projectionViewMatrix")
	modelMatrixUniform = uniform("u_modelMatrix")
	normalMatrixUniform = uniform("u_normalMatrix")
	ambientLightColorUniform = uniform("u_ambientLightColor")
	directionalLightColorUniform = uniform("u_directionalLightColor")
	directionalVectorUniform = uniform("u_directionalVector")
	textureUniform = uniform("u_texture")
	grayscaleUniform = uniform("u_grayscale")
	brightnessUniform = uniform("u_brightness")
	alphaUniform = uniform("u_alpha")
	mixColorUniform = uniform("u_mixColor")
	mixAmountUniform = uniform("u_mixAmount")

	if shaderErr != nil {
		return shaderErr
	}

	vm := newViewMatrix(cameraPosition, targetPosition, up)
	nm := vm.inverse().transpose()
	gl.UniformMatrix4fv(normalMatrixUniform, 1, false, &nm[0])

	gl.Uniform3fv(ambientLightColorUniform, 1, &ambientLightColor[0])
	gl.Uniform3fv(directionalLightColorUniform, 1, &directionalLightColor[0])
	gl.Uniform3fv(directionalVectorUniform, 1, &directionalVector[0])

	SizeCallback = func(width, height int) {
		if winWidth == width && winHeight == height {
			return
		}

		log.Printf("window size changed (%dx%d -> %dx%d)", int(winWidth), int(winHeight), width, height)
		gl.Viewport(0, 0, int32(width), int32(height))

		// Calculate new perspective projection view matrix.
		winWidth, winHeight = width, height
		fw, fh := float32(width), float32(height)
		aspect := fw / fh
		fovRadians := float32(math.Pi) / 3
		perspectiveProjectionViewMatrix = vm.mult(newPerspectiveMatrix(fovRadians, aspect, 1, 2000))

		// Calculate new ortho projection view matrix.
		orthoProjectionViewMatrix = newOrthoMatrix(fw, fh, fw /* use width as depth */)
	}

	if err := initMeshes(); err != nil {
		return err
	}

	if err := initTextures(); err != nil {
		return err
	}

	gl.Enable(gl.CULL_FACE)
	gl.CullFace(gl.BACK)

	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.ClearColor(0, 0, 0, 0)

	return nil
}
Ejemplo n.º 13
0
func CreateContext(width, height int) Context {
	vertexShader := `
#version 330
uniform mat4 projection;
uniform mat4 camera;
uniform mat4 model;
in vec3 vert;
void main() {
    gl_Position = projection * camera * model * vec4(vert, 1);
}
` + "\x00"

	fragmentShader := `
#version 330
uniform vec4 color;
out vec4 outputColor;
void main() {
    outputColor = color;
}
` + "\x00"

	vertices := []float32{
		0.0, 0.0, 0.0,
		1.0, 0.0, 0.0,
		1.0, 1.0, 0.0,
		1.0, 1.0, 0.0,
		0.0, 1.0, 0.0,
		0.0, 0.0, 0.0,
	}

	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(width, height, "OpenGL", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

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

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

	gl.UseProgram(program)

	projection := mgl32.Ortho2D(0, 800, 0, 600)
	projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00"))
	gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])

	camera := mgl32.LookAtV(mgl32.Vec3{0, 0, 0.5}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
	cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00"))
	gl.UniformMatrix4fv(cameraUniform, 1, false, &camera[0])

	model := mgl32.Ident4()
	modelUniform := gl.GetUniformLocation(program, gl.Str("model\x00"))
	gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])

	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0))

	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)

	return Context{Window: window, program: program}
}
Ejemplo n.º 14
0
func main() {
	vertices, normals := obj.Parse(os.Args[1])

	// initialize GLFW
	if err := glfw.Init(); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	// set opengl core profile 3.3
	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(640, 480, "GOpenGL", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	// initialise OpenGL library
	if err := gl.Init(); err != nil {
		panic(err)
	}

	// link program from shaders
	program, err := newProgram("vertex.glsl", "fragment.glsl")
	if err != nil {
		panic(err)
	}
	gl.UseProgram(program)

	// vertex attribute object holds links between attributes and vbo
	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)

	// vertex buffer with per-vertex data
	var vbo [2]uint32
	gl.GenBuffers(2, &vbo[0])

	// position data
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo[0])
	gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	// set up position attribute with layout of vertices
	posAttrib := uint32(gl.GetAttribLocation(program, gl.Str("position\x00")))
	gl.VertexAttribPointer(posAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0))
	gl.EnableVertexAttribArray(posAttrib)

	// normal data
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo[1])
	gl.BufferData(gl.ARRAY_BUFFER, len(normals)*4, gl.Ptr(normals), gl.STATIC_DRAW)

	normAttrib := uint32(gl.GetAttribLocation(program, gl.Str("normal\x00")))
	gl.VertexAttribPointer(normAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0))
	gl.EnableVertexAttribArray(normAttrib)

	uniModel := gl.GetUniformLocation(program, gl.Str("model\x00"))
	uniView := gl.GetUniformLocation(program, gl.Str("view\x00"))
	uniProj := gl.GetUniformLocation(program, gl.Str("proj\x00"))

	matView := mgl32.LookAt(2.0, 2.0, 2.0,
		0.0, 0.0, 0.0,
		0.0, 0.0, 1.0)
	gl.UniformMatrix4fv(uniView, 1, false, &matView[0])

	matProj := mgl32.Perspective(mgl32.DegToRad(45.0), 640.0/480.0, 1.0, 10.0)
	gl.UniformMatrix4fv(uniProj, 1, false, &matProj[0])

	uniLightDir := gl.GetUniformLocation(program, gl.Str("lightDir\x00"))
	uniLightCol := gl.GetUniformLocation(program, gl.Str("lightCol\x00"))

	gl.Uniform3f(uniLightDir, -0.5, 0.0, -1.0)
	gl.Uniform3f(uniLightCol, 0.0, 0.5, 0.5)

	startTime := glfw.GetTime()
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)

	for !window.ShouldClose() {
		// clear buffer
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		matRot := mgl32.HomogRotate3DZ(float32(glfw.GetTime() - startTime))
		gl.UniformMatrix4fv(uniModel, 1, false, &matRot[0])

		gl.DrawArrays(gl.TRIANGLES, 0, int32(len(vertices)))

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Ejemplo n.º 15
0
func main() {
	// init glfw
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

	// make an application window
	window, err := glfw.CreateWindow(windowWidth, windowHeight, "Hello", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	// init gl
	if err := gl.Init(); err != nil {
		panic(err)
	}
	fmt.Println("OpenGL version", gl.GoStr(gl.GetString(gl.VERSION)))

	// create vertex & fragment shader
	program, err := newProgram(vertexShader, fragmentShader)
	if err != nil {
		panic(err)
	}
	gl.UseProgram(program)

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

	points := []float32{
		-0.5, -0.5,
		0.5, 0.5,
		0.5, -0.5,
		-0.5, 0.5,
	}

	vertices := []uint32{
		0, 2, 1, 3,
	}

	// configure the vertex data
	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)

	defer gl.BindVertexArray(0)

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(points)*4, gl.Ptr(points), gl.STATIC_DRAW)

	var ibo uint32
	gl.GenBuffers(1, &ibo)
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("pv\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 2, gl.FLOAT, false, 2*4, gl.PtrOffset(0))

	// global settings
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)

	for !window.ShouldClose() {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		gl.UseProgram(program)

		gl.BindVertexArray(vao)

		gl.DrawElements(gl.LINE_LOOP, 4, gl.UNSIGNED_INT, gl.PtrOffset(0))

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Ejemplo n.º 16
0
func main() {
	// init glfw
	if err := glfw.Init(); err != nil {
		panic(err)
	}

	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

	// make an application window
	window, err := glfw.CreateWindow(windowWidth, windowHeight, "Transform", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	// init gl
	if err := gl.Init(); err != nil {
		panic(err)
	}
	fmt.Println("OpenGL version", gl.GoStr(gl.GetString(gl.VERSION)))

	// create vertex & fragment shader
	program, err := newProgram(vertexShader, fragmentShader)
	if err != nil {
		panic(err)
	}
	gl.UseProgram(program)

	projection := mgl32.Perspective(mgl32.DegToRad(45.0), float32(windowWidth)/windowHeight, 0.1, 10.0)
	projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00"))
	gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])

	camera := mgl32.LookAtV(
		mgl32.Vec3{3, 3, 3},
		mgl32.Vec3{0, 0, 0},
		mgl32.Vec3{0, 1, 0},
	)
	cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00"))
	gl.UniformMatrix4fv(cameraUniform, 1, false, &camera[0])

	model := mgl32.Ident4()
	modelUniform := gl.GetUniformLocation(program, gl.Str("model\x00"))
	gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])

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

	points := []float32{
		-0.9, -0.9, -0.9,
		0.9, -0.9, -0.9,
		0.9, -0.9, 0.9,
		-0.9, -0.9, 0.9,
		-0.9, 0.9, -0.9,
		0.9, 0.9, -0.9,
		0.9, 0.9, 0.9,
		-0.9, 0.9, 0.9,
	}

	vertices := []uint32{
		0, 1,
		1, 2,
		2, 3,
		3, 0,
		0, 4,
		1, 5,
		2, 6,
		3, 7,
		4, 5,
		5, 6,
		6, 7,
		7, 4,
	}

	// configure the vertex data
	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)

	defer gl.BindVertexArray(0)

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(points)*4, gl.Ptr(points), gl.STATIC_DRAW)

	var ibo uint32
	gl.GenBuffers(1, &ibo)
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0))

	// global settings
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)

	angleX := 0.0
	angleY := 0.0
	angleZ := 0.0
	previousTime := glfw.GetTime()

	for !window.ShouldClose() {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		time := glfw.GetTime()
		elapsed := time - previousTime
		previousTime = time

		angleX += math.Sin((elapsed / period) * math.Pi * 2.0)
		angleY += math.Sin((elapsed / period) / 6.0 * math.Pi * 2.0)
		angleZ += math.Sin((elapsed / period) / 3.0 * math.Pi * 2.0)
		model = mgl32.HomogRotate3DY(float32(angleY)).Mul4(mgl32.HomogRotate3DX(float32(angleX))).Mul4(mgl32.HomogRotate3DZ(float32(angleZ)))
		gl.UseProgram(program)
		gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])

		gl.BindVertexArray(vao)

		gl.DrawElements(gl.LINES, int32(len(vertices)), gl.UNSIGNED_INT, gl.PtrOffset(0))

		window.SwapBuffers()
		glfw.PollEvents()
	}
}
Ejemplo n.º 17
0
func InitGL() error {
	defer tlog.FuncLog(tlog.Func("InitGL"))

	err := gl.Init()
	if err != nil {
		return err
	}
	gl.GetError() // cleanup gl error state

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

	gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
	Check()
	gl.ReadBuffer(gl.BACK)
	Check()
	gl.DrawBuffer(gl.BACK)
	Check()

	gl.FrontFace(gl.CW)
	Check()
	gl.CullFace(gl.BACK)
	Check()
	gl.Disable(gl.CULL_FACE)
	Check()

	gl.ClearColor(1, 1, 0.5, 0)
	Check()
	gl.ClearDepth(1.0)
	Check()
	gl.ClearStencil(0)
	Check()

	gl.Disable(gl.STENCIL_TEST)
	Check()
	gl.StencilMask(0xFFFFFFFF)
	Check()
	gl.StencilFunc(gl.EQUAL, 0x00000000, 0x00000001)
	Check()
	gl.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP)
	Check()

	gl.Disable(gl.DITHER)
	Check()

	gl.Enable(gl.DEPTH_TEST)
	Check()
	gl.DepthFunc(gl.LEQUAL)
	Check()
	gl.DepthMask(true)
	Check()
	gl.DepthRange(0., 1.)
	Check()
	gl.Enable(gl.DEPTH_CLAMP)
	Check()

	gl.Enable(gl.BLEND)
	Check()
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	ForceCheck()
	return nil
}