Commit 82e1ae6d by Kai Westerkamp

FBO

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