Commit a31ebbd8 by Kai Westerkamp

basic landscape

parent ee021a83
......@@ -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;
......
......@@ -131,6 +131,71 @@ void Controler::setEditMode(){
viewMode = EDIT;
}
void Controler::addLandscape()
{
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..."),
......
......@@ -28,6 +28,9 @@ public slots:
void addVolume();
void addVolume(QString filePath);
void addLandscape();
void addLandscape(QString filePath);
private:
MainWindow *mainwindow;
Scene *scene;
......
......@@ -14,6 +14,7 @@ GLView::GLView(Scene *scene,Camera * camera,Controler *controler )
isGridEnabled = false;
MIP = false;
afterInit = false;
landscape = false;
}
QSize GLView::minimumSizeHint() const
......@@ -29,6 +30,7 @@ QSize GLView::sizeHint() const
void GLView::initializeGL ( ) {
Q_ASSERT(initializeOpenGLFunctions());
qDebug()<<"OpenGL Version"<<this->format().majorVersion()<<this->format().minorVersion();
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
......@@ -93,6 +95,35 @@ void GLView::initShader()
qCritical()<< "Linking 2 failed:"<<displayShader->log();
}
//landscape
QOpenGLShader *vertex2 = new QOpenGLShader(QOpenGLShader::Vertex);
if(!vertex2->compileSourceFile(QLatin1String(":/landscape.vert")))
qCritical()<< "Vertex Shader landscape failed"<< vertex2->log();
QOpenGLShader *fragment2 = new QOpenGLShader(QOpenGLShader::Fragment);
if(!fragment2->compileSourceFile(QLatin1String(":/landscape.frag")))
qCritical()<< "Fragment Shader landscape failed"<< fragment2->log();
QOpenGLShader *tesselationControl = new QOpenGLShader(QOpenGLShader::TessellationControl);
if(!tesselationControl->compileSourceFile(QLatin1String(":/landscape.tcs")))
qCritical()<< "Tesselation Control landscape failed"<< tesselationControl->log();
QOpenGLShader *tesselationEval = new QOpenGLShader(QOpenGLShader::TessellationEvaluation);
if(!tesselationEval->compileSourceFile(QLatin1String(":/landscape.tes")))
qCritical()<< "Tesselation Eval landscape failed"<< tesselationEval->log();
landscapeShader = new QOpenGLShaderProgram(this);
landscapeShader->addShader(vertex2);
landscapeShader->addShader(fragment2);
landscapeShader->addShader(tesselationControl);
landscapeShader->addShader(tesselationEval);
if(!landscapeShader->link()){
qCritical()<< "Linking landscape failed:"<<landscapeShader->log();
}
}
void GLView::home(){
......@@ -114,7 +145,7 @@ void GLView::paintGL ()
// QGLFunctions functions = QGLFunctions(this->context());
shader->bind();
if(useFBO){
glBindFramebuffer(GL_FRAMEBUFFER,fbo);
GLenum buffers[] = {GL_COLOR_ATTACHMENT0,GL_COLOR_ATTACHMENT1,GL_COLOR_ATTACHMENT2,GL_COLOR_ATTACHMENT3};
......@@ -134,14 +165,30 @@ void GLView::paintGL ()
glLoadIdentity ();
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//set Projection and Camera Rotation
camera->setupCamera(aspect);
if(landscape){
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
shader->setUniformValue("shaded",true);
landscapeShader->bind();
glBindVertexArray(vertexArray);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indices);
glPatchParameteri(GL_PATCH_VERTICES, 4);
glDrawElements(GL_PATCHES, IndexCount, GL_UNSIGNED_INT, 0);
landscapeShader->release();
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
}
//set Projection and Camera Rotation
camera->setupCamera(aspect);
shader->bind();
shader->setUniformValue("shaded",true);
//draw Scene
scene->draw(shader);
......@@ -178,12 +225,7 @@ void GLView::paintGL ()
shader->release();
if(useFBO){
displayShader->bind();
displayShader->setUniformValue("MIP", MIP);
QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*camera->rotation);
displayShader->setUniformValue("NormalMatrix",mat.inverted().transposed());
//
glBindFramebuffer(GL_FRAMEBUFFER,0);
......@@ -195,6 +237,17 @@ void GLView::paintGL ()
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
displayShader->bind();
displayShader->setUniformValue("MIP", MIP);
QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*camera->rotation);
displayShader->setUniformValue("NormalMatrix",mat.inverted().transposed());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,color);
displayShader->setUniformValue("Texture",0);
......@@ -434,13 +487,106 @@ void GLView::loadData(int width, int height, int depth, char* data, int type )
GL_RED,type , data); // Imagedata as ByteBuffer
}
void GLView::setLandscape(int width,int height,int max,boolean shortNeeded,char* data){
glGenTextures(1,&heightmap);
glBindTexture(GL_TEXTURE_2D,heightmap);
// Filtering
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// Wrap
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
int type;
if(shortNeeded){
type = GL_SHORT;
} else
type = GL_BYTE;
glTexImage2D(GL_TEXTURE_2D,0,GL_R8,width,height,0,GL_RED,type,data);
glGenerateMipmap(heightmap);
const int numberOfQuads = 50;
const int numberOfVertecies = numberOfQuads+1;
const float sizeOfQuads = 0.5;
float offset = (numberOfQuads)/2.0 * sizeOfQuads;
const int size = numberOfVertecies*numberOfVertecies * 3;
float vertices[size];
for (int i = 0; i < numberOfVertecies; ++i) {
for (int j = 0; j < numberOfVertecies; ++j) {
int pos = (i*numberOfVertecies+j)*3;
vertices[pos] = j*sizeOfQuads-offset;
vertices[pos+1] = i*sizeOfQuads-offset;
vertices[pos+2] = 0.0;
}
}
// qDebug()<<"--------------------------------------";
// for (int var = 0; var < 51*3; var+=3) {
// qDebug()<<var/3<<vertices[var]<<vertices[var+1]<< vertices[var+2];
// }
IndexCount = numberOfQuads*numberOfQuads * 4;
const int size2 = numberOfQuads*numberOfQuads * 4;
int indicesI[size2];
for (int i = 0; i < numberOfQuads; ++i) {
for (int j = 0; j < numberOfQuads; ++j) {
int index = (i*numberOfQuads+j);
int VertexIndex = (i*numberOfVertecies+j);
indicesI[index*4] = VertexIndex;
indicesI[index*4+1] = VertexIndex+1;
indicesI[index*4+2] = VertexIndex+numberOfVertecies+1;
indicesI[index*4+3] = VertexIndex+numberOfVertecies;
}
}
// qDebug()<<"--------------------------------------";
// for (int var = 50*4*0; var < 50*4*2; var+=4) {
// qDebug()<<var/4<<"-"<<indicesI[var]<<indicesI[var+1]<< indicesI[var+2]<< indicesI[var+3];
// }
// Create the vertexArray:
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
// Create the VBO for positions:
GLuint positions;
GLsizei stride = 3 * sizeof(float);
glGenBuffers(1, &positions);
glBindBuffer(GL_ARRAY_BUFFER, positions);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, 0);
// Create the VBO for indices:
glGenBuffers(1, &indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicesI), indicesI, GL_STATIC_DRAW);
// glDisableVertexAttribArray(0);
// glBindVertexArray(0);
landscape = true;
}
void GLView::loadTransfer(uchar* data )
{
// uchar *test = new uchar[4*256];
// uchar *test = new uchar[4*256];
// for(int i = 0; i< 4*256;i++){
// test[i] = data[i];
// }
// for(int i = 0; i< 4*256;i++){
// test[i] = data[i];
// }
glGenTextures( 1, &transferFunction );
glBindTexture(GL_TEXTURE_1D, transferFunction);
......
......@@ -28,6 +28,7 @@ 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 loadTransfer(uchar* data);
void setMIP(bool mip);
......@@ -47,6 +48,7 @@ private:
bool afterInit;
QGLShaderProgram *shader;
QGLShaderProgram *displayShader;
QOpenGLShaderProgram *landscapeShader;
Scene *scene;
Controler *controler;
......@@ -64,10 +66,19 @@ private:
GLuint startRay;
GLuint stopRay;
GLuint texture3D;
GLuint transferFunction;
bool MIP;
int IndexCount;
bool landscape;
GLuint heightmap;
GLuint vertexArray;
GLuint indices;
void drawGrid();
void initShader();
......
......@@ -49,6 +49,10 @@ DISTFILES += \
phong.frag \
phong.vert \
display.frag \
display.vert
display.vert \
landscape.frag \
landscape.vert \
landscape.tec \
landscape.tes
FORMS +=
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2015-12-18T10:06:19. -->
<!-- Written by QtCreator 3.5.1, 2016-01-02T12:10:50. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -20,5 +20,10 @@
<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>
</qresource>
</RCC>
#version 420
out vec4 FragColor;
//in vec3 gFacetNormal;
//in vec3 gTriDistance;
//in vec3 gPatchDistance;
//in float gPrimitive;
//uniform vec3 LightPosition;
//uniform vec3 DiffuseMaterial;
//uniform vec3 AmbientMaterial;
//float amplify(float d, float scale, float offset)
//{
// d = scale * d + offset;
// d = clamp(d, 0, 1);
// d = 1 - exp2(-2*d*d);
// return d;
//}
void main()
{
// vec3 N = normalize(gFacetNormal);
// vec3 L = LightPosition;
// float df = abs(dot(N, L));
// vec3 color = AmbientMaterial + df * DiffuseMaterial;
// float d1 = min(min(gTriDistance.x, gTriDistance.y), gTriDistance.z);
// float d2 = min(min(gPatchDistance.x, gPatchDistance.y), gPatchDistance.z);
// color = amplify(d1, 40, -0.5) * amplify(d2, 60, -0.5) * color;
FragColor = vec4(1.0,0.0,0.0, 1.0);
}
#version 420
layout(vertices = 4) out;
in vec3 vPosition[];
out vec3 tcPosition[];
void main()
{
tcPosition[gl_InvocationID] = vPosition[gl_InvocationID];
float tesselation = 1.0;
gl_TessLevelInner[0] = tesselation;
gl_TessLevelInner[1] = tesselation;
gl_TessLevelOuter[0]= tesselation;
gl_TessLevelOuter[1]= tesselation;
gl_TessLevelOuter[2]= tesselation;
gl_TessLevelOuter[3]= tesselation;
}
#version 420
layout(quads, equal_spacing, cw) in;
in vec3 tcPosition[];
//out vec3 tePosition;
//out vec3 tePatchDistance;
//uniform mat4 Projection;
//uniform mat4 Modelview;
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;
}
void main()
{
float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
vec3 p0 = tcPosition[0];
vec3 p1 = tcPosition[1];
vec3 p2 = tcPosition[2];
vec3 p3 = tcPosition[3];
//tePatchDistance = gl_TessCoord;
vec3 tePosition = pt_q(p0,p1,p2,p3,u,v);
float Angle = 1.5707;
mat4 RotationMatrix = mat4( cos( Angle ), -sin( Angle ), 0.0, 0.0,
sin( Angle ), cos( Angle ), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 );
gl_Position = RotationMatrix*vec4(tePosition, 1);
}
#version 420
layout(location = 0) in vec4 Position;
out vec3 vPosition;
void main(void)
{
vPosition.xyz = Position.xyz;
}
......@@ -91,6 +91,7 @@ void MainWindow::initToolbar()
toolBar->addSeparator();
toolBar->addAction(camHome);
toolBar->addAction(landscapeAdd);
toolBar->addSeparator();
toolBar->addWidget(showGridButton);
......@@ -198,6 +199,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);
......@@ -207,6 +212,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;
......
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