Commit 82e1ae6d by Kai Westerkamp

FBO

parent cb240d35
uniform sampler2D Texture; uniform sampler2D Texture;
uniform sampler2D TextureID;
uniform int active;
varying vec4 qt_TexCoord0; varying vec4 qt_TexCoord0;
void main(void) void main(void)
{ {
gl_FragColor = texture2D(Texture, qt_TexCoord0.st); gl_FragColor = texture2D(Texture,gl_TexCoord[0].st);
//gl_FragColor = texture2D(TextureID,gl_TexCoord[0].st);
//gl_FragColor = vec4(active);
// gl_FragColor = vec4(gl_TexCoord[0].st,0,1);
} }
attribute vec4 qt_Vertex; attribute vec4 gl_Vertex;
attribute vec4 qt_MultiTexCoord0; uniform mat4 gl_ModelViewProjectionMatrix;
uniform mat4 qt_ModelViewProjectionMatrix;
varying vec4 qt_TexCoord0;
void main(void) void main(void)
{ {
gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
qt_TexCoord0 = qt_MultiTexCoord0; gl_TexCoord[0] = gl_MultiTexCoord0;
} }
...@@ -53,13 +53,15 @@ void GLView::initShader() ...@@ -53,13 +53,15 @@ void GLView::initShader()
shader = new QGLShaderProgram(this); shader = new QGLShaderProgram(this);
shader->addShader(vertex); shader->addShader(vertex);
shader->addShader(fragment); shader->addShader(fragment);
shader->bindAttributeLocation("pickID",4);
if(!shader->link()) if(!shader->link())
qCritical()<< "Linking failed"<<shader->log(); qCritical()<< "Linking failed"<<shader->log();
vertex = new QGLShader(QGLShader::Vertex); vertex = new QGLShader(QGLShader::Vertex);
if(!vertex->compileSourceFile(QLatin1String(":/display.vert"))) if(!vertex->compileSourceFile(QLatin1String(":/display.vert")))
qCritical()<< "Vertex Shader 2failed"<< vertex->log(); qCritical()<< "Vertex Shader 2 failed"<< vertex->log();
fragment = new QGLShader(QGLShader::Fragment); fragment = new QGLShader(QGLShader::Fragment);
if(!fragment->compileSourceFile(QLatin1String(":/display.frag"))) if(!fragment->compileSourceFile(QLatin1String(":/display.frag")))
...@@ -81,10 +83,12 @@ void GLView::home(){ ...@@ -81,10 +83,12 @@ void GLView::home(){
void GLView::paintGL () void GLView::paintGL ()
{ {
bool useFBO = true;
QGLFunctions functions = QGLFunctions(this->context()); QGLFunctions functions = QGLFunctions(this->context());
shader->bind(); shader->bind();
// functions.glBindFramebuffer(GL_FRAMEBUFFER,fbo); if(useFBO)
functions.glBindFramebuffer(GL_FRAMEBUFFER,fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
...@@ -99,12 +103,15 @@ void GLView::paintGL () ...@@ -99,12 +103,15 @@ void GLView::paintGL ()
shader->setUniformValue("shaded",true); shader->setUniformValue("shaded",true);
//qDebug()<<"IsLinked"<<shader->isLinked();
//set Projection and Camera Rotation //set Projection and Camera Rotation
camera->setupCamera(aspect); camera->setupCamera(aspect);
//draw Scene //draw Scene
scene->draw(); scene->draw(shader);
shader->setAttributeValue(3,0);
if(isGridEnabled){ if(isGridEnabled){
drawGrid(); drawGrid();
...@@ -135,27 +142,45 @@ void GLView::paintGL () ...@@ -135,27 +142,45 @@ void GLView::paintGL ()
} }
shader->release(); shader->release();
if(useFBO){
displayShader->bind();
//
functions.glBindFramebuffer(GL_FRAMEBUFFER,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,this->width(),this->height());
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
functions.glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,color);
displayShader->setUniformValue("Texture",0);
// displayShader->bind(); functions.glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,picID);
displayShader->setUniformValue("TextureID",1);
// functions.glBindFramebuffer(GL_FRAMEBUFFER,fbo); displayShader->setUniformValue("active",scene->getActive()->getID());
// glViewport(0,0,this->width(),this->height());
// glMatrixMode (GL_MODELVIEW); glBegin (GL_QUADS);
// glLoadIdentity (); glTexCoord2d(0,0);
// glMatrixMode (GL_PROJECTION); glVertex3i (-1, -1, 0);
// glLoadIdentity (); glTexCoord2d(1,0);
glVertex3i (1, -1, 0);
glTexCoord2d(1,1);
glVertex3i (1, 1, 0);
glTexCoord2d(0,1);
glVertex3i (-1, 1, 0);
glEnd ();
// displayShader->setUniformValue("Texture",color);
// glBegin (GL_LINE_LOOP);
// glVertex3i (-1, -1, 0);
// glVertex3i (1, -1, 0);
// glVertex3i (1, 1, 0);
// glVertex3i (-1, 1, 0);
// glEnd ();
displayShader->release();
// displayShader->release(); }
} }
...@@ -260,7 +285,6 @@ void GLView::drawGrid() ...@@ -260,7 +285,6 @@ void GLView::drawGrid()
void GLView::resizeGL(int width , int height ) void GLView::resizeGL(int width , int height )
{ {
aspect = 1.0*width/height; aspect = 1.0*width/height;
qDebug()<<"Resize:"<<width<<height;
QGLFunctions functions = QGLFunctions(this->context()); QGLFunctions functions = QGLFunctions(this->context());
...@@ -278,6 +302,7 @@ void GLView::resizeGL(int width , int height ) ...@@ -278,6 +302,7 @@ void GLView::resizeGL(int width , int height )
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create the color buffer // Create the color buffer
glGenTextures(1, &picID); glGenTextures(1, &picID);
glBindTexture(GL_TEXTURE_2D, picID); glBindTexture(GL_TEXTURE_2D, picID);
...@@ -289,17 +314,13 @@ void GLView::resizeGL(int width , int height ) ...@@ -289,17 +314,13 @@ void GLView::resizeGL(int width , int height )
// Create the depth buffer // Create the depth buffer
glGenTextures(1, &depth); functions.glGenRenderbuffers(1, &depth);
glBindTexture(GL_TEXTURE_2D, depth); functions.glBindRenderbuffer(GL_RENDERBUFFER, depth);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); functions.glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT,width,height);
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_DEPTH_COMPONENT32, width, height, 0, GL_RED, GL_BYTE, NULL);
functions.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,color,0); functions.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,color,0);
functions.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT1,GL_TEXTURE_2D,picID,0); functions.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT1,GL_TEXTURE_2D,picID,0);
functions.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,depth,0); functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depth);
GLenum err = functions.glCheckFramebufferStatus(GL_FRAMEBUFFER); GLenum err = functions.glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(err == GL_FRAMEBUFFER_COMPLETE){ if(err == GL_FRAMEBUFFER_COMPLETE){
......
uniform bool shaded; uniform bool shaded;
varying vec4 pick;
varying vec3 position; varying vec3 position;
varying vec3 normal; varying vec3 normal;
...@@ -34,5 +36,6 @@ void main(void) ...@@ -34,5 +36,6 @@ void main(void)
else else
gl_FragData[0] = vec4(1,1,0,1); gl_FragData[0] = vec4(1,1,0,1);
gl_FragData[1] = vec4(normal,1); gl_FragData[1] = pick;
gl_FragData[1] = vec4(abs(normal),1);
} }
attribute vec4 gl_Vertex; attribute vec4 gl_Vertex;
attribute vec3 gl_Normal; attribute vec3 gl_Normal;
attribute int pickID;
uniform mat4 gl_ModelViewProjectionMatrix; uniform mat4 gl_ModelViewProjectionMatrix;
uniform mat4 gl_ModelViewMatrix; uniform mat4 gl_ModelViewMatrix;
...@@ -8,12 +10,16 @@ uniform mat3 gl_NormalMatrix; ...@@ -8,12 +10,16 @@ uniform mat3 gl_NormalMatrix;
varying vec3 position; varying vec3 position;
varying vec3 normal; varying vec3 normal;
varying vec4 pick;
void main(void) void main(void)
{ {
normal = normalize(gl_NormalMatrix * gl_Normal); normal = normalize(gl_NormalMatrix * gl_Normal);
position = vec3(gl_ModelViewMatrix *gl_Vertex); position = vec3(gl_ModelViewMatrix *gl_Vertex);
pick = vec4(pickID);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
} }
...@@ -154,9 +154,9 @@ SceneObject *Scene::getActive(){ ...@@ -154,9 +154,9 @@ SceneObject *Scene::getActive(){
return active; return active;
} }
void Scene::draw() void Scene::draw(QGLShaderProgram *shader)
{ {
root->draw(); root->draw(shader);
} }
SceneNode *Scene::getRoot() SceneNode *Scene::getRoot()
......
...@@ -16,7 +16,7 @@ class Scene: public QAbstractItemModel // QAbstractItemModel ...@@ -16,7 +16,7 @@ class Scene: public QAbstractItemModel // QAbstractItemModel
public: public:
Scene(); Scene();
~Scene(); ~Scene();
void draw(); void draw(QGLShaderProgram *shader);
int simpleScene(); int simpleScene();
......
#include "scenenode.h" #include "scenenode.h"
SceneNode::SceneNode() SceneNode::SceneNode()
:SceneObject() :SceneObject()
{ {
childs = QList<SceneObject*>(); childs = QList<SceneObject*>();
} }
SceneNode::~SceneNode() SceneNode::~SceneNode()
...@@ -48,12 +50,12 @@ SceneObject *SceneNode::find(int id) ...@@ -48,12 +50,12 @@ SceneObject *SceneNode::find(int id)
} }
void SceneNode::draw() void SceneNode::draw(QGLShaderProgram *shader)
{ {
glPushMatrix(); glPushMatrix();
applyTransformation(); applyTransformation();
foreach (SceneObject *obj, childs) { foreach (SceneObject *obj, childs) {
obj->draw(); obj->draw(shader);
} }
glPopMatrix(); glPopMatrix();
......
...@@ -21,7 +21,7 @@ public: ...@@ -21,7 +21,7 @@ public:
virtual bool isNode(){return true;qDebug()<<"isNode";} virtual bool isNode(){return true;qDebug()<<"isNode";}
SceneObject *find(int id); SceneObject *find(int id);
void draw(); void draw(QGLShaderProgram *shader);
void add(SceneObject *child); void add(SceneObject *child);
void remove(int number); void remove(int number);
......
#include "sceneobject.h" #include "sceneobject.h"
#include "scenenode.h" #include "scenenode.h"
int SceneNode::idCount = 1;
SceneObject::SceneObject() SceneObject::SceneObject()
{ {
id = 3; id = idCount;
idCount++;
name = QString("Scene Object "+id); name = QString("Scene Object "+id);
rotation = QQuaternion(); rotation = QQuaternion();
translation = QVector3D(); translation = QVector3D();
...@@ -88,7 +92,7 @@ void SceneObject::rotate(QQuaternion rot) ...@@ -88,7 +92,7 @@ void SceneObject::rotate(QQuaternion rot)
} }
void SceneObject::draw(){qDebug()<<"Drawing abstract Scene Element";} void SceneObject::draw(QGLShaderProgram *shader){qDebug()<<"Drawing abstract Scene Element";}
SceneObject *SceneObject::find(int id) SceneObject *SceneObject::find(int id)
{ {
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
class SceneObject : public QObject class SceneObject : public QObject
{ {
Q_OBJECT Q_OBJECT
static int idCount;
protected: protected:
QQuaternion rotation; QQuaternion rotation;
QVector3D translation; QVector3D translation;
...@@ -37,7 +39,7 @@ public: ...@@ -37,7 +39,7 @@ public:
QVariant data(int column) const; QVariant data(int column) const;
bool setData(int column, const QVariant &value); bool setData(int column, const QVariant &value);
virtual void draw(); virtual void draw(QGLShaderProgram *shader);
virtual SceneObject *find(int id); virtual SceneObject *find(int id);
int getID(); int getID();
QString getName(); QString getName();
......
...@@ -56,7 +56,8 @@ ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation) ...@@ -56,7 +56,8 @@ ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation)
// qDebug()<<"Tesselation"<<this->tesselation<<getName(); // qDebug()<<"Tesselation"<<this->tesselation<<getName();
} }
void ScenePrimitive::draw(){ void ScenePrimitive::draw(QGLShaderProgram *shader){
shader->setAttributeValue(4,getID());
glPushMatrix(); glPushMatrix();
applyTransformation(); applyTransformation();
// qDebug()<<this->getName()<<"Matrial"<<*color<<"Trans"<<translation<<"Rot"<<rotation; // qDebug()<<this->getName()<<"Matrial"<<*color<<"Trans"<<translation<<"Rot"<<rotation;
......
...@@ -26,7 +26,7 @@ private: ...@@ -26,7 +26,7 @@ private:
public: public:
void draw(); void draw(QGLShaderProgram *shader);
void setMaterial(float *color); void setMaterial(float *color);
......
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