Commit 9fa4f133 by Kai Westerkamp

Lighting

parent 4fe659ca
......@@ -200,8 +200,12 @@ void Controler::addVolume(QString filePath){
}
//qDebug()<<file.readData(data,file.size()-file.pos());
int type = GL_UNSIGNED_BYTE;
if(rawdata.size()>x*y*z)
type = GL_UNSIGNED_SHORT;
for(int i = 0; i < 4; i++)
mainwindow->getViews()[i]->loadData(x,y,z,rawdata.data());
mainwindow->getViews()[i]->loadData(x,y,z,rawdata.data(),type);
SceneVolume *volume = new SceneVolume(x,y,z,dx,dy,dz);
......
......@@ -4,59 +4,101 @@ uniform sampler2D PickID;
uniform sampler2D StartRay;
uniform sampler2D StopRay;
uniform sampler3D volumeData;
uniform sampler1D transferData;
uniform mat4x4 NormalMatrix;
uniform bool MIP;
varying vec3 position;
float stepsize = 0.001;
vec4 transfer(float scalar){
float pos = (scalar*255.0/256.0 + 0.5*256.0);
return vec4(scalar);
//return texture1D(transferData,pos);
}
void main(void)
{
// gl_FragColor = vec4(gl_TexCoord[0].st,0,1);
//gl_FragColor = texture2D(PickID,gl_TexCoord[0].st);
//gl_FragColor = texture2D(StartRay,gl_TexCoord[0].st);
gl_FragColor = vec4(texture2D(StopRay,gl_TexCoord[0].st).xyz,1);
vec3 phong(vec3 color, vec3 normal){
vec3 V = normalize(-position);
vec3 N = normalize(normal);
vec3 L = normalize(gl_LightSource[0].position.xyz-position);
vec3 R = normalize(reflect(-L,N));
vec4 diffuse = vec4(max(dot(L,N),0.0));
float specular = pow(max(dot(R,V),0.0),16);
return (color * diffuse+ color * specular);
}
vec3 volumeNormal(vec3 uvw){
float stepsize = 0.01;
vec4 res = vec4(
texture3D(volumeData,uvw- vec3(stepsize,0,0)).r
-texture3D(volumeData,uvw+ vec3(stepsize,0,0)).r,
texture3D(volumeData, uvw- vec3(0,stepsize,0)).r
-texture3D(volumeData, uvw+ vec3(0,stepsize,0)).r,
// gl_FragColor = vec4(gl_TexCoord[0].st,0,1);
texture3D(volumeData, uvw- vec3(0,0,stepsize)).r
-texture3D(volumeData, uvw+ vec3(0,0,stepsize)).r
, 0);
res = NormalMatrix * res; //TODO Normal Matrix
return normalize(res.xyz);
}
void main(void)
{
vec3 front = texture2D(StartRay,gl_TexCoord[0].st).xyz;
vec3 back = texture2D(StopRay,gl_TexCoord[0].st).xyz;
if(front == back){
gl_FragColor = vec4(texture2D(Texture,gl_TexCoord[0].st).xyz,1);
}else{
vec3 ray = back-front;
float rayLength = length(ray);
vec3 stepVector = 0.01 * ray/rayLength;
// vec3 ray = front-back;
float rayLength = length(ray);
vec3 stepVector = stepsize * ray/rayLength;
vec3 pos = front;
vec4 dst = vec4(0);
float maxfloat = 0;
vec3 normalMax = vec3(0);
while (dst.a < 1 && rayLength > 0) {
vec3 pos = front;
vec4 dst = vec4(0);
float maxfloat = 0;
float density = texture3D(volumeData, pos).x;
maxfloat = max(density,maxfloat);
while (dst.a < 1 && rayLength > 0) {
vec4 src = transfer(density);
vec3 normal = volumeNormal(pos);
float density = texture3D(volumeData, pos).x;
maxfloat = max(density,maxfloat);
src.xyz = phong(src.xyz,normal);
src.rgb *= src.a;
vec4 src = transfer(density);
// dst = dst * (1.0f - src.a) + src;// * src.a;
// dst = (1.0 - dst.a) * src + dst ;
dst = src + dst*src.a ;
src.rgb *= src.a;
if(density>maxfloat){
maxfloat = density;
normalMax = normal;
}
dst = (1.0 - dst.a) * src + dst;
pos += stepVector;
rayLength -= 0.01;
}
pos += stepVector;
rayLength -= stepsize;
}
if(MIP) {
gl_FragColor = transfer(maxfloat);
}else{
if(MIP) {
gl_FragColor = vec4(phong(transfer(maxfloat).xyz,normalMax),1) ;
}else{
gl_FragColor = dst;
}
}
// gl_FragColor = vec4(gl_TexCoord[0].xyz,1);
......
attribute vec4 gl_Vertex;
uniform mat4 gl_ModelViewProjectionMatrix;
varying vec3 position;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
position = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_TexCoord[0] = gl_MultiTexCoord0;
}
#include "glview.h"
#include <QMatrix4x4>
......@@ -10,6 +11,7 @@ GLView::GLView(Scene *scene,Camera * camera,Controler *controler )
gridSize = 5;
gridStepSize = 1;
isGridEnabled = false;
MIP = false;
}
QSize GLView::minimumSizeHint() const
......@@ -36,6 +38,17 @@ void GLView::initializeGL ( ) {
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
uchar *trans = new uchar[4*256];
for(int i = 0; i < 256; i+=1){
int index = i*4;
trans[index] = i;
trans[index+1] = i;
trans[index+2] = i;
trans[index+3] = i;
}
loadTransfer(trans);
//Shader Setup
initShader();
......@@ -84,13 +97,17 @@ void GLView::home(){
updateGL();
}
void GLView::setMIP(bool mip){
this->MIP = mip;
}
void GLView::paintGL ()
{
bool useFBO = true;
//QOpenGLFunctions functions = QOpenGLContext::currentContext()->functions();
// QGLFunctions functions = QGLFunctions(this->context());
// QGLFunctions functions = QGLFunctions(this->context());
shader->bind();
......@@ -118,6 +135,7 @@ void GLView::paintGL ()
shader->setUniformValue("shaded",true);
//set Projection and Camera Rotation
camera->setupCamera(aspect);
......@@ -157,6 +175,11 @@ void GLView::paintGL ()
shader->release();
if(useFBO){
displayShader->bind();
displayShader->setUniformValue("MIP", MIP);
QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*camera->rotation);
displayShader->setUniformValue("NormalMatrix",mat.inverted().transposed());
//
glBindFramebuffer(GL_FRAMEBUFFER,0);
......@@ -188,6 +211,10 @@ void GLView::paintGL ()
glBindTexture(GL_TEXTURE_3D,texture3D);
displayShader->setUniformValue("volumeData",4);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_1D,transferFunction);
displayShader->setUniformValue("transferData",5);
//displayShader->setUniformValue("active",scene->getActive()->getID());
glBegin (GL_QUADS);
......@@ -312,7 +339,7 @@ void GLView::resizeGL(int width , int height )
aspect = 1.0*width/height;
//QGLFunctions functions = QGLFunctions(this->context());
//QOpenGLFunctions_4_3_Core functions = *this;
//QOpenGLFunctions_4_3_Core functions = *this;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER,fbo);
......@@ -372,7 +399,7 @@ void GLView::resizeGL(int width , int height )
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(err == GL_FRAMEBUFFER_COMPLETE){
// qDebug()<<"FBO OK";
// qDebug()<<"FBO OK";
} else {
qDebug()<<"FBO ERROR"<<err;
}
......@@ -382,26 +409,42 @@ void GLView::resizeGL(int width , int height )
}
void GLView::loadData(int width, int height, int depth, char* data )
{
void GLView::loadData(int width, int height, int depth, char* data, int type )
{
glEnable(GL_TEXTURE_3D);
glGenTextures( 1, &texture3D );
glBindTexture(GL_TEXTURE_3D, texture3D);
glEnable(GL_TEXTURE_3D);
glGenTextures( 1, &texture3D );
glBindTexture(GL_TEXTURE_3D, texture3D);
// Filtering
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Filtering
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Wrap
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP );
// Wrap
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP );
glTexImage3D( GL_TEXTURE_3D, 0, GL_R8,//
width, height, depth, 0,
GL_RED, GL_UNSIGNED_BYTE, data); // Imagedata as ByteBuffer
}
GL_RED,type , data); // Imagedata as ByteBuffer
}
void GLView::loadTransfer(uchar* data )
{
glGenTextures( 1, &transferFunction );
glBindTexture(GL_TEXTURE_1D, transferFunction);
// Filtering
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Wrap
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,256,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
}
......
......@@ -27,7 +27,9 @@ public slots:
void setGridSize(int size){gridSize = size;}
void setGridStepSize(int size){gridStepSize = size;}
void showGrid(bool bo){isGridEnabled = bo;}
void loadData(int width, int height, int depth, char* data);
void loadData(int width, int height, int depth, char* data, int type);
void loadTransfer(uchar* data);
void setMIP(bool mip);
public:
GLView(Scene *scene,Camera * camera,Controler *controler );
......@@ -62,6 +64,8 @@ private:
GLuint stopRay;
GLuint texture3D;
GLuint transferFunction;
bool MIP;
void drawGrid();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment