Пример #1
0
// Draw actually renders the shape on the surface.
func (box *Box) Draw() {
	box.program.Use()

	gl.VertexAttribPointer(box.posId, 2, gl.FLOAT, false, 0, &box.vertices[0])
	gl.EnableVertexAttribArray(box.posId)

	gl.VertexAttribPointer(box.colorId, 4, gl.FLOAT, false, 0, &box.vColor[0])
	gl.EnableVertexAttribArray(box.colorId)

	gl.UniformMatrix4fv(int32(box.modelMatrixId), 1, false, (*float32)(&box.modelMatrix[0]))
	gl.UniformMatrix4fv(int32(box.projMatrixId), 1, false, (*float32)(&box.projMatrix[0]))
	gl.UniformMatrix4fv(int32(box.viewMatrixId), 1, false, (*float32)(&box.viewMatrix[0]))

	gl.Uniform1f(int32(box.texRatioId), 0.0)

	// Texture
	if len(box.texCoords) > 0 {
		gl.Uniform1f(int32(box.texRatioId), 1.0)
		gl.VertexAttribPointer(box.texInId, 2, gl.FLOAT, false, 0, &box.texCoords[0])
		gl.EnableVertexAttribArray(box.texInId)
		gl.ActiveTexture(gl.TEXTURE0)
		gl.BindTexture(gl.TEXTURE_2D, box.texBuffer)
		gl.Uniform1i(int32(box.textureId), 0)
	}

	gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)

	gl.Flush()
	gl.Finish()
}
Пример #2
0
func (renderState *renderState) init(window gorgasm.Window) {
	window.MakeContextCurrent()

	renderState.window = window
	width, height := window.GetSize()

	// Set the viewport
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	check()

	// Compile the shaders
	program := Program(FragmentShader(fsh), VertexShader(vsh))
	gl.UseProgram(program)
	check()

	// Get attributes
	attrPos = uint32(gl.GetAttribLocation(program, "pos"))
	attrTexIn = uint32(gl.GetAttribLocation(program, "texIn"))
	unifTexture = gl.GetUniformLocation(program, "texture")
	gl.EnableVertexAttribArray(attrPos)
	gl.EnableVertexAttribArray(attrTexIn)
	check()

	// Upload vertices data
	gl.GenBuffers(1, &verticesArrayBuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, verticesArrayBuffer)
	gl.BufferData(gl.ARRAY_BUFFER, gl.SizeiPtr(len(vertices))*4, gl.Void(&vertices[0]), gl.STATIC_DRAW)
	check()

	// Upload texture data
	img, err := loadImage(GOPHER_PNG)
	if err != nil {
		panic(err)
	}

	// Prepare the image to be placed on a texture.
	bounds := img.Bounds()
	imgWidth, imgHeight := bounds.Size().X, bounds.Size().Y
	buffer := make([]byte, imgWidth*imgHeight*4)
	index := 0
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, a := img.At(x, y).RGBA()
			buffer[index] = byte(r)
			buffer[index+1] = byte(g)
			buffer[index+2] = byte(b)
			buffer[index+3] = byte(a)
			index += 4
		}
	}

	gl.GenTextures(1, &textureBuffer)
	gl.BindTexture(gl.TEXTURE_2D, textureBuffer)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Void(&buffer[0]))
	check()

	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
}
Пример #3
0
func (renderState *renderState) init(window mandala.Window) {
	window.MakeContextCurrent()

	renderState.window = window
	width, height := window.GetSize()

	// Set the viewport
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
	check()

	// Compile the shaders
	program := shaders.NewProgram(fsh, vsh)
	program.Use()
	check()

	// Get attributes
	attrPos = program.GetAttribute("pos")
	attrTexIn = program.GetAttribute("texIn")
	unifTexture = program.GetUniform("texture")
	gl.EnableVertexAttribArray(attrPos)
	gl.EnableVertexAttribArray(attrTexIn)
	check()

	// Upload texture data
	img, err := loadImage(GOPHER_PNG)
	if err != nil {
		panic(err)
	}

	// Prepare the image to be placed on a texture.
	bounds := img.Bounds()
	imgWidth, imgHeight := bounds.Size().X, bounds.Size().Y
	buffer := make([]byte, imgWidth*imgHeight*4)
	index := 0
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, a := img.At(x, y).RGBA()
			buffer[index] = byte(r)
			buffer[index+1] = byte(g)
			buffer[index+2] = byte(b)
			buffer[index+3] = byte(a)
			index += 4
		}
	}

	gl.GenTextures(1, &textureBuffer)
	gl.BindTexture(gl.TEXTURE_2D, textureBuffer)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Void(&buffer[0]))
	check()

	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
}
Пример #4
0
// Draw actually renders the segment on the surface.
func (segment *Segment) Draw() {
	segment.program.Use()
	gl.VertexAttribPointer(segment.posId, 2, gl.FLOAT, false, 0, &segment.vertices[0])
	gl.EnableVertexAttribArray(segment.posId)

	gl.VertexAttribPointer(segment.colorId, 4, gl.FLOAT, false, 0, &segment.vColor[0])
	gl.EnableVertexAttribArray(segment.colorId)

	gl.UniformMatrix4fv(int32(segment.modelMatrixId), 1, false, (*float32)(&segment.modelMatrix[0]))
	gl.UniformMatrix4fv(int32(segment.projMatrixId), 1, false, (*float32)(&segment.projMatrix[0]))
	gl.UniformMatrix4fv(int32(segment.viewMatrixId), 1, false, (*float32)(&segment.viewMatrix[0]))

	gl.DrawArrays(gl.LINES, 0, 2)

	gl.Flush()
	gl.Finish()
}
Пример #5
0
func NewCube() *Cube {
	cube := new(Cube)
	cube.model = mathgl.Ident4f()

	cube.Vertices = NewBufferFloat([]float32{
		// Front
		1, -1, 1, 1, TEX_COORD_MAX, 0,
		1, 1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, 1, 1, 0, TEX_COORD_MAX,
		-1, -1, 1, 1, 0, 0,
		// Back
		1, 1, -1, 1, TEX_COORD_MAX, 0,
		-1, -1, -1, 1, 0, TEX_COORD_MAX,
		1, -1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, 0,
		// Left
		-1, -1, 1, 1, TEX_COORD_MAX, 0,
		-1, 1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, TEX_COORD_MAX,
		-1, -1, -1, 1, 0, 0,
		// Right
		1, -1, -1, 1, TEX_COORD_MAX, 0,
		1, 1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		1, 1, 1, 1, 0, TEX_COORD_MAX,
		1, -1, 1, 1, 0, 0,
		// Top
		1, 1, 1, 1, TEX_COORD_MAX, 0,
		1, 1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, TEX_COORD_MAX,
		-1, 1, 1, 1, 0, 0,
		// Bottom
		1, -1, -1, 1, TEX_COORD_MAX, 0,
		1, -1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, -1, 1, 1, 0, TEX_COORD_MAX,
		-1, -1, -1, 1, 0, 0,
	})
	cube.indices = NewBufferByte([]byte{
		// Front
		0, 1, 2,
		2, 3, 0,
		// Back
		4, 5, 6,
		4, 5, 7,
		// Left
		8, 9, 10,
		10, 11, 8,
		// Right
		12, 13, 14,
		14, 15, 12,
		// Top
		16, 17, 18,
		18, 19, 16,
		// Bottom
		20, 21, 22,
		22, 23, 20,
	})

	fragmentShader := (FragmentShader)(`
        precision highp float;
	varying vec2 texOut;
        uniform sampler2D texture;

	void main() {
		gl_FragColor = texture2D(texture, texOut);
	}
        `)
	vertexShader := (VertexShader)(`
        uniform mat4 model;
        uniform mat4 projection_view;
        attribute vec4 pos;
        attribute vec2 texIn;
        varying vec2 texOut;

        void main() {
          gl_Position = projection_view*model*pos;
          texOut = texIn;
        }
        `)

	fsh := fragmentShader.Compile()
	vsh := vertexShader.Compile()
	cube.Program.Link(fsh, vsh)

	cube.Program.Use()
	cube.attrPos = cube.Program.GetAttribute("pos")
	cube.attrColor = cube.Program.GetAttribute("color")
	cube.attrTexIn = cube.Program.GetAttribute("texIn")
	cube.uniformTexture = cube.Program.GetUniform("texture")

	cube.uniformModel = cube.Program.GetUniform("model")
	cube.uniformProjectionView = cube.Program.GetUniform("projection_view")

	gl.EnableVertexAttribArray(cube.attrPos)
	gl.EnableVertexAttribArray(cube.attrColor)
	gl.EnableVertexAttribArray(cube.attrTexIn)

	return cube
}