Documentation ¶
Overview ¶
glw document
Index ¶
- Constants
- Variables
- func GlRgba(img image.Image, rot matrix23) []gl.GLubyte
- func LoadImage(filename string) (image.Image, error)
- func LoadSkybox()
- func LoadTexture()
- type Buffer
- type CameraBuffer
- type DrawElement
- type DrawElementInstanced
- type ElementsUbyte
- type GlContext
- type GlError
- type GlLight
- type InstancedDrawer
- type InstancedMesh
- type Light
- type LightBuffer
- type ModelMatInstance
- type ModelMatInstances
- type Programs
- type Renderer
- type ShaderRef
- type ShaderRefs
- type ShaderSeed
- type ShaderType
- type Shaders
- type Uniforms
- type UniformsLoc
- type UniformsLocInstanced
- type UniformsNone
- type UninstancedDrawer
- type UninstancedMesh
- type VertexXyz
- type VertexXyzNor
- type VertexXyzNorUv
- type VertexXyzRgb
- type VerticesXyz
- type VerticesXyzNor
- type VerticesXyzNorUv
- type VerticesXyzRgb
Constants ¶
const ( CameraUboBindingPoint = iota LightUboBindingPoint )
const ( VERTEX_SHADER = ShaderType(gl.VERTEX_SHADER) GEOMETRY_SHADER = ShaderType(gl.GEOMETRY_SHADER) FRAGMENT_SHADER = ShaderType(gl.FRAGMENT_SHADER) )
const ( VSH_NOR_UV_INSTANCED = ShaderRef(iota) FSH_NOR_UV )
Variables ¶
var ERROR_NAMES = map[gl.GLenum]string{ gl.NO_ERROR: "no error", gl.INVALID_ENUM: "invalid enum", gl.INVALID_VALUE: "invalid value", gl.INVALID_OPERATION: "invalid operation", gl.STACK_OVERFLOW: "stack overflow", gl.STACK_UNDERFLOW: "stack underflow", gl.OUT_OF_MEMORY: "out of memory", gl.INVALID_FRAMEBUFFER_OPERATION: "invalid frame buffer operation", gl.TABLE_TOO_LARGE: "table too large", }
var SHADER_SOURCES = map[ShaderRef]ShaderSeed{ VSH_NOR_UV_INSTANCED: ShaderSeed{Type: VERTEX_SHADER, Source: ` #version 330 core layout(std140) uniform GlobalMatrices { mat4 eye_to_clip; mat4 eye_to_world; }; layout(location = 0) in vec3 vpos; layout(location = 1) in vec3 vnor; layout(location = 2) in vec2 vuv; layout(location = 3) in mat4 model_to_eye; // Instanced attribute. // Note that the model_to_eye matrix occupies 4 attribute positions. // The next layout location would be 7. out vec4 fpos_eye; // Fragment position in eye space. For View vector. out vec4 fnor_eye; out vec4 fnor_world; // Used to point to environment maps. out vec2 fuv; void main(){ fpos_eye = model_to_eye * vec4(vpos, 1.0); fnor_eye = model_to_eye * vec4(vnor, 0.0); fnor_world = eye_to_world * fnor_eye; fuv = vuv; gl_Position = eye_to_clip * fpos_eye; }`}, FSH_NOR_UV: ShaderSeed{Type: FRAGMENT_SHADER, Source: ` #version 330 core #define NB_LIGHTS_MAX 100 struct Light { vec4 color; vec4 origin; }; layout(std140) uniform GlobalLights { Light [NB_LIGHTS_MAX]lights; uint nb_lights; }; in vec4 fpos_eye; in vec4 fnor_eye; in vec4 fnor_world; // Environment map is in world space. in vec2 fuv; uniform samplerCube environment_map; uniform sampler2D albedo_map; uniform sampler2D normal_map; out vec3 color; #define PI 3.1415926535897932384626433832795 #define TAU 6.2831853071795864769252867665590 #define SQRT_TAU 2.5066282746310005024157652848110 // Schlick approximation of Fresnel's reflectance. // cspec is a specular color defined for normal incidence. // Typically 2..5 % for dielectrics, and 50..100 % for metals. // Dielectrics: r=g=b. In metals, it varies. // Note that I don't see any reason to let alpha out; I treat it as a color // until I have another use for it. Just say it's UV. // l is the direction of the light. It is pointing out of the surface. // h is half way between the direction of the light and that of the view. // It makes sense because we are looking at the microfacets that reflect the // light into our eyes, and these microfacets have a normal h. // Here, l and h must be in the same reference frame, it does not matter which // one. vec4 fresnel(vec4 cspec, vec3 l, vec3 h) { float cosangle = max(0, dot(l, h)); return cspec + (1 - cspec ) * pow(1 - cosangle, 5); } // Normal distribution term of microfacets. // h is a direction pointing out of the surface. // Currently expressed in the eye reference frame. This may have to change // when we start caring about anisotropy and/or normal mapping. float mfNormalDist(float sigma, vec3 v, vec3 h) { // This is a "normal distribution". Not to be confused with the normal // distribution. Yeah, same names for two things, amazing. This whole // function computes a distribution of normal vectors. To do this, it // uses a continuous probability distribution called "normal distribution". // It's also called "gaussian". float x = dot(h, v) - 1; float si = 1 / sigma; return SQRT_TAU * si * exp(-x*x*si*si*.5); } void main(){ vec3 normal_world = normalize(fnor_world.xyz); vec3 normal_eye = normalize(fnor_eye.xyz); // The 1000 here is an insanely high Level Of Detail which will fall down // to the blurriest version of the texture there is. This simulates the // lambertian reflection model of a surface: the ray from your eye is // reflected on the entire half-space that the surface sees, therefore you // see an average of that. // In other words: this is our ambiant lighting. Instead of taking a single // color, it takes its value from the environment map. This actually makes // perfect sense. At sunset, a white building will then seem orangeish on // one side. And a piece of paper held horizontally above a grass field // will be greenish on the lower side, and white (sun+sky) on the upper // side. color = textureLod(environment_map, normal_world, 1000).rgb; // Lights. for (uint i = 0; i < nb_lights; i++) { vec3 l_dir = lights[i].origin.xyz; float att = 1; if (lights[i].origin.w == 1) { // Point light, so l_dir contains now the position of the point // light, not the direction. l_dir -= fpos_eye.xyz; float distsq = dot(l_dir, l_dir); att = 1/distsq; } vec3 l_col = lights[i].color.rgb; color += l_col * max(dot(l_dir, normal_eye), 0) * att; } // Surface albedo. vec3 albedo = vec3(.1, .1, .1); color *= albedo; // Tone mapping. color /= 1+color; }`}, }
Functions ¶
func LoadSkybox ¶
func LoadSkybox()
func LoadTexture ¶
func LoadTexture()
Types ¶
type Buffer ¶
type Buffer interface { SetUp(gl.Program) // UpdateBuffer can be called every frame. It does nothing if it has // nothing to do. If needed, it creates a VBO on the fly, and/or fills // it with the OpenGL-friendly version of the Go-friendly vertexdata // passed with SetVertexData. Update() // Delete the OpenGL vertex buffer object. Delete() // contains filtered or unexported methods }
Concrete classes of array buffers must satisfy this public interface.
type CameraBuffer ¶
type CameraBuffer struct {
// contains filtered or unexported fields
}
func NewCameraBuffer ¶
func NewCameraBuffer(usage gl.GLenum, bindingPoint uint) *CameraBuffer
func (*CameraBuffer) Delete ¶
func (buffer *CameraBuffer) Delete()
Delete the OpenGL vertex buffer object.
func (*CameraBuffer) SetEyeToClp ¶
func (buffer *CameraBuffer) SetEyeToClp(matrix glm.Matrix4)
Data access.
func (*CameraBuffer) SetEyeToWld ¶
func (buffer *CameraBuffer) SetEyeToWld(matrix glm.Matrix4)
func (*CameraBuffer) Update ¶
func (buffer *CameraBuffer) Update()
type DrawElement ¶
type DrawElement struct {
// contains filtered or unexported fields
}
DrawElement contains the parameters required by gl.DrawElements.
func MakeDrawElement ¶
func (DrawElement) Draw ¶
func (drawer DrawElement) Draw()
This Draw method is a wrapper to gl.DrawElements.
type DrawElementInstanced ¶
type DrawElementInstanced struct {
// contains filtered or unexported fields
}
DrawElementInstance curries gl.DrawElementsInstanced.
func (DrawElementInstanced) Draw ¶
func (drawer DrawElementInstanced) Draw(primcount int)
type ElementsUbyte ¶
type ElementsUbyte struct {
// contains filtered or unexported fields
}
func NewElementsUbyte ¶
func NewElementsUbyte(usage gl.GLenum) *ElementsUbyte
func (*ElementsUbyte) Delete ¶
func (buffer *ElementsUbyte) Delete()
Delete the OpenGL vertex buffer object.
func (*ElementsUbyte) SetData ¶
func (buffer *ElementsUbyte) SetData(ed []gl.GLubyte)
func (*ElementsUbyte) SetUp ¶
func (buffer *ElementsUbyte) SetUp(program gl.Program)
func (*ElementsUbyte) Update ¶
func (buffer *ElementsUbyte) Update()
type GlContext ¶
type GlContext struct { Programs Programs // The Program in use. Set by ProgramBatch. Usable by uniform batches // when they need to validate their inputs before a draw call. Program gl.Program // contains filtered or unexported fields }
func NewGlContext ¶
func NewGlContext() *GlContext
func (*GlContext) SetEyeToClp ¶
func (*GlContext) SetEyeToWld ¶
func (*GlContext) UpdateCamera ¶
func (context *GlContext) UpdateCamera()
func (*GlContext) UpdateLights ¶
func (context *GlContext) UpdateLights()
type GlError ¶
func CheckGlError ¶
func CheckGlError() *GlError
type InstancedDrawer ¶
type InstancedDrawer interface {
Draw(primcount int)
}
Drawer interface abstracts all the possible OpenGL Draw calls. The Draw method takes no argument, it is assume that all the arguments are already curried.
type InstancedMesh ¶
type InstancedMesh struct { Drawer InstancedDrawer // contains filtered or unexported fields }
func NewInstancedMesh ¶
func NewInstancedMesh( p Programs, s ShaderRefs, v, e, i Buffer, u Uniforms, d InstancedDrawer) *InstancedMesh
func (*InstancedMesh) Render ¶
func (mesh *InstancedMesh) Render(locations []glm.Matrix4)
type Light ¶
type LightBuffer ¶
type LightBuffer struct {
// contains filtered or unexported fields
}
func NewLightBuffer ¶
func NewLightBuffer(usage gl.GLenum, bindingPoint uint) *LightBuffer
func (*LightBuffer) Delete ¶
func (buffer *LightBuffer) Delete()
Delete the OpenGL vertex buffer object.
func (*LightBuffer) SetLight ¶
func (buffer *LightBuffer) SetLight(i uint32, light Light)
func (*LightBuffer) SetLights ¶
func (buffer *LightBuffer) SetLights(lights []Light)
func (*LightBuffer) SetNbLights ¶
func (buffer *LightBuffer) SetNbLights(nbLights uint32)
func (*LightBuffer) Update ¶
func (buffer *LightBuffer) Update()
type ModelMatInstance ¶
ModelMatInstance defines transformation matrices for instanced rendering. This is a per-instance attribute, but it is read by the shader as a per-vertex attribute. It requires a VBO.
type ModelMatInstances ¶
type ModelMatInstances struct {
// contains filtered or unexported fields
}
func NewModelMatInstances ¶
func NewModelMatInstances(usage gl.GLenum) *ModelMatInstances
func (*ModelMatInstances) Delete ¶
func (buffer *ModelMatInstances) Delete()
Delete the OpenGL vertex buffer object.
func (*ModelMatInstances) SetLocationData ¶
func (buffer *ModelMatInstances) SetLocationData(locations []glm.Matrix4)
func (*ModelMatInstances) SetUp ¶
func (buffer *ModelMatInstances) SetUp(program gl.Program)
func (*ModelMatInstances) Update ¶
func (buffer *ModelMatInstances) Update()
type Programs ¶
type Programs struct {
// contains filtered or unexported fields
}
func NewPrograms ¶
func NewPrograms() Programs
type ShaderRefs ¶
type ShaderRefs []ShaderRef
Programs are uniquely identified by their shaders. I need to be able to sort shader references in order to create program references. Here I implement the interface required by the `sort` package.
func (ShaderRefs) Len ¶
func (self ShaderRefs) Len() int
func (ShaderRefs) Less ¶
func (self ShaderRefs) Less(i, j int) bool
func (ShaderRefs) Swap ¶
func (self ShaderRefs) Swap(i, j int)
type ShaderSeed ¶
type ShaderSeed struct { Type ShaderType Source string }
type ShaderType ¶
type Shaders ¶
This keeps track of the shaders that are in OpenGL. Each shader is known by the application by a unique identifier called "Shader reference", of type ShaderRef.
func MakeShaders ¶
func MakeShaders() Shaders
type UniformsLoc ¶
type UniformsLoc struct {
// contains filtered or unexported fields
}
----------------------------------------------------------------------------
func (*UniformsLoc) Location ¶
func (unif *UniformsLoc) Location() glm.Matrix4
func (*UniformsLoc) SetGl ¶
func (unif *UniformsLoc) SetGl()
func (*UniformsLoc) SetLocation ¶
func (unif *UniformsLoc) SetLocation(location glm.Matrix4)
func (*UniformsLoc) SetUp ¶
func (unif *UniformsLoc) SetUp(program gl.Program)
type UniformsLocInstanced ¶
type UniformsLocInstanced struct {
// contains filtered or unexported fields
}
func (*UniformsLocInstanced) SetGl ¶
func (unif *UniformsLocInstanced) SetGl()
func (*UniformsLocInstanced) SetLocation ¶
func (unif *UniformsLocInstanced) SetLocation(location glm.Matrix4)
func (*UniformsLocInstanced) SetUp ¶
func (unif *UniformsLocInstanced) SetUp(program gl.Program)
type UniformsNone ¶
type UniformsNone struct{}
To use where there is no uniform to deal with.
func (*UniformsNone) SetGl ¶
func (unif *UniformsNone) SetGl()
func (*UniformsNone) SetUp ¶
func (unif *UniformsNone) SetUp(program gl.Program)
type UninstancedDrawer ¶
type UninstancedDrawer interface {
Draw()
}
type UninstancedMesh ¶
type UninstancedMesh struct { Drawer UninstancedDrawer // contains filtered or unexported fields }
-----------------------------------------------------------------------------
func NewUninstancedMesh ¶
func NewUninstancedMesh( p Programs, s ShaderRefs, v, e Buffer, u Uniforms, d UninstancedDrawer, ) *UninstancedMesh
func (*UninstancedMesh) Render ¶
func (mesh *UninstancedMesh) Render(locations []glm.Matrix4)
type VertexXyz ¶
VertexXyz defines vertices that have a location and nothing else. No color, UV or any other parameter.
type VertexXyzNor ¶
VertexXyzNor defines vertices that have a position and a normal. It would be nice to compress the normals in a gl_int_2_10_10_10 once I figure out how to do it. The signs confuse me, as well as the fact that if normalized then no value seems to be able to represent 0.
func MakeVertexXyzNor ¶
func MakeVertexXyzNor(pos, nor glm.Vector3) VertexXyzNor
type VertexXyzNorUv ¶
type VertexXyzRgb ¶
VertexXyzRgb defines vertices that have a location and a color. No UV information. Note that there is no Alpha component to the color.
type VerticesXyz ¶
type VerticesXyz struct {
// contains filtered or unexported fields
}
func NewVerticesXyz ¶
func NewVerticesXyz(usage gl.GLenum) *VerticesXyz
func (*VerticesXyz) Delete ¶
func (buffer *VerticesXyz) Delete()
Delete the OpenGL vertex buffer object.
func (*VerticesXyz) SetData ¶
func (buffer *VerticesXyz) SetData(vd []VertexXyz)
func (*VerticesXyz) SetUp ¶
func (buffer *VerticesXyz) SetUp(program gl.Program)
func (*VerticesXyz) Update ¶
func (buffer *VerticesXyz) Update()
type VerticesXyzNor ¶
type VerticesXyzNor struct {
// contains filtered or unexported fields
}
func NewVerticesXyzNor ¶
func NewVerticesXyzNor(usage gl.GLenum) *VerticesXyzNor
func (*VerticesXyzNor) Delete ¶
func (buffer *VerticesXyzNor) Delete()
Delete the OpenGL vertex buffer object.
func (*VerticesXyzNor) SetData ¶
func (buffer *VerticesXyzNor) SetData(vd []VertexXyzNor)
func (*VerticesXyzNor) SetUp ¶
func (buffer *VerticesXyzNor) SetUp(program gl.Program)
func (*VerticesXyzNor) Update ¶
func (buffer *VerticesXyzNor) Update()
type VerticesXyzNorUv ¶
type VerticesXyzNorUv struct {
// contains filtered or unexported fields
}
func NewVerticesXyzNorUv ¶
func NewVerticesXyzNorUv(usage gl.GLenum) *VerticesXyzNorUv
func (*VerticesXyzNorUv) Delete ¶
func (buffer *VerticesXyzNorUv) Delete()
Delete the OpenGL vertex buffer object.
func (*VerticesXyzNorUv) SetData ¶
func (buffer *VerticesXyzNorUv) SetData(vd []VertexXyzNorUv)
func (*VerticesXyzNorUv) SetUp ¶
func (buffer *VerticesXyzNorUv) SetUp(program gl.Program)
func (*VerticesXyzNorUv) Update ¶
func (buffer *VerticesXyzNorUv) Update()
type VerticesXyzRgb ¶
type VerticesXyzRgb struct {
// contains filtered or unexported fields
}
func NewVerticesXyzRgb ¶
func NewVerticesXyzRgb(usage gl.GLenum) *VerticesXyzRgb
func (*VerticesXyzRgb) Delete ¶
func (buffer *VerticesXyzRgb) Delete()
Delete the OpenGL vertex buffer object.
func (*VerticesXyzRgb) SetData ¶
func (buffer *VerticesXyzRgb) SetData(vd []VertexXyzRgb)
func (*VerticesXyzRgb) SetUp ¶
func (buffer *VerticesXyzRgb) SetUp(program gl.Program)
func (*VerticesXyzRgb) Update ¶
func (buffer *VerticesXyzRgb) Update()