Commit f7a1ac58 by Alisa Jung

phong shading alles bis auf korrekte farben

parent d093f154
...@@ -21,3 +21,7 @@ HEADERS += mainwindow.h \ ...@@ -21,3 +21,7 @@ HEADERS += mainwindow.h \
RESOURCES += \ RESOURCES += \
helloqube.qrc helloqube.qrc
DISTFILES += \
phong.vert \
phong.frag
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2015-11-01T21:26:19. --> <!-- Written by QtCreator 3.5.1, 2015-11-02T15:23:12. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
......
...@@ -5,5 +5,7 @@ ...@@ -5,5 +5,7 @@
<file>img/gouraud.png</file> <file>img/gouraud.png</file>
<file>img/phong.png</file> <file>img/phong.png</file>
<file>img/wireframe.png</file> <file>img/wireframe.png</file>
<file>phong.frag</file>
<file>phong.vert</file>
</qresource> </qresource>
</RCC> </RCC>
...@@ -46,7 +46,33 @@ void MyGLWidget::initializeGL() ...@@ -46,7 +46,33 @@ void MyGLWidget::initializeGL()
glClearColor(0.0, 0.0, 0.0, 0.0);//TODO brauch ich das? glClearColor(0.0, 0.0, 0.0, 0.0);//TODO brauch ich das?
glEnable(GL_DEPTH_TEST);//TODO warum genau? glEnable(GL_DEPTH_TEST);//TODO warum genau?
//Phong Shader:
QGLShader* vert = new QGLShader(QGLShader::Vertex);
qDebug() << "new vert shader";
QGLShader* frag = new QGLShader(QGLShader::Fragment);
qDebug() << "new frag shader";
bool e = QFile::exists(":/phong.vert");
qDebug() << "vert exists" << e;
bool successVertex = vert->compileSourceFile(":/phong.vert");
if (!successVertex)qDebug() << "Vertex compilation failed.";
else qDebug() << "Vertex compiled.";
bool successFragment = frag->compileSourceFile(":/phong.frag");
if (!successFragment) qDebug() << "Frag failed";
else qDebug() << "frag success";
phongShader = new QGLShaderProgram(this);
phongShader->addShader(vert);
phongShader->addShader(frag);
phongShader->link();
shadeFlatSlot();//set up flat shading shadeFlatSlot();//set up flat shading
qDebug() << "flat done";
// glEnable(GL_CULL_FACE);//backface culling. // glEnable(GL_CULL_FACE);//backface culling.
// glCullFace(GL_BACK);//backface culling // glCullFace(GL_BACK);//backface culling
...@@ -90,15 +116,14 @@ void MyGLWidget::paintGL(){ ...@@ -90,15 +116,14 @@ void MyGLWidget::paintGL(){
double dist = qubeWidth/(double)tesselation; double dist = qubeWidth/(double)tesselation;
//for cube rotation
QMatrix4x4 translateRot1 = QMatrix4x4(1,0,0,tx,0,1,0,ty,0,0,1,0,0,0,0,1); QMatrix4x4 translateRot1 = QMatrix4x4(1,0,0,tx,0,1,0,ty,0,0,1,0,0,0,0,1);
QMatrix4x4 translateRot2 = QMatrix4x4(1,0,0,-tx,0,1,0,-ty,0,0,1,0,0,0,0,1); QMatrix4x4 translateRot2 = QMatrix4x4(1,0,0,-tx,0,1,0,-ty,0,0,1,0,0,0,0,1);
//for cube rotation
QMatrix4x4 m = QMatrix4x4(); QMatrix4x4 m = QMatrix4x4();
m.rotate(qubeRotationQuaternion); m.rotate(qubeRotationQuaternion);
glMultMatrixf(translateRot1.data());//Punkte ins zentrum verschieben damit rotation um qube zentrum ist glMultMatrixf(translateRot1.data());//Punkte zurück schieben damit rotation um qube zentrum ist
glMultMatrixf(m.data()); glMultMatrixf(m.data());
glMultMatrixf(translateRot2.data());//Punkte zurück verschieben glMultMatrixf(translateRot2.data());//Punkte ins Zentrum schieben
//4.1.1 Unit Qube + Tesselation + Diffuse Material //4.1.1 Unit Qube + Tesselation + Diffuse Material
{ {
...@@ -317,12 +342,15 @@ void MyGLWidget::wheelEvent(QWheelEvent *event){ ...@@ -317,12 +342,15 @@ void MyGLWidget::wheelEvent(QWheelEvent *event){
void MyGLWidget::shadeWireframeSlot(){ void MyGLWidget::shadeWireframeSlot(){
qDebug() << "Wireframe selected"; qDebug() << "Wireframe selected";
phongShader->release();
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//Wireframe looks way cooler with both faces on glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//Wireframe looks way cooler with both faces on
updateGL(); updateGL();
} }
void MyGLWidget::shadeFlatSlot(){ void MyGLWidget::shadeFlatSlot(){
qDebug() << "Flat selected"; qDebug() << "Flat selected";
phongShader->release();
glShadeModel(GL_FLAT); glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT,GL_FILL); glPolygonMode(GL_FRONT,GL_FILL);
updateGL(); updateGL();
...@@ -330,6 +358,7 @@ void MyGLWidget::shadeFlatSlot(){ ...@@ -330,6 +358,7 @@ void MyGLWidget::shadeFlatSlot(){
void MyGLWidget::shadeGouraudSlot(){ void MyGLWidget::shadeGouraudSlot(){
qDebug() << "Gouraud selected"; qDebug() << "Gouraud selected";
phongShader->release();
glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT,GL_FILL); glPolygonMode(GL_FRONT,GL_FILL);
updateGL(); updateGL();
...@@ -337,6 +366,8 @@ void MyGLWidget::shadeGouraudSlot(){ ...@@ -337,6 +366,8 @@ void MyGLWidget::shadeGouraudSlot(){
void MyGLWidget::shadePhongSlot(){ void MyGLWidget::shadePhongSlot(){
qDebug() << "Phong selected"; qDebug() << "Phong selected";
glPolygonMode(GL_FRONT,GL_FILL);
phongShader->bind();
updateGL(); updateGL();
} }
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include <QMatrix4x4> #include <QMatrix4x4>
#include <math.h> #include <math.h>
#include <QVector3D> #include <QVector3D>
#include <QGLShaderProgram>
#include <QGLShader>
class MyGLWidget : public QGLWidget class MyGLWidget : public QGLWidget
{ {
...@@ -49,6 +51,9 @@ private: ...@@ -49,6 +51,9 @@ private:
QQuaternion qubeRotationQuaternion; QQuaternion qubeRotationQuaternion;
QPoint screenCenter;//Center of screen in pixel coordinates QPoint screenCenter;//Center of screen in pixel coordinates
//Phong Shader
QGLShaderProgram *phongShader;
public slots: public slots:
//4.1.1 slots for shading modes //4.1.1 slots for shading modes
......
varying vec3 Normal;
varying vec4 Vertex;
varying vec4 VertexColor;
uniform gl_MaterialParameters gl_FrontMaterial;
void main(void)
{
/*
// vec4 white = vec4(1.0f,1.0f,1.0f,1.0f);
// gl_FragColor = white;
*/
vec3 V = -normalize(Vertex.xyz);
vec3 N = normalize(Normal);
vec3 L = normalize(gl_LightSource[0].position.xyz - Vertex.xyz);
vec3 R = normalize(reflect(-L,N));//- oder +?
float shininess = gl_FrontMaterial.shininess;
float kd = max(0.0,dot(L.xyz,N));
float rdotv = max(dot(R,V.xyz),0);
float ks = pow(rdotv,shininess);
vec4 color = vec4(rdotv);
// gl_FragColor = color;
gl_FragColor = vec4(ks + 0.5f*kd);
// gl_FragColor = vec4(gl_FrontMaterial.diffuse*kd);//es fehlt: licht und color
// gl_FragColor = vec4(gl_Color);//Nein.
// gl_FragColor = VertexColor;//Pink.
}
varying vec3 Normal;
varying vec4 Vertex;
varying vec4 VertexColor;
void main(void)
{
Vertex = gl_ModelViewMatrix * gl_Vertex;
Normal = normalize(gl_NormalMatrix*gl_Normal);
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
VertexColor = gl_FrontMaterial.diffuse;
}
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