Esempio n. 1
0
func main() {
	fmt.Println("Init GLFW3")
	if glfw3.Init() {
		fmt.Println("Init ok")
		defer closeGLFW()
	}

	// Create the window
	fmt.Println("Opening window")
	win, err := glfw3.CreateWindow(1024, 768, "Kunos Rulez", nil, nil)
	gl.Init()
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("ok", win)
	}

	win.MakeContextCurrent()

	for win.ShouldClose() == false {
		glfw3.PollEvents()
		gl.ClearColor(1.0, 0.0, 0.0, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)

		win.SwapBuffers()
	}

	fmt.Println("Destroying win")
	win.Destroy()
}
Esempio n. 2
0
func NewGLRenderer(view *View, title string) *GLRenderer {
	r := new(GLRenderer)
	r.View = view
	GL <- func() {
		if sdl.Init(sdl.INIT_VIDEO) < 0 {
			panic(sdl.GetError())
		}
		sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
		sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)
		r.Screen = sdl.SetVideoMode(view.Width, view.Height, 32, sdl.OPENGL)
		if r.Screen == nil {
			panic(sdl.GetError())
		}
		sdl.WM_SetCaption(title, title)
		err := gl33.Init()
		if err != nil {
			panic(err)
		}
		gl33.Enable(gl33.BLEND)
		gl33.Enable(gl33.CULL_FACE)
		gl33.Enable(gl33.DEPTH_TEST)
		gl33.ClearColor(1.0, 1.0, 1.0, 1.0)
		gl33.BlendFunc(gl33.SRC_ALPHA, gl33.ONE_MINUS_SRC_ALPHA)
		gl33.DepthFunc(gl33.LEQUAL)
		gl33.Viewport(0, 0, gl33.Sizei(view.Width), gl33.Sizei(view.Height))
	}
	r.PerspectiveMatrix = s3dm.PerspectiveMatrix(view.Fovy, view.Aspect, view.Near, view.Far)
	r.OrthographicMatrix = s3dm.OrthographicMatrix(float64(view.Width), float64(view.Height), 0, 1)
	return r
}
Esempio n. 3
0
func main() {
	var err error
	err = glfw.Init()
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.Terminate()

	// You could probably change the required versions down
	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 1)

	// Open Window with 8 bit Alpha
	err = glfw.OpenWindow(ScreenWidth, ScreenHeight, 0, 0, 0, 8, 0, 0, glfw.Windowed)
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetWindowTitle(WindowTitle)

	major, minor, rev := glfw.GLVersion()
	if major < 3 {
		fmt.Printf("Error your graphic card does not support OpenGL 3.3\n Your GL-Version is: %d, %d, %d\n", major, minor, rev)
		fmt.Println("You can try to lower the settings in glfw.OpenWindowHint(glfw.OpenGLVersionMajor/Minor.")
	}

	// Init extension loading
	err = gl.Init()
	if err != nil {
		fmt.Printf("Init OpenGL extension loading failed with %s.\n", err)
	}

	// Enable transparency in OpenGL
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	initResources()

	for {
		display()
	}

	// Free resources
	free()
}
Esempio n. 4
0
func main() {
	fmt.Println("OpenGL Programming/Modern OpenGL Introduction")
	fmt.Println("Tutorial taken from http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction")

	var err error
	err = glfw.Init()
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.Terminate()

	glfw.OpenWindowHint(glfw.OpenGLVersionMajor, 3)
	glfw.OpenWindowHint(glfw.OpenGLVersionMinor, 3)
	glfw.OpenWindowHint(glfw.OpenGLProfile, 1)

	err = glfw.OpenWindow(ScreenWidth, ScreenHeight, 0, 0, 0, 0, 0, 0, glfw.Windowed)
	if err != nil {
		fmt.Printf("GLFW: %s\n", err)
		return
	}
	defer glfw.CloseWindow()

	glfw.SetWindowTitle(WindowTitle)

	// Init extension loading
	err = gl.Init()
	if err != nil {
		fmt.Printf("Init OpenGL extension loading failed with %s.\n", err)
	}

	initResources()

	// Render loop
	for glfw.WindowParam(glfw.Opened) == 1 {
		display()
	}

	// Free resources
	free()
}
Esempio n. 5
0
func main() {
	var window *sdl.Window
	var context sdl.GLContext
	var event sdl.Event
	var running bool
	var err error
	runtime.LockOSThread()
	if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
		panic(err)
	}
	defer sdl.Quit()
	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED,
		sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_OPENGL)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()
	context, err = sdl.GL_CreateContext(window)
	if err != nil {
		panic(err)
	}
	defer sdl.GL_DeleteContext(context)

	gl.Init()
	gl.Viewport(0, 0, gl.Sizei(winWidth), gl.Sizei(winHeight))
	// OPENGL FLAGS
	gl.ClearColor(0.0, 0.1, 0.0, 1.0)
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	// VERTEX BUFFER
	var vertexbuffer gl.Uint
	gl.GenBuffers(1, &vertexbuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_vertices)*4), gl.Pointer(&triangle_vertices[0]), gl.STATIC_DRAW)

	// COLOUR BUFFER
	var colourbuffer gl.Uint
	gl.GenBuffers(1, &colourbuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_colours)*4), gl.Pointer(&triangle_colours[0]), gl.STATIC_DRAW)

	// GUESS WHAT
	program := createprogram()

	// VERTEX ARRAY
	var VertexArrayID gl.Uint
	gl.GenVertexArrays(1, &VertexArrayID)
	gl.BindVertexArray(VertexArrayID)
	gl.EnableVertexAttribArray(0)
	gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer)
	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 0, nil)

	// VERTEX ARRAY HOOK COLOURS
	gl.EnableVertexAttribArray(1)
	gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer)
	gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 0, nil)

	//UNIFORM HOOK
	unistring := gl.GLString("scaleMove")
	UniScale = gl.GetUniformLocation(program, unistring)
	fmt.Printf("Uniform Link: %v\n", UniScale+1)

	gl.UseProgram(program)

	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:

				xrot = float32(t.Y) / 2
				yrot = float32(t.X) / 2
				fmt.Printf("[%dms]MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel)
			}
		}
		drawgl()
		sdl.GL_SwapWindow(window)
	}
}
Esempio n. 6
0
func Init() error {
	if err := gl.Init(); err != nil {
		return err
	}
	// Load default vertex- and fragmentshader for surfaces
	surfaceVS, err := LoadShader(surfaceVertexShaderData, VertexShader)
	if err != nil {
		return err
	}
	if err = surfaceVS.Compile(); err != nil {
		return err
	}
	surfaceFS, err := LoadShader(surfaceFragmentShaderData, FragmentShader)
	if err != nil {
		return err
	}
	if err = surfaceFS.Compile(); err != nil {
		return err
	}
	DefaultSurfaceShaderProgram = NewProgram()
	DefaultSurfaceShaderProgram.AttachShaders(surfaceVS, surfaceFS)
	if err = DefaultSurfaceShaderProgram.Link(); err != nil {
		return err
	}
	// Surface AttributeLocations
	surfaceCoordAttrib, err = DefaultSurfaceShaderProgram.GetAttributeLocation("coord2d")
	if err != nil {
		return err
	}
	surfaceTexAttrib, err = DefaultSurfaceShaderProgram.GetAttributeLocation("texcoord")
	if err != nil {
		return err
	}
	// UniformLocations
	surfaceColorUni, err = DefaultSurfaceShaderProgram.GetUniformLocation("v_color")
	if err != nil {
		return err
	}
	surfaceMvpUni, err = DefaultSurfaceShaderProgram.GetUniformLocation("mvp")
	if err != nil {
		return err
	}
	surfaceTexUni, err = DefaultSurfaceShaderProgram.GetUniformLocation("texture")
	if err != nil {
		return err
	}

	// Primitives
	primitivesVertexShader, err := LoadShader(primitivesVertexShaderData, VertexShader)
	if err != nil {
		return err
	}
	if err = primitivesVertexShader.Compile(); err != nil {
		return err
	}
	primitivesFragmentShader, err := LoadShader(primitivesFragmentShaderData, FragmentShader)
	if err != nil {
		return err
	}
	if err = primitivesFragmentShader.Compile(); err != nil {
		return err
	}
	DefaultPrimitivesShaderProgram = NewProgram()
	DefaultPrimitivesShaderProgram.AttachShaders(primitivesVertexShader, primitivesFragmentShader)
	err = DefaultPrimitivesShaderProgram.Link()
	if err != nil {
		return err
	}
	primitivesAttributeCoord, err = DefaultPrimitivesShaderProgram.GetAttributeLocation("coord")
	if err != nil {
		return err
	}
	primitivesAttributeColor, err = DefaultPrimitivesShaderProgram.GetAttributeLocation("v_color")
	if err != nil {
		return err
	}
	primitivesUniformMatrix, err = DefaultPrimitivesShaderProgram.GetUniformLocation("mvp")
	if err != nil {
		return err
	}
	return nil
}