Commit 5de04035 by Kai Westerkamp

Merge branch 'master' of ssh://git.breab.org:2223/kai/GraPa

# Conflicts: # A4/glview.cpp # A4/glview.h # A4/hellocube.pro.user
parents 03b4c0ce 227f8ace
......@@ -4,3 +4,6 @@
/build-hellocube-Desktop_Qt_5_5_1_MinGW_32bit-Debug
/build-hellocube-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug
/A2/*.user
build-hellocube-Desktop_Qt_5_4_2_MinGW_32bit2-Debug
build-hellocube-Desktop_Qt_5_4_2_MSVC2013_OpenGL_64bit-Debug
build-hellocube-Desktop_Qt_5_4_2_MSVC2013_OpenGL_64bit-Release
File deleted
......@@ -63,6 +63,8 @@ void Camera::setupCamera(GLdouble aspect )
}
void Camera::perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;
......
......@@ -12,7 +12,7 @@ class Camera: public QObject
{
Q_OBJECT
private:
bool persp;
// QQuaternion *rotation;
// QVector3D *translation;
......@@ -25,6 +25,7 @@ public slots:
void home();
public:
bool persp;
QQuaternion *rotation;
QVector3D *translation;
......
......@@ -131,6 +131,94 @@ void Controler::setEditMode(){
viewMode = EDIT;
}
QImage* Controler::loadImage(QString Text)
{
QString fn = QFileDialog::getOpenFileName(NULL, Text,
QString("D:\\Projekte\\GraPa\\A4"), tr("Texture (*.jpg )"));
if(fn.isEmpty())
return NULL;
qDebug()<<"Opening Texture:"<<fn;
return new QImage(fn, "jpg");
}
void Controler::addLandscape()
{
QImage *texture = loadImage("First Texture");
// for(int i = 0; i < 4; i++)
mainwindow->getViews()[0]->setLandscapeTexture(1,texture);
texture = loadImage("Second Texture");
// for(int i = 0; i < 4; i++)
mainwindow->getViews()[0]->setLandscapeTexture(2,texture);
QString fn = QFileDialog::getOpenFileName(NULL, tr("Open Volume..."),
QString("D:\\Projekte\\GraPa\\A4"), tr("Portable Grayscale Map (*.pgm )"));
if(fn.isEmpty())
return;
qDebug()<<"Opening File:"<<fn;
addLandscape(fn);
}
void Controler::addLandscape(QString filePath)
{
QFile file(filePath);
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}
QByteArray line1 = file.readLine();
QTextStream l1(line1);
if(!line1.contains("P5")){
qDebug("Not a Landscape");
return;
}
QByteArray line2 = file.readLine();
QTextStream l2(line2);
QString w = l2.readLine();
int width = w.toInt();
QByteArray line3 = file.readLine();
QTextStream l3(line3);
QString h = l3.readLine();
int height = h.toInt();
QByteArray line4 = file.readLine();
QTextStream l4(line4);
QString m = l4.readLine();
int max = m.toInt();
qDebug()<<width<<height<<max;
QByteArray rawdata = file.readAll();
boolean shortNeeded = max > 256;
char *data;
if(shortNeeded){
data = new char[width*height*2];
for (int var = 0; var < width*height*2; var+=2) {
data[var] = rawdata.at(var+1);
data[var+1] = rawdata.at(var);
}
} else
data = rawdata.data();
for(int i = 0; i < 4; i++)
mainwindow->getViews()[i]->setLandscape(width,height,max,shortNeeded,data);
}
void Controler::addVolume()
{
QString fn = QFileDialog::getOpenFileName(NULL, tr("Open Volume..."),
......@@ -239,12 +327,12 @@ void Controler::addVolume(QString filePath){
trans[index+3] = i;
}
for(int i = 0; i < 4; i++){
// mainwindow->getViews()[i]->loadTransfer(trans);
}
for(int i = 0; i < 4; i++){
// mainwindow->getViews()[i]->loadTransfer(trans);
}
this->mainwindow->transEditor->setHistogram(histogram);
// this->mainwindow->transEditor->setTransferFunction(trans);
this->mainwindow->transEditor->setHistogram(histogram);
// this->mainwindow->transEditor->setTransferFunction(trans);
......
......@@ -3,6 +3,7 @@
#include <QObject>
#include <QtGui>
#include <QImage>
class camera;
class GLView;
......@@ -28,6 +29,11 @@ public slots:
void addVolume();
void addVolume(QString filePath);
void addLandscape();
void addLandscape(QString filePath);
QImage *loadImage(QString Text);
private:
MainWindow *mainwindow;
Scene *scene;
......
......@@ -10,6 +10,7 @@
#include <gl/GLU.h>
#include <QGLFunctions>
#include <QOpenGLFunctions_4_0_Core>
#include <math.h>
class GLView : public QGLWidget, public QOpenGLFunctions_4_0_Core
{
......@@ -21,6 +22,8 @@ protected :
void mousePressEvent(QMouseEvent *event ) ;
void mouseMoveEvent(QMouseEvent *event ) ;
void wheelEvent(QWheelEvent *event ) ;
void keyPressEvent(QKeyEvent* event);
public slots:
void home();
......@@ -28,9 +31,12 @@ public slots:
void setGridStepSize(int size){gridStepSize = size;}
void showGrid(bool bo){isGridEnabled = bo;}
void loadData(int width, int height, int depth, char* data, int type);
void setLandscape(int width,int height,int max,boolean shortNeeded,char* data);
void setLandscapeTexture(int number, QImage *texture);
void loadTransfer(uchar* data);
void setMIP(bool mip);
public:
GLView(Scene *scene,Camera * camera,Controler *controler,QGLFormat format );
void setHome(QQuaternion *rotation, QVector3D *translation);
......@@ -47,6 +53,7 @@ private:
bool afterInit;
QGLShaderProgram *shader;
QGLShaderProgram *displayShader;
QOpenGLShaderProgram *landscapeShader;
Scene *scene;
Controler *controler;
......@@ -64,9 +71,22 @@ private:
GLuint startRay;
GLuint stopRay;
GLuint texture3D;
GLuint transferFunction;
bool MIP;
bool MIP;
bool wireframe;
int IndexCount;
bool landscape;
GLuint heightmap;
GLuint vertexArray;
GLuint indices;
GLuint landscapeTexture1;
GLuint landscapeTexture2;
void drawGrid();
......
......@@ -51,6 +51,11 @@ DISTFILES += \
phong.frag \
phong.vert \
display.frag \
display.vert
display.vert \
landscape.frag \
landscape.vert \
landscape.tec \
landscape.tes \
landscape.geo
FORMS +=
......@@ -20,5 +20,11 @@
<file>img/delete.png</file>
<file>display.frag</file>
<file>display.vert</file>
<file>img/land.png</file>
<file>landscape.frag</file>
<file>landscape.vert</file>
<file>landscape.tcs</file>
<file>landscape.tes</file>
<file>landscape.geo</file>
</qresource>
</RCC>
#version 420
out vec4 FragColor;
in vec3 gPosition;
in vec2 gUV;
in vec3 gTriDistance;
in vec2 gPatchDistance;
in float tesselationLevel;
uniform sampler2D heightmap;
uniform sampler2D landTex1;
uniform sampler2D landTex2;
uniform bool wireframe;
uniform float heightFactor;
void main()
{
//
vec2 textureUV = gUV*8;
vec3 col1 = texture2D(landTex1,textureUV).xyz;
vec3 col2 = texture2D(landTex2,textureUV).xyz;
float height = clamp(gPosition.y/heightFactor*15,0.0,1.0);
float alpha = smoothstep(0.5f,0.7f,height);
FragColor = +vec4((1-alpha)*col2 + alpha *col1,1.0);
// FragColor = clamp(vec4(vec3(textureLod(heightmap,gUV,5-tesselationLevel).x), 1.0)*10,0,1);
//FragColor = vec4(gUV,0.0,1.0);
//FragColor = vec4(height);
if(wireframe == true){
float d = min(gTriDistance.x,min(gTriDistance.y,gTriDistance.z));
if(d < 0.05){
FragColor = vec4(0.0,1.0,0.0,1.0);
}
float d2 = min(gPatchDistance.x,min(gPatchDistance.y,1-max(gPatchDistance.x,gPatchDistance.y)));
if(d2 < 0.01){
FragColor = vec4(0.0,0.0,1.0,1.0);
}
}
}
#version 420
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
in vec2 teUV[3];
out vec2 gUV;
in vec3 tePosition[3];
out vec3 gPosition;
out vec3 gTriDistance;
in vec2 tePatchDistance[3];
out vec2 gPatchDistance;
void main(void)
{
gTriDistance = vec3(1, 0, 0);
gUV = teUV[0];
gPosition = tePosition[0];
gPatchDistance = tePatchDistance[0];
gl_Position = gl_in[0].gl_Position; EmitVertex();
gTriDistance = vec3(0, 1, 0);
gUV = teUV[1];
gPosition = tePosition[1];
gPatchDistance = tePatchDistance[1];
gl_Position = gl_in[1].gl_Position; EmitVertex();
gTriDistance = vec3(0, 0, 1);
gUV = teUV[2];
gPosition = tePosition[2];
gPatchDistance = tePatchDistance[2];
gl_Position = gl_in[2].gl_Position; EmitVertex();
EndPrimitive();
}
#version 420
layout(vertices = 4) out;
in vec3 vPosition[];
out vec3 tcPosition[];
in vec2 vUV[];
out vec2 tcUV[];
uniform vec3 CamPos;
float calcTessellationLevel( const in float v1, const in float v2 )
{
float maxdist = min(v1,v2)/8;
float tesselationFactor = 5/(maxdist*maxdist);
return pow(min(tesselationFactor,5),2);
}
float computeDistance(in vec3 a)
{
return length(vec3(0.0,-CamPos.y,0.0)-a);
}
void main()
{
tcPosition[gl_InvocationID] = vPosition[gl_InvocationID];
tcUV[gl_InvocationID] = vUV[gl_InvocationID];
float tesselation = 1.0;
float distances[4];
distances[0] = computeDistance(vPosition[0]);
distances[1] = computeDistance(vPosition[1]);
distances[2] = computeDistance(vPosition[2]);
distances[3] = computeDistance(vPosition[3]);
gl_TessLevelOuter[0] = calcTessellationLevel( distances[0], distances[3] );
gl_TessLevelOuter[2] = calcTessellationLevel( distances[1], distances[2] );
gl_TessLevelOuter[1] = calcTessellationLevel( distances[0], distances[1] );
gl_TessLevelOuter[3] = calcTessellationLevel( distances[2], distances[3] );
float tess = max( gl_TessLevelOuter[1], gl_TessLevelOuter[2] );
tess = max(tess, gl_TessLevelOuter[3]);
tess = max(tess, gl_TessLevelOuter[0]);
gl_TessLevelInner[0] = tess;
gl_TessLevelInner[1] = tess;
}
#version 420 compatibility
layout(quads, equal_spacing, cw) in;
in vec3 tcPosition[];
out vec3 tePosition;
in vec2 tcUV[];
out vec2 teUV;
uniform sampler2D heightmap;
uniform mat4x4 CamOffset;
uniform float heightFactor;
uniform vec2 scaleUV;
uniform vec2 offsetUV;
out vec2 tePatchDistance;
out float tesselationLevel;
vec3 pt_q(in vec3 p0,in vec3 p1,in vec3 p2,in vec3 p3,in float u,in float v){
return (1.0-u)*(1.0-v)*p0+u*(1.0-v)*p1+(1.0-u)*v*p3+u*v*p2;
}
vec2 pt_uv(in vec2 p0,in vec2 p1,in vec2 p2,in vec2 p3,in float u,in float v){
return (1.0-u)*(1.0-v)*p0+u*(1.0-v)*p1+(1.0-u)*v*p3+u*v*p2;
}
void main()
{
float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
tePatchDistance = gl_TessCoord.xy;
vec3 p0 = tcPosition[0];
vec3 p1 = tcPosition[1];
vec3 p2 = tcPosition[2];
vec3 p3 = tcPosition[3];
float tess = gl_TessLevelInner[0];
teUV = pt_uv(tcUV[0],tcUV[1],tcUV[2],tcUV[3],u,v);
teUV = scaleUV * teUV + offsetUV;
tesselationLevel = gl_TessLevelOuter[0];
float height = textureLod(heightmap,teUV,5-tesselationLevel).x*heightFactor;
//tePatchDistance = gl_TessCoord;
tePosition = pt_q(p0,p1,p2,p3,u,v);
tePosition.y = height;
tePosition = vec3(CamOffset*vec4(tePosition,1));
gl_Position = gl_ModelViewProjectionMatrix*vec4(tePosition, 1);
}
#version 420
layout(location = 0) in vec4 Position;
out vec3 vPosition;
out vec2 vUV;
void main(void)
{
vPosition.xyz = Position.xyz;
vUV = (Position.xz / 50)+0.5;
}
......@@ -95,6 +95,7 @@ void MainWindow::initToolbar()
toolBar->addSeparator();
toolBar->addAction(camHome);
toolBar->addAction(landscapeAdd);
toolBar->addSeparator();
toolBar->addWidget(showGridButton);
......@@ -202,6 +203,10 @@ void MainWindow::initPrimitivesMenu()
//activeDelete->setIcon(QIcon(":/img/delete.png"));
connect(volumeAdd, SIGNAL(triggered()), controler, SLOT(addVolume()));
landscapeAdd = new QAction("Landscape",this);
landscapeAdd->setIcon(QIcon(":/img/land.png"));
connect(landscapeAdd, SIGNAL(triggered()), controler, SLOT(addLandscape()));
primitivesMenu->addAction(spheresAdd);
primitivesMenu->addAction(boxesAdd);
......@@ -211,6 +216,7 @@ void MainWindow::initPrimitivesMenu()
primitivesMenu->addAction(groupAdd);
primitivesMenu->addAction(activeDelete);
primitivesMenu->addAction(volumeAdd);
primitivesMenu->addAction(landscapeAdd);
}
MainWindow::MainWindow(QWidget *parent)
......
......@@ -54,6 +54,7 @@ private:
QAction *groupAdd;
QAction *activeDelete;
QAction *volumeAdd;
QAction *landscapeAdd;
QToolBar *toolBar;
......
File deleted
#-------------------------------------------------
#
# Project created by QtCreator 2016-01-14T19:42:18
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Animation
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mainwidget.cpp
HEADERS += mainwindow.h \
mainwidget.h
FORMS +=
DISTFILES += \
animate.vert \
animate.frag
RESOURCES += \
resources.qrc
#version 330
layout(location = 0) out vec3 color;
in vec3 vPosition;
void main(void)
{
color = vec3(1.0,0.0,0.0);
}
#version 330
layout(location = 0) in vec3 Position;
out vec3 vPosition;
uniform mat4x4 VP;
void main(void)
{
vPosition = Position;
gl_Position = VP * vec4(Position,1.0);
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwidget.h"
MainWidget::MainWidget()
{
}
QSize MainWidget::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize MainWidget::sizeHint() const
{
return QSize(1280, 720);
}
void MainWidget::initializeGL(){
bool glFunctionsOK = initializeOpenGLFunctions();
Q_ASSERT(glFunctionsOK);
if(!glFunctionsOK)
exit(4);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
// Shader
animationShader = initShader(QLatin1String(":/animate.frag"),QLatin1String(":/animate.vert"));
m_view = QMatrix4x4();
m_view.lookAt(QVector3D(2.0,2.0,4.0),QVector3D(0.0,0.0,0.0),QVector3D(0.0,1.0,0.0));
//temp
static const GLfloat g_vertex_buffer_data[] = {
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f
};
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
}
QOpenGLShaderProgram* MainWidget::initShader(QString fragSource, QString vertSource){
QOpenGLShader *vert = new QOpenGLShader(QOpenGLShader::Vertex);
if(!vert->compileSourceFile(vertSource)){
qCritical()<< "Fragment Shader"<<vertSource<<"failed"<< vert->log();
exit(5);
}
QOpenGLShader *frag = new QOpenGLShader(QOpenGLShader::Fragment);
if(!frag->compileSourceFile(fragSource)){
qCritical()<< "Fragment Shader"<<fragSource<<"failed"<< frag->log();
exit(5);
}
QOpenGLShaderProgram* shader = new QOpenGLShaderProgram();
shader->addShader(vert);
shader->addShader(frag);
if(!shader->link()){
qCritical()<< "Linking shader failed:"<<shader->log();
exit(5);
}
return shader;
}
void MainWidget::paintGL(){
glViewport(0,0,width(),height());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
animationShader->bind();
QMatrix4x4 VP = m_projection*m_view;
//QMatrix4x4 VP = m_view*m_projection;
animationShader->setUniformValue("VP",VP);
glEnableVertexAttribArray(positionIndex);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, 0,(void*)0 );
glDrawArrays(GL_TRIANGLES, 0, 12*3);
glDisableVertexAttribArray(positionIndex);
animationShader->release();
}
void MainWidget::resizeGL(int width, int height){
qDebug()<<"Resize"<<width<<height;
m_projection = QMatrix4x4();
m_projection.perspective(45.0,1.0*width/height,0.01,100.0);
}
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QObject>
#include <QtOpenGL>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
class MainWidget : public QOpenGLWidget, public QOpenGLFunctions_3_3_Core
{
Q_OBJECT
public:
MainWidget();
QSize minimumSizeHint() const;
QSize sizeHint() const;
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
private:
static const int positionIndex = 0;
static const int normalIndex = 1;
static const int uvIndex = 2;
static const int boneIndex = 3;
static const int boneweightIndex = 4;
QOpenGLShaderProgram* animationShader;
QMatrix4x4 m_projection;
QMatrix4x4 m_view;
GLuint vertexbuffer;
QOpenGLShaderProgram* initShader(QString fragSource, QString vertSource);
};
#endif // MAINWIDGET_H
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
m_centralWidget = new MainWidget();
this->setCentralWidget(m_centralWidget);
// showMaximized();
}
MainWindow::~MainWindow()
{
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <mainwidget.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
MainWidget *m_centralWidget;
QMatrix4x4 m_projection;
};
#endif // MAINWINDOW_H
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
<RCC>
<qresource prefix="/">
<file>animate.frag</file>
<file>animate.vert</file>
</qresource>
</RCC>
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