Commit 9fa4f133 by Kai Westerkamp

Lighting

parent 4fe659ca
...@@ -200,8 +200,12 @@ void Controler::addVolume(QString filePath){ ...@@ -200,8 +200,12 @@ void Controler::addVolume(QString filePath){
} }
//qDebug()<<file.readData(data,file.size()-file.pos()); //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++) 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); SceneVolume *volume = new SceneVolume(x,y,z,dx,dy,dz);
......
...@@ -4,59 +4,101 @@ uniform sampler2D PickID; ...@@ -4,59 +4,101 @@ uniform sampler2D PickID;
uniform sampler2D StartRay; uniform sampler2D StartRay;
uniform sampler2D StopRay; uniform sampler2D StopRay;
uniform sampler3D volumeData; uniform sampler3D volumeData;
uniform sampler1D transferData;
uniform mat4x4 NormalMatrix;
uniform bool MIP; uniform bool MIP;
varying vec3 position;
float stepsize = 0.001;
vec4 transfer(float scalar){ vec4 transfer(float scalar){
float pos = (scalar*255.0/256.0 + 0.5*256.0);
return vec4(scalar); return vec4(scalar);
//return texture1D(transferData,pos);
} }
void main(void) vec3 phong(vec3 color, vec3 normal){
{ vec3 V = normalize(-position);
// gl_FragColor = vec4(gl_TexCoord[0].st,0,1); vec3 N = normalize(normal);
//gl_FragColor = texture2D(PickID,gl_TexCoord[0].st); vec3 L = normalize(gl_LightSource[0].position.xyz-position);
//gl_FragColor = texture2D(StartRay,gl_TexCoord[0].st); vec3 R = normalize(reflect(-L,N));
gl_FragColor = vec4(texture2D(StopRay,gl_TexCoord[0].st).xyz,1);
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 front = texture2D(StartRay,gl_TexCoord[0].st).xyz;
vec3 back = texture2D(StopRay,gl_TexCoord[0].st).xyz; vec3 back = texture2D(StopRay,gl_TexCoord[0].st).xyz;
if(front == back){ if(front == back){
gl_FragColor = vec4(texture2D(Texture,gl_TexCoord[0].st).xyz,1); gl_FragColor = vec4(texture2D(Texture,gl_TexCoord[0].st).xyz,1);
}else{ }else{
vec3 ray = back-front; vec3 ray = back-front;
float rayLength = length(ray); // vec3 ray = front-back;
vec3 stepVector = 0.01 * ray/rayLength; 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; float density = texture3D(volumeData, pos).x;
vec4 dst = vec4(0); maxfloat = max(density,maxfloat);
float maxfloat = 0;
while (dst.a < 1 && rayLength > 0) { vec4 src = transfer(density);
vec3 normal = volumeNormal(pos);
float density = texture3D(volumeData, pos).x; src.xyz = phong(src.xyz,normal);
maxfloat = max(density,maxfloat); 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;
pos += stepVector; rayLength -= stepsize;
rayLength -= 0.01; }
}
if(MIP) { if(MIP) {
gl_FragColor = transfer(maxfloat); gl_FragColor = vec4(phong(transfer(maxfloat).xyz,normalMax),1) ;
}else{ }else{
gl_FragColor = dst; gl_FragColor = dst;
} }
// gl_FragColor = vec4(gl_TexCoord[0].xyz,1); // gl_FragColor = vec4(gl_TexCoord[0].xyz,1);
......
attribute vec4 gl_Vertex; attribute vec4 gl_Vertex;
uniform mat4 gl_ModelViewProjectionMatrix; uniform mat4 gl_ModelViewProjectionMatrix;
varying vec3 position;
void main(void) void main(void)
{ {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
position = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[0] = gl_MultiTexCoord0;
} }
#include "glview.h" #include "glview.h"
#include <QMatrix4x4>
...@@ -10,6 +11,7 @@ GLView::GLView(Scene *scene,Camera * camera,Controler *controler ) ...@@ -10,6 +11,7 @@ GLView::GLView(Scene *scene,Camera * camera,Controler *controler )
gridSize = 5; gridSize = 5;
gridStepSize = 1; gridStepSize = 1;
isGridEnabled = false; isGridEnabled = false;
MIP = false;
} }
QSize GLView::minimumSizeHint() const QSize GLView::minimumSizeHint() const
...@@ -36,6 +38,17 @@ void GLView::initializeGL ( ) { ...@@ -36,6 +38,17 @@ void GLView::initializeGL ( ) {
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); 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 //Shader Setup
initShader(); initShader();
...@@ -84,13 +97,17 @@ void GLView::home(){ ...@@ -84,13 +97,17 @@ void GLView::home(){
updateGL(); updateGL();
} }
void GLView::setMIP(bool mip){
this->MIP = mip;
}
void GLView::paintGL () void GLView::paintGL ()
{ {
bool useFBO = true; bool useFBO = true;
//QOpenGLFunctions functions = QOpenGLContext::currentContext()->functions(); //QOpenGLFunctions functions = QOpenGLContext::currentContext()->functions();
// QGLFunctions functions = QGLFunctions(this->context()); // QGLFunctions functions = QGLFunctions(this->context());
shader->bind(); shader->bind();
...@@ -118,6 +135,7 @@ void GLView::paintGL () ...@@ -118,6 +135,7 @@ void GLView::paintGL ()
shader->setUniformValue("shaded",true); shader->setUniformValue("shaded",true);
//set Projection and Camera Rotation //set Projection and Camera Rotation
camera->setupCamera(aspect); camera->setupCamera(aspect);
...@@ -157,6 +175,11 @@ void GLView::paintGL () ...@@ -157,6 +175,11 @@ void GLView::paintGL ()
shader->release(); shader->release();
if(useFBO){ if(useFBO){
displayShader->bind(); displayShader->bind();
displayShader->setUniformValue("MIP", MIP);
QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*camera->rotation);
displayShader->setUniformValue("NormalMatrix",mat.inverted().transposed());
// //
glBindFramebuffer(GL_FRAMEBUFFER,0); glBindFramebuffer(GL_FRAMEBUFFER,0);
...@@ -188,6 +211,10 @@ void GLView::paintGL () ...@@ -188,6 +211,10 @@ void GLView::paintGL ()
glBindTexture(GL_TEXTURE_3D,texture3D); glBindTexture(GL_TEXTURE_3D,texture3D);
displayShader->setUniformValue("volumeData",4); displayShader->setUniformValue("volumeData",4);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_1D,transferFunction);
displayShader->setUniformValue("transferData",5);
//displayShader->setUniformValue("active",scene->getActive()->getID()); //displayShader->setUniformValue("active",scene->getActive()->getID());
glBegin (GL_QUADS); glBegin (GL_QUADS);
...@@ -312,7 +339,7 @@ void GLView::resizeGL(int width , int height ) ...@@ -312,7 +339,7 @@ void GLView::resizeGL(int width , int height )
aspect = 1.0*width/height; aspect = 1.0*width/height;
//QGLFunctions functions = QGLFunctions(this->context()); //QGLFunctions functions = QGLFunctions(this->context());
//QOpenGLFunctions_4_3_Core functions = *this; //QOpenGLFunctions_4_3_Core functions = *this;
glGenFramebuffers(1, &fbo); glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER,fbo); glBindFramebuffer(GL_FRAMEBUFFER,fbo);
...@@ -372,7 +399,7 @@ void GLView::resizeGL(int width , int height ) ...@@ -372,7 +399,7 @@ void GLView::resizeGL(int width , int height )
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER); GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(err == GL_FRAMEBUFFER_COMPLETE){ if(err == GL_FRAMEBUFFER_COMPLETE){
// qDebug()<<"FBO OK"; // qDebug()<<"FBO OK";
} else { } else {
qDebug()<<"FBO ERROR"<<err; qDebug()<<"FBO ERROR"<<err;
} }
...@@ -382,26 +409,42 @@ void GLView::resizeGL(int width , int height ) ...@@ -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); glEnable(GL_TEXTURE_3D);
glGenTextures( 1, &texture3D ); glGenTextures( 1, &texture3D );
glBindTexture(GL_TEXTURE_3D, texture3D); glBindTexture(GL_TEXTURE_3D, texture3D);
// Filtering // Filtering
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Wrap // Wrap
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP ); 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_T, GL_CLAMP );
glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP );
glTexImage3D( GL_TEXTURE_3D, 0, GL_R8,// glTexImage3D( GL_TEXTURE_3D, 0, GL_R8,//
width, height, depth, 0, 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: ...@@ -27,7 +27,9 @@ public slots:
void setGridSize(int size){gridSize = size;} void setGridSize(int size){gridSize = size;}
void setGridStepSize(int size){gridStepSize = size;} void setGridStepSize(int size){gridStepSize = size;}
void showGrid(bool bo){isGridEnabled = bo;} 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: public:
GLView(Scene *scene,Camera * camera,Controler *controler ); GLView(Scene *scene,Camera * camera,Controler *controler );
...@@ -62,6 +64,8 @@ private: ...@@ -62,6 +64,8 @@ private:
GLuint stopRay; GLuint stopRay;
GLuint texture3D; GLuint texture3D;
GLuint transferFunction;
bool MIP;
void drawGrid(); 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