Example #1
0
func (s *Shader) initWithString(vert_shader string, frag_shader string) {

	s.initShader(gl.VERTEX_SHADER, vert_shader, &s.vert_shader)
	s.initShader(gl.FRAGMENT_SHADER, frag_shader, &s.frag_shader)

	s.program = gl.CreateProgram()

	gl.AttachShader(s.program, s.vert_shader)
	gl.AttachShader(s.program, s.frag_shader)
	gl.LinkProgram(s.program)

	var (
		status      gl.Int
		info_length gl.Int
		message     *gl.Char
	)

	gl.GetProgramiv(s.program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		fmt.Println("Error linking program")
		gl.GetProgramiv(s.program, gl.INFO_LOG_LENGTH, &info_length)
		message = gl.GLStringAlloc(gl.Sizei(info_length))
		gl.GetProgramInfoLog(s.program, gl.Sizei(info_length), nil, message)
		fmt.Println(gl.GoString(message))
		gl.GLStringFree(message)
	}

	s.initAttributes()
	s.initUniforms()

}
Example #2
0
func InitViewport() {
	gl.Viewport(0, 0, gl.Sizei(Width), gl.Sizei(Height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	x := gl.Double(float64(Height) / float64(Width))
	gl.Frustum(-1./2., 1./2., -x/2, x/2, 10, 100)
}
Example #3
0
// resize resizes the window to the specified dimensions.
func resize(width, height int) {
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0)
	gl.MatrixMode(gl.MODELVIEW)
}
Example #4
0
func (w *Window) run() {
	runtime.LockOSThread()
	glfw.MakeContextCurrent(w.w)
	defer glfw.MakeContextCurrent(nil)

	// glfw should fire initial resize events to avoid this duplication (https://github.com/glfw/glfw/issues/62)
	width, height := w.w.Size()
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, gl.Double(width), 0, gl.Double(height), -1, 1)
	Resize(w, Pt(float64(width), float64(height)))
	width, height = w.w.FramebufferSize()
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))

	gl.Enable(gl.BLEND)
	gl.Enable(gl.POINT_SMOOTH)
	gl.Enable(gl.LINE_SMOOTH)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	for !w.close {
		select {
		case f := <-w.do:
			f()
		case <-w.paint:
			gl.MatrixMode(gl.MODELVIEW)
			gl.LoadIdentity()

			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
			w.base().paint()
			w.w.SwapBuffers()
		}
	}
}
Example #5
0
func (s *Shader) initShader(t gl.Enum, str string, shader *gl.Uint) {
	*shader = gl.CreateShader(t)
	if *shader == 0 {
		fmt.Println("error creating shader of type ", t)
	}

	src := gl.GLStringArray(str)
	defer gl.GLStringArrayFree(src)

	gl.ShaderSource(*shader, 1, &src[0], nil)
	gl.CompileShader(*shader)

	var (
		status      gl.Int
		info_length gl.Int
		message     *gl.Char
	)

	gl.GetShaderiv(*shader, gl.COMPILE_STATUS, &status)
	if status == gl.FALSE {
		fmt.Println("Error compiling shader")
		gl.GetShaderiv(*shader, gl.INFO_LOG_LENGTH, &info_length)
		message = gl.GLStringAlloc(gl.Sizei(info_length))
		gl.GetShaderInfoLog(*shader, gl.Sizei(info_length), nil, message)
		fmt.Println(gl.GoString(message))
		gl.GLStringFree(message)
	}

}
Example #6
0
func createTexture(r io.Reader) (textureId gl.Uint, err error) {
	img, err := png.Decode(r)
	if err != nil {
		return 0, err
	}

	rgbaImg, ok := img.(*image.NRGBA)
	if !ok {
		return 0, errors.New("texture must be an NRGBA image")
	}

	gl.GenTextures(1, &textureId)
	gl.BindTexture(gl.TEXTURE_2D, textureId)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)

	// flip image: first pixel is lower left corner
	imgWidth, imgHeight := img.Bounds().Dx(), img.Bounds().Dy()
	data := make([]byte, imgWidth*imgHeight*4)
	lineLen := imgWidth * 4
	dest := len(data) - lineLen
	for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride {
		copy(data[dest:dest+lineLen], rgbaImg.Pix[src:src+rgbaImg.Stride])
		dest -= lineLen
	}
	gl.TexImage2D(gl.TEXTURE_2D, 0, 4, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))

	return textureId, nil
}
Example #7
0
func (ms *ManaSource) Draw(local *LocalData, zoom float64, dx float64, dy float64) {
	if local.nodeTextureData == nil {
		//		gl.Enable(gl.TEXTURE_2D)
		local.nodeTextureData = make([]byte, ms.options.NumNodeRows*ms.options.NumNodeCols*3)
		gl.GenTextures(1, &local.nodeTextureId)
		gl.BindTexture(gl.TEXTURE_2D, local.nodeTextureId)
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
		gl.TexImage2D(
			gl.TEXTURE_2D,
			0,
			gl.RGB,
			gl.Sizei(ms.options.NumNodeRows),
			gl.Sizei(ms.options.NumNodeCols),
			0,
			gl.RGB,
			gl.UNSIGNED_BYTE,
			gl.Pointer(&local.nodeTextureData[0]))
	}
	for i := range ms.rawNodes {
		for c := 0; c < 3; c++ {
			color_frac := ms.rawNodes[i].Mana[c] * 1.0 / ms.options.NodeMagnitude
			color_range := float64(ms.options.MaxNodeBrightness - ms.options.MinNodeBrightness)
			local.nodeTextureData[i*3+c] = byte(
				color_frac*color_range + float64(ms.options.MinNodeBrightness))
		}
	}
	gl.Enable(gl.TEXTURE_2D)
	//gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, local.nodeTextureId)
	gl.TexSubImage2D(
		gl.TEXTURE_2D,
		0,
		0,
		0,
		gl.Sizei(ms.options.NumNodeRows),
		gl.Sizei(ms.options.NumNodeCols),
		gl.RGB,
		gl.UNSIGNED_BYTE,
		gl.Pointer(&local.nodeTextureData[0]))

	base.EnableShader("nodes")
	base.SetUniformI("nodes", "width", ms.options.NumNodeRows*3)
	base.SetUniformI("nodes", "height", ms.options.NumNodeCols*3)
	base.SetUniformI("nodes", "drains", 1)
	base.SetUniformI("nodes", "tex0", 0)
	base.SetUniformI("nodes", "tex1", 1)
	base.SetUniformF("nodes", "zoom", float32(zoom))
	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, local.nodeTextureId)

	// I have no idea why this value for move works, but it does.  So, hooray.
	move := (dx - dy) / 2
	texture.RenderAdvanced(move, -move, dy, dx, 3.1415926535/2, true)
	base.EnableShader("")
	gl.Disable(gl.TEXTURE_2D)
}
Example #8
0
func upload(id gl.Uint, data []byte, stride int, w int, h int) {
	gl.BindTexture(gl.TEXTURE_2D, id)
	gl.PixelStorei(gl.UNPACK_ROW_LENGTH, gl.Int(stride))
	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE,
		gl.Sizei(w), gl.Sizei(h), 0,
		gl.LUMINANCE, gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))
}
Example #9
0
func windowResizeCallback(window *glfw.Window, width, height int) {

	paunchWindow.width = width
	paunchWindow.height = height

	for _, eventManager := range paunchWindow.eventManagers {
		eventManager.RunWindowResizeEvent(width, height)
	}

	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
}
Example #10
0
// NewSprite creates a new Sprite object using the given data, which is
// expected to be in RGBA format. If you use PNG image files, you can use the
// NewSpriteFromImage shortcut function instead.
func NewSprite(x, y, width, height float64, data []byte, clip int) (*Sprite, error) {

	verticies := []float64{
		x, y,
		x + width, y,
		x, y + height,

		x + width, y + height,
		x + width, y,
		x, y + height}

	shape, err := NewShape(gl.TRIANGLES, verticies)
	if err != nil {
		return nil, err
	}

	sprite := &Sprite{texcoordBuffer: 0, texture: nil, shape: shape}

	texCoords := []float32{
		0, 0,
		1, 0,
		0, 1,

		1, 1,
		1, 0,
		0, 1}

	gl.GenBuffers(1, &sprite.texcoordBuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(sprite.texcoordBuffer))
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(texCoords)*4), gl.Pointer(&texCoords[0]), gl.STREAM_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	sprite.texture = make([]gl.Uint, clip)
	gl.GenTextures(gl.Sizei(clip), &sprite.texture[0])

	clips := make([][]byte, clip)
	for i := range clips {
		clips[i] = data[i*(len(data)/len(clips)) : (i+1)*(len(data)/len(clips))]
		gl.BindTexture(gl.TEXTURE_2D, sprite.texture[len(clips)-1-i])
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
			gl.Sizei(width), gl.Sizei(height),
			0, gl.RGBA, gl.UNSIGNED_BYTE,
			gl.Pointer(&clips[i][0]))
	}

	gl.BindTexture(gl.TEXTURE_2D, 0)

	return sprite, checkForErrors()
}
Example #11
0
func (g *Game) RenderLocalEditor(region g2.Region) {
	g.editor.Lock()
	defer g.editor.Unlock()
	g.editor.region = region
	g.editor.camera.regionDims = linear.Vec2{float64(region.Dims.Dx), float64(region.Dims.Dy)}
	levelDims := linear.Vec2{float64(g.Level.Room.Dx), float64(g.Level.Room.Dy)}
	g.editor.camera.StandardRegion(levelDims.Scale(0.5), levelDims)
	g.editor.camera.approachTarget()

	gl.MatrixMode(gl.PROJECTION)
	gl.PushMatrix()
	gl.LoadIdentity()
	defer gl.PopMatrix()

	gl.PushAttrib(gl.VIEWPORT_BIT)
	gl.Viewport(gl.Int(region.X), gl.Int(region.Y), gl.Sizei(region.Dx), gl.Sizei(region.Dy))
	defer gl.PopAttrib()

	current := &g.editor.camera.current
	gl.Ortho(
		gl.Double(current.mid.X-current.dims.X/2),
		gl.Double(current.mid.X+current.dims.X/2),
		gl.Double(current.mid.Y+current.dims.Y/2),
		gl.Double(current.mid.Y-current.dims.Y/2),
		gl.Double(1000),
		gl.Double(-1000),
	)
	defer func() {
		gl.MatrixMode(gl.PROJECTION)
		gl.PopMatrix()
		gl.MatrixMode(gl.MODELVIEW)
	}()
	gl.MatrixMode(gl.MODELVIEW)

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

	g.renderWalls()
	g.renderEdges()
	g.renderBases()
	g.renderEntsAndAbilities()
	g.renderProcesses()

	g.editor.renderPathing(&g.Level.Room, g.local.pathingData)

	switch g.editor.action {
	case editorActionNone:
	case editorActionPlaceBlock:
		g.editor.renderPlaceBlock(g)
	default:
		base.Error().Printf("Unexpected editorAction: %v", g.editor.action)
	}
}
Example #12
0
func reuploadTexture(tex *gl.Uint, w, h int, data []byte) {
	if *tex > 0 {
		gl.BindTexture(gl.TEXTURE_2D, *tex)
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(w), gl.Sizei(h), 0,
			gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))
		if gl.GetError() != gl.NO_ERROR {
			gl.DeleteTextures(1, tex)
			panic("Failed to reupload texture")
		}
		return
	}
	*tex = uploadTexture_RGBA32(w, h, data)
}
Example #13
0
func initScene(width, height int, init func()) (err error) {
	gl.Disable(gl.DEPTH_TEST)

	gl.ClearColor(0.5, 0.5, 0.5, 0.0)

	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, gl.Double(width), gl.Double(height), 0, 0, 1)
	gl.MatrixMode(gl.MODELVIEW)

	init()

	return
}
Example #14
0
// Draw draws the Sprite object.
func (sprite *Sprite) Draw(frame int) error {

	gl.BindBuffer(gl.ARRAY_BUFFER, sprite.shape.vertexBuffer)
	gl.VertexAttribPointer(gl.Uint(0), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0))
	gl.BindAttribLocation(paunchEffect.program, gl.Uint(0), gl.GLString("position"))
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	if sprite.texcoordBuffer != 0 {
		gl.ActiveTexture(gl.TEXTURE0)

		gl.BindBuffer(gl.ARRAY_BUFFER, sprite.texcoordBuffer)
		gl.VertexAttribPointer(gl.Uint(1), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0))
		gl.BindAttribLocation(paunchEffect.program, gl.Uint(0), gl.GLString("texcoord"))
		gl.BindBuffer(gl.ARRAY_BUFFER, 0)

		gl.BindTexture(gl.TEXTURE_2D, sprite.texture[frame])
		gl.EnableVertexAttribArray(gl.Uint(1))
	}

	gl.EnableVertexAttribArray(gl.Uint(0))
	gl.DrawArrays(gl.TRIANGLES, 0, gl.Sizei(sprite.shape.size))
	gl.DisableVertexAttribArray(gl.Uint(0))
	gl.DisableVertexAttribArray(gl.Uint(1))

	gl.BindTexture(gl.TEXTURE_2D, 0)

	return checkForErrors()
}
Example #15
0
func (vb *VertexBuffer) Render(position Point3) {
	gl.PushMatrix()
	gl.Translated(
		gl.Double(position.X),
		gl.Double(position.Y),
		gl.Double(position.Z))

	gl.VertexPointer(
		gl.Int(3),
		gl.DOUBLE,
		0,
		gl.Pointer(&vb.VertexData[0]))
	gl.ColorPointer(
		gl.Int(4),
		gl.DOUBLE,
		0,
		gl.Pointer(&vb.ColorData[0]))
	gl.TexCoordPointer(
		gl.Int(2),
		gl.DOUBLE,
		0,
		gl.Pointer(&vb.TexCoordData[0]))

	for i := range vb.RenderSteps {
		rs := &vb.RenderSteps[i]
		gl.DrawElements(
			rs.Mode,
			gl.Sizei(len(rs.Indices)),
			gl.UNSIGNED_INT,
			gl.Pointer(&rs.Indices[0]))
	}
	gl.PopMatrix()
}
Example #16
0
func Screenshot() {
	log.Println("screenshot")
	img := image.NewNRGBA(image.Rect(0, 0, Width, Height))
	gl.ReadPixels(0, 0, gl.Sizei(Width), gl.Sizei(Height), gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, gl.Pointer(&img.Pix[0]))
	// reverse byte order, opengl seems to use little endian.
	pix := img.Pix
	for i := 0; i < len(pix); i += 4 {
		pix[i+0], pix[i+1], pix[i+2], pix[i+3] = pix[i+3], pix[i+2], pix[i+1], pix[i+0]
	}
	fname := fmt.Sprintf("frame%04d.png", scroti)
	scroti++
	f, err := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
	core.Fatal(err)
	defer f.Close()
	core.Fatal(png.Encode(f, img))
}
Example #17
0
func (a *app) OnResize(w, h int) {
	oaspect := float64(a.imgW()) / float64(a.imgH())
	haspect := float64(w) / float64(h)
	vaspect := float64(h) / float64(w)
	var scx, scy float64
	if oaspect > haspect {
		scx = 1
		scy = haspect / oaspect
	} else {
		scx = vaspect * oaspect
		scy = 1
	}
	gl.Viewport(0, 0, gl.Sizei(w), gl.Sizei(h))
	gl.LoadIdentity()
	gl.Scaled(gl.Double(scx), gl.Double(scy), 1)
}
Example #18
0
func SetUpCamera(
	screenWidth int32,
	screenHeight int32,
	lb *LevelBlueprint,
	playerIndex int32) {
	pb := &lb.Players[playerIndex]

	// Calculate the distance between the near plane and the camera. We want
	// (PaddleAreaWidth / 2) / cameraDist = tan(CameraHorzFovDegrees / 2)
	t := math.Tan(math.Pi * Config.CameraHorzFovDegrees / 360)
	cameraDist := pb.PaddleAreaWidth / (2 * t)
	cameraPos := pb.PaddleAreaCenter.Minus(
		pb.Orientation.Forward.Times(cameraDist))

	// Set up the projection matrix.
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Viewport(0, 0, gl.Sizei(screenWidth), gl.Sizei(screenHeight))
	gl.Frustum(
		gl.Double(-pb.PaddleAreaWidth/2), gl.Double(pb.PaddleAreaWidth/2),
		gl.Double(-pb.PaddleAreaHeight/2), gl.Double(pb.PaddleAreaHeight/2),
		gl.Double(cameraDist), gl.Double(cameraDist+Config.ViewDepth))

	// Set up the modelview matrix.
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	transformation := [16]gl.Double{
		gl.Double(pb.Orientation.Right.X),
		gl.Double(pb.Orientation.Up.X),
		gl.Double(-pb.Orientation.Forward.X),
		gl.Double(0.0),
		gl.Double(pb.Orientation.Right.Y),
		gl.Double(pb.Orientation.Up.Y),
		gl.Double(-pb.Orientation.Forward.Y),
		gl.Double(0.0),
		gl.Double(pb.Orientation.Right.Z),
		gl.Double(pb.Orientation.Up.Z),
		gl.Double(-pb.Orientation.Forward.Z),
		gl.Double(0.0),
		gl.Double(0.0), gl.Double(0.0), gl.Double(0.0), gl.Double(1.0)}
	gl.MultMatrixd(&transformation[0])
	gl.Translated(
		gl.Double(-cameraPos.X),
		gl.Double(-cameraPos.Y),
		gl.Double(-cameraPos.Z))
}
Example #19
0
func (g *Game) RenderLocalGame(region g2.Region) {
	g.local.Camera.regionDims = linear.Vec2{float64(region.Dims.Dx), float64(region.Dims.Dy)}
	// func (g *Game) renderLocalHelper(region g2.Region, local *LocalData, camera *cameraInfo, side int) {
	g.local.Camera.FocusRegion(g, 0)
	gl.MatrixMode(gl.PROJECTION)
	gl.PushMatrix()
	gl.LoadIdentity()
	// Set the viewport so that we only render into the region that we're supposed
	// to render to.
	// TODO: Check if this works on all graphics cards - I've heard that the opengl
	// spec doesn't actually require that viewport does any clipping.
	gl.PushAttrib(gl.VIEWPORT_BIT)
	gl.Viewport(gl.Int(region.X), gl.Int(region.Y), gl.Sizei(region.Dx), gl.Sizei(region.Dy))
	defer gl.PopAttrib()

	current := &g.local.Camera.current
	gl.Ortho(
		gl.Double(current.mid.X-current.dims.X/2),
		gl.Double(current.mid.X+current.dims.X/2),
		gl.Double(current.mid.Y+current.dims.Y/2),
		gl.Double(current.mid.Y-current.dims.Y/2),
		gl.Double(1000),
		gl.Double(-1000),
	)
	defer func() {
		gl.MatrixMode(gl.PROJECTION)
		gl.PopMatrix()
		gl.MatrixMode(gl.MODELVIEW)
	}()
	gl.MatrixMode(gl.MODELVIEW)

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

	level := g.Level
	zoom := current.dims.X / float64(region.Dims.Dx)
	level.ManaSource.Draw(zoom, float64(level.Room.Dx), float64(level.Room.Dy))

	g.renderWalls()
	g.renderEdges()
	g.renderBases()
	g.renderEntsAndAbilities()
	g.renderProcesses()
	g.RenderLosMask()
}
Example #20
0
func (m *Model) initTexture() {
	//file, err := os.Open("model/test.png")
	file, err := os.Open("model/ceil.png")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	img, _, err := image.Decode(file)
	if err != nil {
		log.Fatal(err)
	}

	rgbaImg, ok := img.(*image.NRGBA)
	if !ok {
		//return 0, errors.New("texture must be an NRGBA image")
		log.Fatal("image is not rgba")
	}

	gl.GenTextures(1, &m.texture_id)
	gl.BindTexture(gl.TEXTURE_2D, m.texture_id)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)

	// flip image: first pixel is lower left corner
	img_width, img_height := img.Bounds().Dx(), img.Bounds().Dy()
	data := make([]byte, img_width*img_height*4)
	line_len := img_width * 4
	dest := len(data) - line_len
	for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride {
		copy(data[dest:dest+line_len], rgbaImg.Pix[src:src+rgbaImg.Stride])
		dest -= line_len
	}
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA, //4,
		gl.Sizei(img_width),
		gl.Sizei(img_height),
		0,
		gl.RGBA,
		gl.UNSIGNED_BYTE,
		gl.Pointer(&data[0]))

}
Example #21
0
func uploadTexture_RGBA32(w, h int, data []byte) gl.Uint {
	var id gl.Uint
	gl.GenTextures(1, &id)
	gl.BindTexture(gl.TEXTURE_2D, id)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(w), gl.Sizei(h), 0, gl.RGBA,
		gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))

	if gl.GetError() != gl.NO_ERROR {
		gl.DeleteTextures(1, &id)
		panic("Failed to load a texture")
	}
	return id
}
Example #22
0
func compileShader(shaderType ShaderType, scripts []*gl.Char) (gl.Uint, error) {

	shaderID := gl.CreateShader(gl.Enum(shaderType))
	gl.ShaderSource(shaderID, gl.Sizei(len(scripts)), &scripts[0], nil)
	gl.CompileShader(shaderID)
	if err := checkShaderCompiled(shaderID, shaderType); err != nil {
		return 0, err
	}

	return shaderID, checkForErrors()
}
Example #23
0
func glInit(width, height int) {
	gl.Init()
	gl.Enable(gl.TEXTURE_2D)
	gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, gl.Double(width), gl.Double(height), 0, -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	gl.EnableClientState(gl.VERTEX_ARRAY)
	gl.EnableClientState(gl.COLOR_ARRAY)
	gl.EnableClientState(gl.TEXTURE_COORD_ARRAY)
	gl.VertexPointer(2, gl.FLOAT, 0, gl.Pointer(&vs[0]))
	gl.ColorPointer(3, gl.UNSIGNED_BYTE, 0, gl.Pointer(&cs[0]))
	gl.TexCoordPointer(2, gl.FLOAT, 0, gl.Pointer(&ts[0]))
}
Example #24
0
func (o *OpenGl) initScene() (err error) {
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)

	ambient := []gl.Float{0.5, 0.5, 0.5, 1}
	diffuse := []gl.Float{1, 1, 1, 1}
	position := []gl.Float{-5, 5, 10, 0}
	gl.Lightfv(gl.LIGHT0, gl.AMBIENT, &ambient[0])
	gl.Lightfv(gl.LIGHT0, gl.DIFFUSE, &diffuse[0])
	gl.Lightfv(gl.LIGHT0, gl.POSITION, &position[0])

	gl.Viewport(0, 0, gl.Sizei(o.width), gl.Sizei(o.height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Frustum(-1, 1, -1, 1, 1.0, 10.0)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()

	return nil
}
Example #25
0
func (m *Model) draw() {
	m.shader.use()

	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, m.texture_id)
	gl.Uniform1i(m.shader.uniform_texture, 0)

	//texcoord
	if m.has_uv {
		gl.BindBuffer(gl.ARRAY_BUFFER, m.texcoord)
		gl.EnableVertexAttribArray(m.shader.attribute_texcoord)
		gl.VertexAttribPointer(
			m.shader.attribute_texcoord,
			2,
			gl.FLOAT,
			gl.FALSE,
			0,
			gl.Pointer(nil))
	}

	gl.BindBuffer(gl.ARRAY_BUFFER, m.buffer)
	gl.EnableVertexAttribArray(m.shader.attribute_vertex)
	gl.VertexAttribPointer(
		m.shader.attribute_vertex,
		3,
		gl.FLOAT,
		gl.FALSE,
		0,
		gl.Pointer(nil))

	gl.BindBuffer(gl.ARRAY_BUFFER, m.normal_buf)
	gl.EnableVertexAttribArray(m.shader.attribute_normal)
	gl.VertexAttribPointer(
		m.shader.attribute_normal,
		3,
		gl.FLOAT,
		gl.FALSE,
		0,
		gl.Pointer(nil))

	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, m.index)
	gl.DrawElements(gl.TRIANGLES, gl.Sizei(len(m.indices)), gl.UNSIGNED_INT, gl.Pointer(nil))

	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
	gl.DisableVertexAttribArray(m.shader.attribute_vertex)
	gl.DisableVertexAttribArray(m.shader.attribute_normal)
	if m.has_uv {
		gl.DisableVertexAttribArray(m.shader.attribute_texcoord)
	}

}
Example #26
0
// Sets up anything that wouldn't have been loaded from disk, including
// all opengl data, and sets up finalizers for that data.
func (d *Dictionary) setupGlStuff() {
	d.dlists = make(map[string]uint32)
	d.strs = make(map[string]strBuffer)
	d.pars = make(map[string]strBuffer)

	// TODO: This finalizer is untested
	runtime.SetFinalizer(d, func(d *Dictionary) {
		render.Queue(func() {
			for _, v := range d.dlists {
				gl.DeleteLists(gl.Uint(v), 1)
			}
		})
	})

	render.Queue(func() {
		gl.Enable(gl.TEXTURE_2D)
		gl.GenTextures(1, (*gl.Uint)(&d.texture))
		gl.BindTexture(gl.TEXTURE_2D, gl.Uint(d.texture))
		gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
		gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
		gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

		gl.TexImage2D(
			gl.TEXTURE_2D,
			0,
			gl.ALPHA,
			gl.Sizei(d.data.Dx),
			gl.Sizei(d.data.Dy),
			0,
			gl.ALPHA,
			gl.UNSIGNED_BYTE,
			gl.Pointer(&d.data.Pix[0]))

		gl.Disable(gl.TEXTURE_2D)
	})
}
Example #27
0
func (s *sheet) makeTexture(pixer <-chan []byte) {
	gl.Enable(gl.TEXTURE_2D)
	gl.GenTextures(1, &s.texture)
	gl.BindTexture(gl.TEXTURE_2D, s.texture)
	gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	data := <-pixer
	gl.TexImage2D(
		gl.TEXTURE_2D,
		0,
		gl.RGBA,
		gl.Sizei(s.dx),
		gl.Sizei(s.dy),
		0,
		gl.RGBA,
		gl.UNSIGNED_INT,
		gl.Pointer(&data[0]))
	memory.FreeBlock(data)
}
Example #28
0
// Draw draws the Shape object.
func (shape *Shape) Draw() error {

	gl.BindBuffer(gl.ARRAY_BUFFER, shape.vertexBuffer)
	vertexAttribLoc := gl.GetAttribLocation(paunchEffect.program, gl.GLString("position"))
	gl.VertexAttribPointer(gl.Uint(vertexAttribLoc), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0))
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	gl.EnableVertexAttribArray(gl.Uint(vertexAttribLoc))
	gl.DrawArrays(shape.mode, 0, gl.Sizei(shape.size/2))
	gl.DisableVertexAttribArray(gl.Uint(vertexAttribLoc))
	//gl.DisableVertexAttribArray(gl.Uint(1))

	//gl.BindTexture(gl.TEXTURE_2D, 0)

	return checkForErrors()
}
Example #29
0
func loadCubeToGPU() {
	newNormal(0, 0, 1)
	newVertex(-1, -1, 1)
	newVertex(1, -1, 1)
	newVertex(1, 1, 1)
	newVertex(-1, 1, 1)

	newNormal(0, 0, -1)
	newVertex(-1, -1, -1)
	newVertex(-1, 1, -1)
	newVertex(1, 1, -1)
	newVertex(1, -1, -1)

	newNormal(0, 1, 0)
	newVertex(-1, 1, -1)
	newVertex(-1, 1, 1)
	newVertex(1, 1, 1)
	newVertex(1, 1, -1)

	newNormal(0, -1, 0)
	newVertex(-1, -1, -1)
	newVertex(1, -1, -1)
	newVertex(1, -1, 1)
	newVertex(-1, -1, 1)

	newNormal(1, 0, 0)
	newVertex(1, -1, -1)
	newVertex(1, 1, -1)
	newVertex(1, 1, 1)
	newVertex(1, -1, 1)

	newNormal(-1, 0, 0)
	newVertex(-1, -1, -1)
	newVertex(-1, -1, 1)
	newVertex(-1, 1, 1)
	newVertex(-1, 1, -1)

	gl.GenBuffers(1, &gVBO)
	gl.BindBuffer(gl.ARRAY_BUFFER, gVBO)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(unsafe.Sizeof(Vertex{})*24), gl.Pointer(&Vertices[0]), gl.STATIC_DRAW)

	gl.EnableClientState(gl.VERTEX_ARRAY)
	gl.EnableClientState(gl.NORMAL_ARRAY)
	gl.VertexPointer(3, gl.FLOAT, 24, nil)
	tmpStruct := Vertex{}
	gl.NormalPointer(gl.FLOAT, gl.Sizei(unsafe.Sizeof(Vertex{})), gl.Pointer(unsafe.Offsetof(tmpStruct.normal)))
}
Example #30
0
func SetUniformV2Array(shader, variable string, vs []linear.Vec2) {
	prog, ok := shader_progs[shader]
	if !ok {
		if !warned_names[shader] {
			Warn().Printf("Tried to set a uniform in an unknown shader '%s'", shader)
			warned_names[shader] = true
		}
		return
	}
	bvariable := []byte(fmt.Sprintf("%s\x00", variable))
	loc := gl.GetUniformLocation(prog, (*gl.Char)(unsafe.Pointer(&bvariable[0])))
	var fs []gl.Float
	for i := range vs {
		fs = append(fs, gl.Float(vs[i].X))
		fs = append(fs, gl.Float(vs[i].Y))
	}
	gl.Uniform2fv(loc, gl.Sizei(len(vs)), (*gl.Float)(unsafe.Pointer(&fs[0])))
}