Commit 4fe659ca by Kai Westerkamp

ray marching

parent 4685d45b
...@@ -176,7 +176,7 @@ void Controler::addVolume(QString filePath){ ...@@ -176,7 +176,7 @@ void Controler::addVolume(QString filePath){
int count = 0; int count = 0;
// std::cout<<std::hex; // std::cout<<std::hex;
int printLine = 150; int printLine = 400;// z/2;
for(int k = 0; k < z; k++) for(int k = 0; k < z; k++)
{ {
......
uniform sampler2D Texture; uniform sampler2D Texture;
uniform sampler2D TextureID; uniform sampler2D PickID;
uniform int active;
uniform sampler2D StartRay;
uniform sampler2D StopRay;
uniform sampler3D volumeData;
varying vec4 qt_TexCoord0; uniform bool MIP;
vec4 transfer(float scalar){
return vec4(scalar);
}
void main(void) void main(void)
{ {
gl_FragColor = texture2D(Texture,gl_TexCoord[0].st);
//gl_FragColor = texture2D(TextureID,gl_TexCoord[0].st)/255;
//gl_FragColor = vec4(active);
// gl_FragColor = vec4(gl_TexCoord[0].st,0,1); // 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);
// gl_FragColor = vec4(gl_TexCoord[0].st,0,1);
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 pos = front;
vec4 dst = vec4(0);
float maxfloat = 0;
while (dst.a < 1 && rayLength > 0) {
float density = texture3D(volumeData, pos).x;
maxfloat = max(density,maxfloat);
vec4 src = transfer(density);
src.rgb *= src.a;
dst = (1.0 - dst.a) * src + dst;
pos += stepVector;
rayLength -= 0.01;
}
if(MIP) {
gl_FragColor = transfer(maxfloat);
}else{
gl_FragColor = dst;
}
// gl_FragColor = vec4(gl_TexCoord[0].xyz,1);
//gl_FragColor = vec4(vec3(count),1);
//gl_FragColor = vec4(normalize(step),1);
//gl_FragColor = vec4(normalize(gl_ModelViewProjectionMatrixInverse*vec4(0,1,0,0.0)));
//gl_FragColor = vec4(texture3D(volumeData,vec3(front.xy,0.5)).x);
}
} }
...@@ -56,8 +56,9 @@ void GLView::initShader() ...@@ -56,8 +56,9 @@ void GLView::initShader()
shader->addShader(vertex); shader->addShader(vertex);
shader->addShader(fragment); shader->addShader(fragment);
shader->bindAttributeLocation("pickID",4); shader->bindAttributeLocation("pickID",4);
if(!shader->link()) if(!shader->link()){
qCritical()<< "Linking failed"<<shader->log(); qCritical()<< "Linking failed"<<shader->log();
}
...@@ -72,8 +73,10 @@ void GLView::initShader() ...@@ -72,8 +73,10 @@ void GLView::initShader()
displayShader = new QGLShaderProgram(this); displayShader = new QGLShaderProgram(this);
displayShader->addShader(vertex); displayShader->addShader(vertex);
displayShader->addShader(fragment); displayShader->addShader(fragment);
if(!displayShader->link()) if(!displayShader->link()){
qCritical()<< "Linking failed"<<displayShader->log(); qCritical()<< "Linking 2 failed:"<<displayShader->log();
}
} }
void GLView::home(){ void GLView::home(){
...@@ -93,8 +96,8 @@ void GLView::paintGL () ...@@ -93,8 +96,8 @@ void GLView::paintGL ()
shader->bind(); shader->bind();
if(useFBO){ if(useFBO){
glBindFramebuffer(GL_FRAMEBUFFER,fbo); glBindFramebuffer(GL_FRAMEBUFFER,fbo);
GLenum buffers[] = {GL_COLOR_ATTACHMENT0,GL_COLOR_ATTACHMENT1}; GLenum buffers[] = {GL_COLOR_ATTACHMENT0,GL_COLOR_ATTACHMENT1,GL_COLOR_ATTACHMENT2,GL_COLOR_ATTACHMENT3};
glDrawBuffers(2,buffers); glDrawBuffers(4,buffers);
//http://stackoverflow.com/questions/7207422/setting-up-opengl-multiple-render-targets //http://stackoverflow.com/questions/7207422/setting-up-opengl-multiple-render-targets
} }
...@@ -102,6 +105,7 @@ void GLView::paintGL () ...@@ -102,6 +105,7 @@ void GLView::paintGL ()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glViewport(0,0,this->width(),this->height()); glViewport(0,0,this->width(),this->height());
...@@ -110,9 +114,7 @@ void GLView::paintGL () ...@@ -110,9 +114,7 @@ void GLView::paintGL ()
glMatrixMode (GL_PROJECTION); glMatrixMode (GL_PROJECTION);
glLoadIdentity (); glLoadIdentity ();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D,texture3D);
displayShader->setUniformValue("volumeData",0);
shader->setUniformValue("shaded",true); shader->setUniformValue("shaded",true);
...@@ -172,9 +174,21 @@ void GLView::paintGL () ...@@ -172,9 +174,21 @@ void GLView::paintGL ()
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,picID); glBindTexture(GL_TEXTURE_2D,picID);
displayShader->setUniformValue("TextureID",1); displayShader->setUniformValue("PickID",1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D,startRay);
displayShader->setUniformValue("StartRay",2);
displayShader->setUniformValue("active",scene->getActive()->getID()); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D,stopRay);
displayShader->setUniformValue("StopRay",3);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_3D,texture3D);
displayShader->setUniformValue("volumeData",4);
//displayShader->setUniformValue("active",scene->getActive()->getID());
glBegin (GL_QUADS); glBegin (GL_QUADS);
glTexCoord2d(0,0); glTexCoord2d(0,0);
...@@ -315,7 +329,7 @@ void GLView::resizeGL(int width , int height ) ...@@ -315,7 +329,7 @@ void GLView::resizeGL(int width , int height )
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create the color buffer // Create the pcik buffer
glGenTextures(1, &picID); glGenTextures(1, &picID);
glBindTexture(GL_TEXTURE_2D, picID); glBindTexture(GL_TEXTURE_2D, picID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
...@@ -325,6 +339,26 @@ void GLView::resizeGL(int width , int height ) ...@@ -325,6 +339,26 @@ void GLView::resizeGL(int width , int height )
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create the pcik startRay
glGenTextures(1, &startRay);
glBindTexture(GL_TEXTURE_2D, startRay);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create the pcik stopRay
glGenTextures(1, &stopRay);
glBindTexture(GL_TEXTURE_2D, stopRay);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create the depth buffer // Create the depth buffer
glGenRenderbuffers(1, &depth); glGenRenderbuffers(1, &depth);
glBindRenderbuffer(GL_RENDERBUFFER, depth); glBindRenderbuffer(GL_RENDERBUFFER, depth);
...@@ -332,6 +366,8 @@ void GLView::resizeGL(int width , int height ) ...@@ -332,6 +366,8 @@ void GLView::resizeGL(int width , int height )
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,color,0); glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,color,0);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT1,GL_TEXTURE_2D,picID,0); glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT1,GL_TEXTURE_2D,picID,0);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT2,GL_TEXTURE_2D,startRay,0);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT3,GL_TEXTURE_2D,stopRay,0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depth); glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depth);
GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER); GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
......
...@@ -58,6 +58,9 @@ private: ...@@ -58,6 +58,9 @@ private:
GLuint picID; GLuint picID;
GLuint depth; GLuint depth;
GLuint startRay;
GLuint stopRay;
GLuint texture3D; GLuint texture3D;
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2015-12-03T11:54:44. --> <!-- Written by QtCreator 3.5.1, 2015-12-05T11:07:00. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
......
...@@ -303,7 +303,7 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -303,7 +303,7 @@ MainWindow::MainWindow(QWidget *parent)
void MainWindow::initScene(){ void MainWindow::initScene(){
//scene->simpleScene(); //scene->simpleScene();
controler->addVolume("D:/Projekte/GraPa/A3/lobster.raw"); // controler->addVolume("D:/Projekte/GraPa/A3/MRI-head.raw");
timer->stop(); timer->stop();
} }
......
...@@ -12,14 +12,7 @@ void main(void) ...@@ -12,14 +12,7 @@ void main(void)
{ {
vec3 V = normalize(-position); vec3 V = normalize(-position);
if(volume){ if(shaded){
// gl_FragData[0] = vec4(gl_TexCoord[0].xyz,1);
gl_FragData[0] = vec4(texture3D(volumeData,gl_TexCoord[0].xyz).x);
}
else if(shaded){
vec3 N = normalize(normal); vec3 N = normalize(normal);
vec3 L = normalize(gl_LightSource[0].position.xyz-position); vec3 L = normalize(gl_LightSource[0].position.xyz-position);
...@@ -39,10 +32,23 @@ void main(void) ...@@ -39,10 +32,23 @@ void main(void)
vec4 color = (Iamb + Idiff + Ispec ); vec4 color = (Iamb + Idiff + Ispec );
gl_FragData[0] = color; gl_FragData[0] = color;
} } else
else
gl_FragData[0] = vec4(1,1,0,1); gl_FragData[0] = vec4(1,1,0,1);
gl_FragData[1] = pick; //gl_FragData[1] = pick;
//gl_FragData[1] = vec4(abs(normal),1); gl_FragData[1] = vec4(abs(normal),1);
if(volume){
if(gl_FrontFacing){
gl_FragData[2] = gl_TexCoord[0];
// gl_FragData[3] = vec4(0);
} else{
// gl_FragData[2] = vec4(0);
gl_FragData[3] = gl_TexCoord[0];
}
} else {
gl_FragData[2] = vec4(0,0,0,1);
gl_FragData[3] = vec4(0,0,0,1);
}
} }
...@@ -37,6 +37,16 @@ void SceneVolume::draw(QGLShaderProgram *shader){ ...@@ -37,6 +37,16 @@ void SceneVolume::draw(QGLShaderProgram *shader){
shader->setUniformValue("volume", true); shader->setUniformValue("volume", true);
QMatrix4x4 mat = QMatrix4x4();
mat.rotate(getGlobalRotation().conjugate());
glMultMatrixf(mat.data());
// shader->setUniformValue("volumeRot",mat);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
float x = lx/2; float x = lx/2;
float y = ly/2; float y = ly/2;
float z = lz/2; float z = lz/2;
...@@ -113,9 +123,48 @@ void SceneVolume::draw(QGLShaderProgram *shader){ ...@@ -113,9 +123,48 @@ void SceneVolume::draw(QGLShaderProgram *shader){
glVertex3f( x, -y, -z); glVertex3f( x, -y, -z);
glEnd(); glEnd();
shader->setUniformValue("volume", false);
shader->setUniformValue("shaded", false);
glLineWidth(2);
if(true){
glBegin(GL_LINES);
glVertex3f( x, y, -z);
glVertex3f( x, y, z);
glVertex3f( -x, y, -z);
glVertex3f( -x, y, z);
glVertex3f( x, -y, -z);
glVertex3f( x, -y, z);
glVertex3f( -x, -y, -z);
glVertex3f( -x, -y, z);
glVertex3f( x, -y, z);
glVertex3f( x, y, z);
glVertex3f( -x, -y, z);
glVertex3f( -x, y, z);
glVertex3f( x, -y, -z);
glVertex3f( x, y, -z);
glVertex3f( -x, -y, -z);
glVertex3f( -x, y, -z);
glVertex3f( -x, y, z);
glVertex3f( x, y, z);
glVertex3f( -x, -y, z);
glVertex3f( x, -y, z);
glVertex3f( -x, y, -z);
glVertex3f( x, y, -z);
glVertex3f( -x, -y, -z);
glVertex3f( x, -y, -z);
glEnd();
}
shader->setUniformValue("shaded", true);
glEnable(GL_DEPTH_TEST);
glDisable( GL_BLEND );
glPopMatrix(); glPopMatrix();
shader->setUniformValue("volume", false);
} }
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