Skip to content
Snippets Groups Projects
Commit 05383a18 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Infrastructure to implement a vertex-baesd lighting model for GLTensors.

parent 6979e8a5
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,7 @@ class TensorOpts(vectoropts.VectorOpts):
# Enable/disable lighting effects
lighting = props.Boolean(default=False)
lighting = props.Boolean(default=True)
# Tensor ellipsoid resolution
......
......@@ -52,7 +52,7 @@ attribute float vertexID;
varying vec3 fragVoxCoord;
varying vec3 fragTexCoord;
varying vec4 fragColourFactor;
void main(void) {
......@@ -127,6 +127,7 @@ void main(void) {
voxToDisplayMat *
vec4(vertVoxCoord + vector, 1);
fragVoxCoord = voxCoord;
fragTexCoord = texCoord;
fragVoxCoord = voxCoord;
fragTexCoord = texCoord;
fragColourFactor = vec4(1, 1, 1, 1);
}
/*
* OpenGL vertex shader used for rendering GLRGBVector instances.
* All this shader does is set the vertex position, and pass the
* voxel and texture coordinates through to the fragment shader.
*
* Author: Paul McCarthy <pauldmccarthy@gmail.com>
*/
#version 120
attribute vec3 vertex;
attribute vec3 voxCoord;
attribute vec3 texCoord;
varying vec3 fragVoxCoord;
varying vec3 fragTexCoord;
varying vec4 fragColourFactor;
void main(void) {
fragVoxCoord = voxCoord;
fragTexCoord = texCoord;
fragColourFactor = vec4(1, 1, 1, 1);
gl_Position = gl_ModelViewProjectionMatrix * vec4(vertex, 1);
}
......@@ -103,7 +103,8 @@ def compileShaders(self):
'l1Texture', 'l2Texture', 'l3Texture',
'v1ValXform', 'v2ValXform', 'v3ValXform',
'l1ValXform', 'l2ValXform', 'l3ValXform',
'voxToDisplayMat', 'imageShape', 'resolution']
'voxToDisplayMat', 'imageShape', 'resolution',
'lighting']
vertAtts = ['voxel', 'vertex']
......@@ -181,10 +182,12 @@ def updateShaderState(self):
imageShape = np.array(self.image.shape[:3], dtype=np.float32)
resolution = opts.tensorResolution
modThreshold = opts.modThreshold / 100.0
lighting = 1 if opts.lighting else 0
useSpline = 0
gl.glUniform3fv(self.imageShapePos, 1, imageShape)
gl.glUniform1f( self.resolutionPos, resolution)
gl.glUniform1f( self.lightingPos, lighting)
gl.glUniform1f( self.modThresholdPos, modThreshold)
gl.glUniform1f( self.useSplinePos, useSpline)
......@@ -215,13 +218,21 @@ def preDraw(self):
"""Must be called before :func:`draw`. Loads the shader programs, binds
textures, and enables vertex arrays.
"""
gl.glUseProgram(self.shaders)
gl.glUseProgram(self.shaders)
if self.displayOpts.lighting:
gl.glEnable(gl.GL_LIGHTING)
gl.glEnable(gl.GL_LIGHT0)
# gl.glLight(gl.GL_LIGHT0, gl.GL_)
self.v1Texture.bindTexture(gl.GL_TEXTURE5)
self.v2Texture.bindTexture(gl.GL_TEXTURE6)
self.v3Texture.bindTexture(gl.GL_TEXTURE7)
self.l1Texture.bindTexture(gl.GL_TEXTURE8)
self.l2Texture.bindTexture(gl.GL_TEXTURE9)
self.l3Texture.bindTexture(gl.GL_TEXTURE10)
gl.glEnableVertexAttribArray(self.voxelPos)
gl.glEnableVertexAttribArray(self.vertexPos)
......@@ -284,13 +295,20 @@ def draw(self, zpos, xform=None):
def postDraw(self):
gl.glUseProgram(0)
if self.displayOpts.lighting:
gl.glDisable(gl.GL_LIGHT0)
gl.glDisable(gl.GL_LIGHTING)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0)
self.v1Texture.unbindTexture()
self.v2Texture.unbindTexture()
self.v3Texture.unbindTexture()
self.l1Texture.unbindTexture()
self.l2Texture.unbindTexture()
self.l3Texture.unbindTexture()
gl.glDisableVertexAttribArray(self.voxelPos)
gl.glDisableVertexAttribArray(self.vertexPos)
......@@ -25,17 +25,22 @@ uniform vec3 imageShape;
uniform float resolution;
uniform bool lighting;
attribute vec3 voxel;
attribute vec3 vertex;
varying vec3 fragVoxCoord;
varying vec3 fragTexCoord;
varying vec4 fragColourFactor;
void main(void) {
// Lookup the tensor parameters from the textures
vec3 texCoord = (voxel + 0.5) / imageShape;
vec3 light;
vec3 v1 = texture3D(v1Texture, texCoord).xyz;
vec3 v2 = texture3D(v2Texture, texCoord).xyz;
......@@ -58,7 +63,9 @@ void main(void) {
// shift it by the voxel coordinates
// to transform it in to the image
// voxel coordinate system.
vec3 pos = vertex;
vec3 pos = vertex;
// TODO scale by the range of l1/l2/l3
pos.x *= l1 * 150;
pos.y *= l2 * 150;
pos.z *= l3 * 150;
......@@ -66,11 +73,26 @@ void main(void) {
pos = mat3(v1, v2, v3) * pos;
pos += voxel;
if (lighting) {
vec3 norm = vertex;
norm.x /= l2;
norm.y /= l2;
norm.z /= l3;
norm = mat3(v1, v2, v3) * pos;
light = vec3(1, 1, 1);
}
else
light = vec3(1, 1, 1);
// Transform from voxels into display
gl_Position = gl_ModelViewProjectionMatrix *
voxToDisplayMat *
vec4(pos, 1);
fragVoxCoord = voxel;
fragTexCoord = texCoord;
fragVoxCoord = voxel;
fragTexCoord = texCoord;
fragColourFactor = vec4(light, 1);
}
......@@ -75,6 +75,13 @@ varying vec3 fragVoxCoord;
*/
varying vec3 fragTexCoord;
/*
* The final fragment colour is multiplied by this
* scaling factor - this may be used for vertex-based
* lighting.
*/
varying vec4 fragColourFactor;
void main(void) {
......@@ -137,5 +144,5 @@ void main(void) {
if (modValue < modThreshold)
voxColour.a = 0.0;
gl_FragColor = voxColour;
gl_FragColor = voxColour * fragColourFactor;
}
......@@ -243,12 +243,10 @@ _shaderTypePrefixMap = td.TypeDict({
('GLLabel', 'vert') : 'glvolume',
('GLLabel', 'frag') : 'gllabel',
('GLRGBVector', 'vert') : 'glvolume',
('GLRGBVector', 'vert') : 'glrgbvector',
('GLRGBVector', 'frag') : 'glvector',
('GLLineVector', 'vert') : 'gllinevector',
('GLLineVector', 'frag') : 'glvector',
('GLModel', 'vert') : 'glmodel',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment