Commit a99eeaa6 by Kai Westerkamp

A5

parent eb660cbd
......@@ -24,15 +24,20 @@ void GLView::keyPressEvent(QKeyEvent* event){
switch (event->key()) {
case Qt::Key_W:
camera->move(QVector3D(0.0,0.0,stepsize));
break;
case Qt::Key_S:
camera->move(QVector3D(0.0,0.0,-stepsize));
break;
case Qt::Key_A:
camera->move(QVector3D(stepsize,0.0,0.0));
break;
case Qt::Key_D:
camera->move(QVector3D(-stepsize,0.0,0.0));
break;
case Qt::SHIFT:
camera->move(QVector3D(0.0,stepsize,0.0));
break;
default: QWidget::keyPressEvent(event);
};
updateGL();
......@@ -197,12 +202,18 @@ void GLView::paintGL ()
if(landscape){
if(MIP)
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
// if(MIP)
// glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
glLineWidth(1);
float heightScale = 200;
float horizontalScale = 200;
landscapeShader->bind();
landscapeShader->setUniformValue("wireframe",MIP);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,heightmap);
landscapeShader->setUniformValue("HeightMap",0);
......@@ -215,14 +226,46 @@ void GLView::paintGL ()
glBindTexture(GL_TEXTURE_2D,landscapeTexture2);
landscapeShader->setUniformValue("landTex2",2);
landscapeShader->setUniformValue("heightScale",5);
landscapeShader->setUniformValue("horizontalScale",10);
landscapeShader->setUniformValue("heightFactor",heightScale);
//Offset
QVector3D cam = QVector3D()-*camera->translation;
cam = camera->rotation->conjugate().rotatedVector(cam);
cam = camera->rotation->conjugate().rotatedVector(cam); // Cam World Pos
landscapeShader->setUniformValue("CamPos",cam);
QVector3D offset = QVector3D(cam);
offset.setX(ceilf(offset.x()));
offset.setY(0.0);
offset.setZ(ceilf(offset.z()));
QMatrix4x4 offsetM;
offsetM.translate(offset);
landscapeShader->setUniformValue("CamOffset",offsetM);
bool all = false;
//UV
QVector2D offsetUV;
offsetUV.setX(offset.x()/horizontalScale+0.5);
offsetUV.setY(offset.z()/horizontalScale+0.5);
if(all){
offsetUV.setX(0.0);
offsetUV.setY(0.0);
}
QVector2D scaleUV;
scaleUV.setX( 50/horizontalScale);
scaleUV.setY( 50/horizontalScale);
if(all){
scaleUV.setX( 1.0);
scaleUV.setY( 1.0);
}
landscapeShader->setUniformValue("offsetUV", offsetUV);
landscapeShader->setUniformValue("scaleUV", scaleUV);
glBindVertexArray(vertexArray);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indices);
......@@ -232,8 +275,8 @@ void GLView::paintGL ()
landscapeShader->release();
if(MIP)
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
// if(MIP)
// glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
}
......@@ -548,7 +591,6 @@ void GLView::setLandscapeTexture(int number, QImage* texture){
if(number == 1){
glGenTextures( 1, &landscapeTexture1 );
glBindTexture( GL_TEXTURE_2D, landscapeTexture1 );
qDebug()<<"Load";
} else if(number == 2){
glGenTextures( 1, &landscapeTexture2 );
......@@ -664,6 +706,7 @@ void GLView::setLandscape(int width,int height,int max,boolean shortNeeded,char*
// glBindVertexArray(0);
landscape = true;
updateGL();
}
......
......@@ -10,6 +10,7 @@
#include <gl/GLU.h>
#include <QGLFunctions>
#include <QOpenGLFunctions_4_3_Core>
#include <math.h>
class GLView : public QGLWidget, public QOpenGLFunctions_4_3_Core
{
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2016-01-04T16:24:23. -->
<!-- Written by QtCreator 3.5.1, 2016-01-17T17:25:28. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -4,12 +4,16 @@ 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()
{
......@@ -19,27 +23,25 @@ void main()
vec3 col1 = texture2D(landTex1,textureUV).xyz;
vec3 col2 = texture2D(landTex2,textureUV).xyz;
float height = gPosition.y/5;
float alpha = smoothstep(0.4f,0.6f,height);
float height = clamp(gPosition.y/heightFactor*15,0.0,1.0);
float alpha = smoothstep(0.5f,0.7f,height);
//float alpha = clamp(height,0.0,1.0);
float d = min(gTriDistance[0],min(gTriDistance.y,gTriDistance.z));
float edgeIntensity = exp2(-1.0*d*d);
FragColor = +vec4((1-alpha)*col2 + alpha *col1,1.0);
if(d < 0.05){
FragColor = edgeIntensity*vec4(0.0,1.0,0.0,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(alpha);
//FragColor = vec4(col1,1.0);
//FragColor = clamp(vec4(vec3(textureLod(heightmap,teUV,0).x), 1.0)*10,0,1);
//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);
}
}
}
......@@ -11,6 +11,9 @@ out vec3 gPosition;
out vec3 gTriDistance;
in vec2 tePatchDistance[3];
out vec2 gPatchDistance;
void main(void)
{
......@@ -18,16 +21,19 @@ 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();
......
......@@ -8,7 +8,15 @@ in vec2 tcUV[];
out vec2 teUV;
uniform sampler2D heightmap;
uniform vec3 CamPos;
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;
......@@ -23,6 +31,8 @@ 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];
......@@ -32,15 +42,17 @@ void main()
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,0).x*50;
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 =tePosition+ vec3(CamPos.x,0.0,CamPos.z);
tePosition = vec3(CamOffset*vec4(tePosition,1));
gl_Position = gl_ModelViewProjectionMatrix*vec4(tePosition, 1);
......
#-------------------------------------------------
#
# 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 vec4 Position;
out vec3 vPosition;
uniform mat4x4 VP;
void main(void)
{
vPosition = Position.xyz;
gl_Position = VP * Position;
}
#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(0.0,1.0,3.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(){
qDebug()<<"Paint";
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("MVP",VP);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
glDisableVertexAttribArray(0);
animationShader->release();
}
void MainWidget::resizeGL(int width, int height){
qDebug()<<"Resize"<<width<<height;
m_projection = QMatrix4x4();
m_projection.perspective(45.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:
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