Handles to reused GL buffers and texture

Introduced properties

private var _buffer_array: Int

gamnit :: ShadowContext :: _buffer_array

Buffer name for vertex data
private var _depth_texture: Int

gamnit :: ShadowContext :: _depth_texture

Depth attached to light_view_framebuffer
private var _light_view_framebuffer: Int

gamnit :: ShadowContext :: _light_view_framebuffer

Framebuffer for the light point of view
private var _screen_framebuffer: Int

gamnit :: ShadowContext :: _screen_framebuffer

Real screen framebuffer
private fun buffer_array: Int

gamnit :: ShadowContext :: buffer_array

Buffer name for vertex data
private fun buffer_array=(buffer_array: Int)

gamnit :: ShadowContext :: buffer_array=

Buffer name for vertex data
private fun depth_texture: Int

gamnit :: ShadowContext :: depth_texture

Depth attached to light_view_framebuffer
private fun depth_texture=(depth_texture: Int)

gamnit :: ShadowContext :: depth_texture=

Depth attached to light_view_framebuffer
private fun destroy

gamnit :: ShadowContext :: destroy

private fun destroyed=(destroyed: Bool)

gamnit :: ShadowContext :: destroyed=

private fun light_view_framebuffer: Int

gamnit :: ShadowContext :: light_view_framebuffer

Framebuffer for the light point of view
private fun light_view_framebuffer=(light_view_framebuffer: Int)

gamnit :: ShadowContext :: light_view_framebuffer=

Framebuffer for the light point of view
private fun prepare_once(display: GamnitDisplay, shadow_resolution: Int)

gamnit :: ShadowContext :: prepare_once

Prepare all attributes once per resolution change
private fun resize(display: GamnitDisplay, shadow_resolution: Int)

gamnit :: ShadowContext :: resize

Init size or resize depth_texture
private fun screen_framebuffer: Int

gamnit :: ShadowContext :: screen_framebuffer

Real screen framebuffer
private fun screen_framebuffer=(screen_framebuffer: Int)

gamnit :: ShadowContext :: screen_framebuffer=

Real screen framebuffer

Redefined properties

redef type SELF: ShadowContext

gamnit $ ShadowContext :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _buffer_array: Int

gamnit :: ShadowContext :: _buffer_array

Buffer name for vertex data
private var _depth_texture: Int

gamnit :: ShadowContext :: _depth_texture

Depth attached to light_view_framebuffer
private var _light_view_framebuffer: Int

gamnit :: ShadowContext :: _light_view_framebuffer

Framebuffer for the light point of view
private var _screen_framebuffer: Int

gamnit :: ShadowContext :: _screen_framebuffer

Real screen framebuffer
private fun buffer_array: Int

gamnit :: ShadowContext :: buffer_array

Buffer name for vertex data
private fun buffer_array=(buffer_array: Int)

gamnit :: ShadowContext :: buffer_array=

Buffer name for vertex data
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
private fun depth_texture: Int

gamnit :: ShadowContext :: depth_texture

Depth attached to light_view_framebuffer
private fun depth_texture=(depth_texture: Int)

gamnit :: ShadowContext :: depth_texture=

Depth attached to light_view_framebuffer
private fun destroy

gamnit :: ShadowContext :: destroy

private fun destroyed=(destroyed: Bool)

gamnit :: ShadowContext :: destroyed=

fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private fun light_view_framebuffer: Int

gamnit :: ShadowContext :: light_view_framebuffer

Framebuffer for the light point of view
private fun light_view_framebuffer=(light_view_framebuffer: Int)

gamnit :: ShadowContext :: light_view_framebuffer=

Framebuffer for the light point of view
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
private fun prepare_once(display: GamnitDisplay, shadow_resolution: Int)

gamnit :: ShadowContext :: prepare_once

Prepare all attributes once per resolution change
private fun resize(display: GamnitDisplay, shadow_resolution: Int)

gamnit :: ShadowContext :: resize

Init size or resize depth_texture
private fun screen_framebuffer: Int

gamnit :: ShadowContext :: screen_framebuffer

Real screen framebuffer
private fun screen_framebuffer=(screen_framebuffer: Int)

gamnit :: ShadowContext :: screen_framebuffer=

Real screen framebuffer
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram gamnit::shadow::ShadowContext ShadowContext core::Object Object gamnit::shadow::ShadowContext->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

gamnit $ ShadowContext
# Handles to reused GL buffers and texture
private class ShadowContext

	# Real screen framebuffer
	var screen_framebuffer: Int = -1

	# Framebuffer for the light point of view
	var light_view_framebuffer: Int = -1

	# Depth attached to `light_view_framebuffer`
	var depth_texture: Int = -1

	# Buffer name for vertex data
	var buffer_array: Int = -1

	# Prepare all attributes once per resolution change
	fun prepare_once(display: GamnitDisplay, shadow_resolution: Int)
	do
		assert display.gl_extensions.has("GL_OES_depth_texture")

		# Set aside the real screen framebuffer name
		var screen_framebuffer = glGetIntegerv(gl_FRAMEBUFFER_BINDING, 0)
		self.screen_framebuffer = screen_framebuffer

		# Framebuffer
		var framebuffer = glGenFramebuffers(1).first
		glBindFramebuffer(gl_FRAMEBUFFER, framebuffer)
		assert glIsFramebuffer(framebuffer)
		self.light_view_framebuffer = framebuffer
		var gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		# Depth & texture/color
		var textures = glGenTextures(1)
		self.depth_texture = textures[0]
		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		resize(display, shadow_resolution)

		# Array buffer
		buffer_array = glGenBuffers(1).first
		glBindBuffer(gl_ARRAY_BUFFER, buffer_array)
		assert glIsBuffer(buffer_array)
		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		## coord
		var data = new Array[Float]
		data.add_all([-1.0, -1.0, 0.0,
	                   1.0, -1.0, 0.0,
	                  -1.0,  1.0, 0.0,
	                   1.0,  1.0, 0.0])
		## tex_coord
		data.add_all([0.0, 0.0,
		              1.0, 0.0,
		              0.0, 1.0,
		              1.0, 1.0])
		var c_data = new GLfloatArray.from(data)
		glBufferData(gl_ARRAY_BUFFER, data.length*4, c_data.native_array, gl_STATIC_DRAW)

		glBindBuffer(gl_ARRAY_BUFFER, 0)

		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error
	end

	# Init size or resize `depth_texture`
	fun resize(display: GamnitDisplay, shadow_resolution: Int)
	do
		glBindFramebuffer(gl_FRAMEBUFFER, light_view_framebuffer)
		var gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		# Depth texture
		var depth_texture = self.depth_texture
		glActiveTexture gl_TEXTURE0
		glBindTexture(gl_TEXTURE_2D, depth_texture)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_LINEAR)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_NEAREST)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_S, gl_CLAMP_TO_EDGE)
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_WRAP_T, gl_CLAMP_TO_EDGE)
		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		# TODO support hardware shadows with GL ES 3.0 or GL_EXT_shadow_samplers
		#glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_COMPARE_MODE, ...)

		glTexImage2D(gl_TEXTURE_2D, 0, gl_DEPTH_COMPONENT,
		             shadow_resolution, shadow_resolution,
		             0, gl_DEPTH_COMPONENT, gl_UNSIGNED_SHORT, new Pointer.nul)
		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		glFramebufferTexture2D(gl_FRAMEBUFFER, gl_DEPTH_ATTACHMENT, gl_TEXTURE_2D, depth_texture, 0)
		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		# Check if the framebuffer is complete and valid
		assert glCheckFramebufferStatus(gl_FRAMEBUFFER) == gl_FRAMEBUFFER_COMPLETE

		# Take down
		glBindTexture(gl_TEXTURE_2D, 0)
		glBindFramebuffer(gl_FRAMEBUFFER, 0)
		gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error
	end

	var destroyed = false

	fun destroy
	do
		if destroyed then return
		destroyed = true

		# Free the buffer
		glDeleteBuffers([buffer_array])
		var gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error
		buffer_array = -1

		# Free the array and framebuffer plus its attachments
		glDeleteBuffers([buffer_array])
		glDeleteFramebuffers([light_view_framebuffer])
		glDeleteTextures([depth_texture])
	end
end
lib/gamnit/depth/shadow.nit:182,1--308,3