Commit a23de70f by Kai Westerkamp

added Tesselation

Phong shading not working
parent 9e817f2e
Pipeline #82 skipped
......@@ -28,8 +28,7 @@ HEADERS += mainwindow.h \
FORMS +=
DISTFILES += \
animate.vert \
animate.frag
animate.vert
RESOURCES += \
resources.qrc
......
#version 330
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 UV;
layout(location = 3) in ivec4 BoneIDs;
layout(location = 4) in vec4 Weights;
out vec3 vCamPosition;
out vec3 vCamNormal;
out vec2 vUV;
out vec4 debugout;
uniform mat4x4 MV;
uniform mat4x4 MVP;
uniform mat3x3 N;
uniform mat4x4 Bones[100];
void main(void)
{
debugout = vec4(BoneIDs)/60;
debugout = Weights;
//debugout = vec4(1);
mat4x4 BoneTransform= mat4x4(0.0);
for(int i =0; i <4; i++){
BoneTransform += Bones[BoneIDs[i]] * Weights[i];
}
if(Weights[0] == 0.0){
BoneTransform = mat4x4(1.0);
}
vec4 Pos = BoneTransform*vec4(Position,1.0);
vec4 Nor = BoneTransform*vec4(Normal,0.0);
//Pos = vec4(Position,1.0);
vCamPosition = vec4(MV*Pos).xyz;
vCamNormal = N*(Nor.xyz);
vUV = UV;
gl_Position = MVP * Pos;
}
......@@ -5,6 +5,7 @@ MainWidget::MainWidget(Camera *cam)
this->cam = cam;
startTime = QTime::currentTime();
rotTime = QTime::currentTime();
wireframe = true;
rotation = true;
}
......@@ -104,9 +105,9 @@ void MainWidget::initializeGL(){
glDisable(GL_CULL_FACE);
// Shader
subdevisionShader = initShader();
subdevisionShader = initShaderProgram();
loadNewMesh();
loadNewMesh( "D:/Projekte/GraPa/A5/Models/jeep.obj");
}
void MainWidget::loadNewMesh(){
......@@ -114,50 +115,70 @@ void MainWidget::loadNewMesh(){
QString("D:\\Projekte\\GraPa\\A5\\Models"),
tr("*.md5mesh *.3ds *.md2 *.obj *.dae *.dxf *.mesh.xml *.blend *.b3d" ));
if(fn.isEmpty())
loadNewMesh(fn);
}
void MainWidget::loadNewMesh(QString path){
if(path.isEmpty())
return;
qDebug()<<"Opening File:"<<fn;
Mesh* temp = new Mesh(this,fn);
qDebug()<<"Opening File:"<<path;
Mesh* temp = new Mesh(this,path);
mesh = temp;
}
void MainWidget::setRotation(bool started){
qDebug()<<"rotation"<<started;
this->rotation = started;
rotTime = QTime::currentTime();
}
void MainWidget::setWireframe(bool active){
this->wireframe = active;
}
QOpenGLShaderProgram* MainWidget::initShader(){
QString fragSource = QLatin1String(":/animate.frag");
QString vertSource = QLatin1String(":/animate.vert");
QOpenGLShaderProgram* MainWidget::initShaderProgram(){
QOpenGLShader *vert = new QOpenGLShader(QOpenGLShader::Vertex);
if(!vert->compileSourceFile(vertSource)){
qCritical()<< "Fragment Shader"<<vertSource<<"failed"<< vert->log();
exit(5);
}
QString vertSource = QLatin1String(":/subdivide.vert");
QString tesselationControlSource = QLatin1String(":/subdivide.tcs");
QString tesselationEvaluationSource = QLatin1String(":/subdivide.tes");
QString geometrySource = QLatin1String(":/subdivide.geo");
QString fragSource = QLatin1String(":/subdivide.frag");
QOpenGLShader *frag = new QOpenGLShader(QOpenGLShader::Fragment);
if(!frag->compileSourceFile(fragSource)){
qCritical()<< "Fragment Shader"<<fragSource<<"failed"<< frag->log();
exit(5);
}
QOpenGLShader *vert = initGLShader(vertSource,QOpenGLShader::Vertex);
QOpenGLShader *tesselationControl = initGLShader(tesselationControlSource,QOpenGLShader::TessellationControl);
QOpenGLShader *tesselationEval = initGLShader(tesselationEvaluationSource,QOpenGLShader::TessellationEvaluation);
QOpenGLShader *geometry = initGLShader(geometrySource,QOpenGLShader::Geometry);
QOpenGLShader *frag = initGLShader(fragSource,QOpenGLShader::Fragment);
qDebug()<<"Add Shaders ...";
QOpenGLShaderProgram* shader = new QOpenGLShaderProgram();
shader->addShader(vert);
shader->addShader(tesselationControl);
shader->addShader(tesselationEval);
shader->addShader(geometry);
shader->addShader(frag);
qDebug()<<"Linking Shaders ...";
if(!shader->link()){
qCritical()<< "Linking shader failed:"<<shader->log();
exit(5);
}
qDebug()<<"Linking Shader done";
return shader;
}
QOpenGLShader* MainWidget::initGLShader(QString scource, QOpenGLShader::ShaderType type){
QOpenGLShader *shader = new QOpenGLShader(type);
if(!shader->compileSourceFile(scource)){
qCritical()<<type<< "Shader"<<scource<<"failed"<< shader->log();
exit(5);
}
return shader;
}
void MainWidget::paintGL(){
glViewport(0,0,width(),height());
......@@ -172,6 +193,7 @@ void MainWidget::paintGL(){
}
subdevisionShader->bind();
subdevisionShader->setUniformValue("wireframe",wireframe);
subdevisionShader->setUniformValue("colorTexture",0);
subdevisionShader->setUniformValue("LightPos",QVector3D(0,100,100));
......
......@@ -24,7 +24,9 @@ public:
public slots:
void loadNewMesh();
void loadNewMesh(QString path);
void setRotation(bool started);
void setWireframe(bool active);
......@@ -55,10 +57,12 @@ private:
QPointF * lastScreenPos;
boolean rotation;
boolean wireframe;
Mesh *mesh;
QOpenGLShaderProgram* initShader();
QOpenGLShaderProgram* initShaderProgram();
QOpenGLShader* initGLShader(QString scource, QOpenGLShader::ShaderType type);
};
#endif // MAINWIDGET_H
......@@ -22,6 +22,12 @@ MainWindow::MainWindow(QWidget *parent) :
connect(switchRotation, SIGNAL(triggered(bool)), m_centralWidget, SLOT(setRotation(bool)));
toolBar->addAction(switchRotation);
switchWireframe = new QAction("Wireframe",this);
switchWireframe->setCheckable(true);
switchWireframe->setChecked(true);
connect(switchWireframe, SIGNAL(triggered(bool)), m_centralWidget, SLOT(setWireframe(bool)));
toolBar->addAction(switchWireframe);
CamHome = new QAction("Cam",this);
CamHome->setCheckable(false);
......
......@@ -24,6 +24,7 @@ private:
QToolBar *toolBar;
QAction *loadMesh;
QAction *switchRotation;
QAction *switchWireframe;
QAction *CamHome;
......
......@@ -335,7 +335,9 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index)
f->glVertexAttribPointer(uvIndex, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)24); // 12+12
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, entries[index].IB);
f->glDrawElements(GL_TRIANGLES, entries[index].numIndex, GL_UNSIGNED_INT, 0);
f->glPatchParameteri(GL_PATCH_VERTICES, 3);
f->glDrawElements(GL_PATCHES, entries[index].numIndex, GL_UNSIGNED_INT, 0);
}
void Mesh::findObjectDimension(Node node, QMatrix4x4 transform, QVector3D &min, QVector3D &max){
......
<RCC>
<qresource prefix="/">
<file>animate.frag</file>
<file>animate.vert</file>
<file>subdivide.frag</file>
<file>subdivide.geo</file>
<file>subdivide.tcs</file>
<file>subdivide.tes</file>
<file>subdivide.vert</file>
</qresource>
</RCC>
#version 420
layout(location = 0) out vec3 color;
in vec3 gCamPosition;
in vec3 gCamNormal;
in vec2 gUV;
in vec3 gTriDistance;
in vec3 gPatchDistance;
uniform bool wireframe;
uniform sampler2D colorTexture;
uniform vec3 LightPos;
struct Material
{
vec3 Diffuse;
vec3 Specular;
float Shininess;
bool hasTexture;
};
uniform Material materialInfo;
vec3 phong(vec3 lightPos){
vec3 dcolor = materialInfo.Diffuse;
vec3 dcolor2 = texture2D(colorTexture,gUV).xyz;
if(materialInfo.hasTexture){
dcolor = dcolor2;
}
vec3 V = normalize(-gCamPosition);
vec3 N = normalize(gCamNormal);
vec3 L = normalize(lightPos-gCamPosition);
vec3 R = normalize(reflect(-L,N));
float diffuse = max(dot(L,N),0.0);
float specular = pow(max(dot(R,V),0.0),materialInfo.Shininess);
//return vec3(diffuse );
//return dcolor;
return (diffuse*dcolor);
return (dcolor * diffuse + materialInfo.Specular * specular);
}
void addWireframe(){
//Wireframe rendering
if( wireframe == true){
float d = min(gTriDistance.x,min(gTriDistance.y,gTriDistance.z));
if(d < 0.02){
color = vec3(0.0,1.0,0.0);
}
float d2 = min(gPatchDistance.x,min(gPatchDistance.y,gPatchDistance.z));
if(d2 < 0.01){
color = vec3(0.0,0.0,1.0);
}
}
}
void main()
{
color = phong(LightPos);
addWireframe();
// color = materialInfo.Diffuse;
// color = normalize(abs(vec3(gCamPosition.x,gCamPosition.y,0.0)));
}
#version 420
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
in vec2 teUV[3];
out vec2 gUV;
in vec3 teCamPosition[3];
out vec3 gCamPosition;
in vec3 teCamNormal[3];
out vec3 gCamNormal;
out vec3 gTriDistance;
in vec3 tePatchDistance[3];
out vec3 gPatchDistance;
void main(void)
{
// passthrough geometry shader for wireframe rendering
// just addind TriDistance;
gTriDistance = vec3(1, 0, 0);
gUV = teUV[0];
gCamPosition = teCamPosition[0];
gCamNormal = teCamNormal[0];
gPatchDistance = tePatchDistance[0];
gl_Position = gl_in[0].gl_Position;
EmitVertex();
gTriDistance = vec3(0, 1, 0);
gUV = teUV[1];
gCamPosition = teCamPosition[1];
gCamNormal = teCamNormal[1];
gPatchDistance = tePatchDistance[1];
gl_Position = gl_in[1].gl_Position;
EmitVertex();
gTriDistance = vec3(0, 0, 1);
gUV = teUV[2];
gCamPosition = teCamPosition[2];
gCamNormal = teCamNormal[2];
gPatchDistance = tePatchDistance[2];
gl_Position = gl_in[2].gl_Position;
EmitVertex();
EndPrimitive();
}
#version 420
layout(vertices = 3) out;
in vec3 vPosition[];
out vec3 tcPosition[];
in vec3 vNormal[];
out vec3 tcNormal[];
in vec2 vUV[];
out vec2 tcUV[];
uniform vec3 CamPos;
void main()
{
tcPosition[gl_InvocationID] = vPosition[gl_InvocationID];
tcUV[gl_InvocationID] = vUV[gl_InvocationID];
float tesselation = 1.0;
gl_TessLevelOuter[0] = tesselation;
gl_TessLevelOuter[1] = tesselation;
gl_TessLevelOuter[2] = tesselation;
gl_TessLevelInner[0] = tesselation;
}
#version 420
layout(triangles, equal_spacing, cw) in;
in vec3 tcPosition[];
out vec3 teCamPosition;
in vec3 tcNormal[];
out vec3 teCamNormal;
in vec2 tcUV[];
out vec2 teUV;
out vec3 tePatchDistance;
uniform mat4x4 MV;
uniform mat4x4 MVP;
uniform mat3x3 N;
void main()
{
tePatchDistance = gl_TessCoord.xyz;
vec3 p0 = gl_TessCoord.x * tcPosition[0];
vec3 p1 = gl_TessCoord.y * tcPosition[1];
vec3 p2 = gl_TessCoord.z * tcPosition[2];
vec4 pos = vec4((p0 + p1 + p2),1);
teCamPosition = vec4(MV*pos).xyz;
gl_Position = MVP * pos;
vec3 n0 = gl_TessCoord.x * tcNormal[0];
vec3 n1 = gl_TessCoord.y * tcNormal[1];
vec3 n2 = gl_TessCoord.z * tcNormal[2];
teCamNormal = N*(n0 + n1 + n2);
vec2 u0 = gl_TessCoord.x * tcUV[0];
vec2 u1 = gl_TessCoord.y * tcUV[1];
vec2 u2 = gl_TessCoord.z * tcUV[2];
teUV = (u0 + u1 + u2);
}
#version 420
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 UV;
out vec3 vPosition;
out vec3 vNormal;
out vec2 vUV;
void main(void)
{
//transofrmation im tesselation evaluation , einfach nur durchreichen
vPosition.xyz = Position.xyz;
vNormal.xyz = Normal.xyz;
vUV = UV;
}
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