Пример #1
0
func main() {
	initialWidth, initialHeight := 1280, 720
	runtime.LockOSThread()
	if sdlInit := sdl.Init(sdl.INIT_VIDEO); sdlInit != 0 {
		panic("SDL init error")
	}
	reinitScreen(initialWidth, initialHeight)
	defer cleanExit(true, false)
	if err := gl.Init(); err != nil {
		panic(err)
	}
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.DEPTH)
	defer cleanExit(false, true)
	glSetupShaderProg(&shaderTextureCreator, false)
	glSetupShaderProg(&shaderTextureDisplay, true)
	glFillBuffer(rttVerts, &rttVertBuf)
	glFillBuffer(dispVerts, &dispVertBuf)
	gl.GenTextures(1, &rttFrameTex)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_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)
	if doRtt {
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize, texSize, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
		gl.GenFramebuffers(1, &rttFrameBuf)
		gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
		gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttFrameTex, 0)
		gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	} else {
		glFillTextureFromImageFile("texture.jpg")
	}
	gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.ClearColor(0.3, 0.6, 0.9, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.ActiveTexture(gl.TEXTURE0)
	for {
		if evt := sdl.PollEvent(); evt != nil {
			switch event := evt.(type) {
			case *sdl.ResizeEvent:
				reinitScreen(int(event.W), int(event.H))
			case *sdl.QuitEvent:
				return
			}
		} else {
			if doRtt {
				renderToTexture()
			}
			renderToScreen()
			sdl.GL_SwapBuffers()
		}
	}

	sdl.Quit()
}
Пример #2
0
func renderToTexture() {
	gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
	gl.Viewport(0, 0, texSize, texSize)
	gl.ClearColor(0.9, 0.6, 0.3, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.UseProgram(shaderTextureCreator.glProg)
	gl.BindBuffer(gl.ARRAY_BUFFER, rttVertBuf)
	gl.EnableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.VertexAttribPointer(shaderTextureCreator.attrPos, 2, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil))
	gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
	gl.DisableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Пример #3
0
func renderToTexture() {
	gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
	gl.Viewport(0, 0, texWidth, texHeight)
	gl.ClearColor(0.1, 0.2, 0.3, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.UseProgram(shaderTextureCreator.glProg)
	gl.BindTexture(gl.TEXTURE_3D, volTex)
	gl.Uniform1i(shaderTextureCreator.unifTex, 0)
	gl.BindBuffer(gl.ARRAY_BUFFER, rttVertBuf)
	gl.EnableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.VertexAttribPointer(shaderTextureCreator.attrPos, 2, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil))
	gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
	gl.DisableVertexAttribArray(shaderTextureCreator.attrPos)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Пример #4
0
func renderToScreen() {
	gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	gl.Viewport(0, 0, winWidth, winHeight)
	gl.ClearColor(0.1, 0.3, 0.2, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.UseProgram(shaderTextureDisplay.glProg)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.Uniform1i(shaderTextureDisplay.unifTex, 0)
	gl.BindBuffer(gl.ARRAY_BUFFER, dispVertBuf)
	gl.EnableVertexAttribArray(shaderTextureDisplay.attrPos)
	gl.VertexAttribPointer(shaderTextureDisplay.attrPos, 3, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil))
	gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
	gl.DisableVertexAttribArray(shaderTextureDisplay.attrPos)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)
}
Пример #5
0
func RenderFunction() {

	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.DrawArrays(gl.TRIANGLES, 0, 3)
	glfw.SwapBuffers()
}
Пример #6
0
Файл: main.go Проект: vron/fm
func display() {
	gl.ClearColor(0.19, 0.19, 0.21, 0)
	gl.ClearDepth(1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.UseProgram(theProgram)

	// Temporarily set the cam t clip
	id := gls.Identity()

	trans := getLookAtMatrix()
	//	log.Println(trans)
	gls.UniformMatrix4fv(wtcm_unif, 1, false, trans)

	gls.UniformMatrix4fv(mtwm_unif, 1, false, id)

	// And here we do the actual rendering

	// Generate a name for vufferobj and store it
	gl.GenBuffers(1, &positionBufferObject)
	// Bind it to a target
	gl.BindBuffer(gl.ARRAY_BUFFER, positionBufferObject)

	// Conver all nodes to a long array of vertexes!
	i := 0
	// TODO: Fix this, based on 4 per eleemnts
	vpos := make([]float32, len(elements)*4*4)
	elms := make(map[uint32]mS, len(elements))
	// Note that the order here is undefined!
	ii := 0
	for k, v := range elements {
		elms[k] = mS{ii, len(v.Nodes())}
		for _, vv := range v.Nodes() {
			ii++
			vpos[i] = float32(nodes[vv][0])
			i++
			vpos[i] = float32(nodes[vv][1])
			i++
			vpos[i] = float32(nodes[vv][2])
			i++
			vpos[i] = 1.0
			i++
		}
	}
	/*
		for _, v := range nodes {
			vpos[i] = float32(v[0])
			i++
			vpos[i] = float32(v[1])
			i++
			vpos[i] = float32(v[2])
			i++
		}
	*/
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(32/8*len(vpos)), (gl.Pointer)(&vpos[0]), gl.STATIC_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	gl.BindBuffer(gl.ARRAY_BUFFER, positionBufferObject)
	gl.EnableVertexAttribArray(0)
	gl.VertexAttribPointer(0, 4, gl.FLOAT, gl.FALSE, 0, (gl.Pointer)(nil))

	/*	for _,v := range elms {

		}
	*/
	for _, v := range elms {
		gl.DrawArrays(gl.LINE_LOOP, gl.Int(v.Start), gl.Sizei(v.Length))
	}

	// Draw Coordinate acis
	/*for i := 9; i < 9+2*5; i += 2 {
		gl.DrawArrays(gl.LINE_STRIP, gl.Int(i), 2)
	}*/
}
Пример #7
0
func main() {
	var now = time.Now()
	initialWidth, initialHeight := 1280, 720
	runtime.LockOSThread()
	if sdlInit := sdl.Init(sdl.INIT_VIDEO); sdlInit != 0 {
		panic("SDL init error")
	}
	reinitScreen(initialWidth, initialHeight)
	defer cleanExit(true, false)
	if err := gl.Init(); err != nil {
		panic(err)
	}
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.DEPTH)
	sdl.WM_SetCaption("Loading volume...", "")
	loadVolume()
	defer cleanExit(false, true)
	sdl.WM_SetCaption("Compiling shaders...", "")
	glSetupShaderProg(&shaderTextureCreator)
	glSetupShaderProg(&shaderTextureDisplay)
	glFillBuffer(rttVerts, &rttVertBuf)
	glFillBuffer(dispVerts, &dispVertBuf)
	gl.GenTextures(1, &rttFrameTex)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, glTexClamp)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, glTexClamp)
	if doRtt && !noRtt {
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
		gl.GenFramebuffers(1, &rttFrameBuf)
		gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
		gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttFrameTex, 0)
		gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	} else {
		if noRtt {
			rttFrameBuf = 0
		} else {
			glFillTextureFromImageFile("texture.jpg")
		}
	}
	gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.ClearColor(0.3, 0.2, 0.1, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	var looping = true
	var fps = 0
	var durSec time.Duration
	startTime, lastLoopTime, lastSecond = now, now, now
	sdl.WM_SetCaption("Up & running!", "")
	for looping {
		now = time.Now()
		if durSec = now.Sub(lastSecond); durSec.Seconds() >= 1 {
			sdl.WM_SetCaption(fmt.Sprintf("%v×%v @ %vfps", sdlScreen.W, sdlScreen.H, fps), "")
			fps = 0
			lastSecond = now
		} else {
			fps++
		}
		if evt := sdl.PollEvent(); evt != nil {
			switch event := evt.(type) {
			case *sdl.ResizeEvent:
				reinitScreen(int(event.W), int(event.H))
			case *sdl.QuitEvent:
				looping = false
			}
		}
		if doRtt || noRtt {
			renderToTexture()
		}
		if !noRtt {
			renderToScreen()
		}
		sdl.GL_SwapBuffers()
		lastLoopTime = now
	}
	sdl.Quit()
}