Commit 26c7c733 by Kai Westerkamp

added Shader

Fixed Rotation Todo: Shader MAterial Gouraut Shading
parent 546c71b8
......@@ -22,6 +22,7 @@ void CubeWidget::initializeGL ()
glEnable(GL_DEPTH_TEST);
tesselation = 0;
prepareShader();
// enable default shading
home();
showFlat();
......@@ -30,13 +31,40 @@ void CubeWidget::initializeGL ()
glEnable(GL_LIGHT0);
static GLfloat lightPosition[4] = { 0.5, 0.0, 2.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
GLfloat white[] = {1.0,1.0,1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
}
void CubeWidget::prepareShader()
{
QGLShader *vertex = new QGLShader(QGLShader::Vertex);
if(!vertex->compileSourceFile(QLatin1String(":/phong.vert")))
qCritical()<< "Vertex Shader failed"<< vertex->log();
QGLShader *fragment = new QGLShader(QGLShader::Fragment);
if(!fragment->compileSourceFile(QLatin1String(":/phong.frag")))
qCritical()<< "Fragment Shader failed"<< fragment->log();
shader = new QGLShaderProgram(this);
shader->addShader(vertex);
shader->addShader(fragment);
if(!shader->link())
qCritical()<< "Linking failed"<<shader->log();
}
void CubeWidget::setMaterial(GLfloat *color )
{
glMaterialfv(GL_FRONT,GL_AMBIENT,color);
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
glMaterialfv(GL_FRONT,GL_SPECULAR,color);
glMaterialfv(GL_FRONT,GL_SHININESS,color);
glMaterialfv(GL_FRONT,GL_SHININESS,new GLfloat(50.0));
}
void CubeWidget::paintGL ( )
......@@ -50,19 +78,15 @@ void CubeWidget::paintGL ( )
mat.rotate(*rotation);
glMultMatrixf(mat.data());
GLfloat red[] = {1.0,0.0,0.0};
GLfloat green[] = {0.0,1.0,0.0};
GLfloat blue[] = {0.0,0.0,1.0};
GLfloat cyan[] = {0.0,1.0,1.0};
GLfloat magenta[] = {1.0,0.0,1.0};
GLfloat yellow[] = {1.0,1.0,0.0};
glBegin(GL_QUADS);
float increment = 1.0/(pow(2,tesselation));
glBegin(GL_QUADS);
setMaterial(red);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
......@@ -72,7 +96,9 @@ void CubeWidget::paintGL ( )
glVertex3f( x+increment, 0.5f, y+increment);
}
}
glEnd();
glBegin(GL_QUADS);
setMaterial(magenta);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
......@@ -82,7 +108,9 @@ void CubeWidget::paintGL ( )
glVertex3f( x+increment, -0.5f, y);
}
}
glEnd();
glBegin(GL_QUADS);
setMaterial(green);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
......@@ -92,7 +120,9 @@ void CubeWidget::paintGL ( )
glVertex3f( x+increment, y, 0.5f);
}
}
glEnd();
glBegin(GL_QUADS);
setMaterial(yellow);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
......@@ -102,8 +132,9 @@ void CubeWidget::paintGL ( )
glVertex3f( x+increment,y+increment, -0.5f);
}
}
glEnd();
glBegin(GL_QUADS);
setMaterial(blue);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
......@@ -113,7 +144,8 @@ void CubeWidget::paintGL ( )
glVertex3f( -0.5f,x, y+increment);
}
}
glEnd();
glBegin(GL_QUADS);
setMaterial(cyan);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
......@@ -158,9 +190,9 @@ void CubeWidget::showWireframe()
qDebug("show Wireframe");
glShadeModel(GL_FLAT);
// glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
shader->release();
updateGL();
}
......@@ -168,8 +200,8 @@ void CubeWidget::showFlat()
{
qDebug("show Flat");
glShadeModel(GL_FLAT);
// glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
shader->release();
updateGL();
}
......@@ -177,15 +209,17 @@ void CubeWidget::showGouraut()
{
qDebug("show Gouraut");
glShadeModel(GL_SMOOTH);
// glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
shader->release();
updateGL();
}
void CubeWidget::showPhong()
{
qDebug("show Phong");
showFlat();
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
shader->bind();
updateGL();
}
......@@ -210,7 +244,6 @@ void CubeWidget::mousePressEvent(QMouseEvent *event )
// return;
if (event->buttons() & Qt::LeftButton) {
qDebug("press");
lastPos = trackballPoint(event->pos().x(),event->pos().y());
......@@ -230,7 +263,6 @@ void CubeWidget::mouseMoveEvent(QMouseEvent *event )
// return;
if (event->buttons() & Qt::LeftButton) {
qDebug("move");
move(trackballPoint(event->pos().x(),event->pos().y()));
//m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
......@@ -248,7 +280,7 @@ void CubeWidget::move(QVector3D *newPos )
float angle = 180 / M_PI * std::asin(std::sqrt(QVector3D::dotProduct(axis, axis)));
axis.normalize();
axis = rotation->conjugate().rotatedVector(axis);
//axis = rotation->conjugate().rotatedVector(axis);
QQuaternion newRot = QQuaternion::fromAxisAndAngle(axis, angle) * *rotation;
rotation = new QQuaternion(newRot.toVector4D());
......@@ -260,7 +292,7 @@ void CubeWidget::move(QVector3D *newPos )
QVector3D* CubeWidget::trackballPoint(int x, int y){
float xo,yo,zo;
qDebug()<<"x:"<< x << " y:"<<y;
// qDebug()<<"x:"<< x << " y:"<<y;
xo = ((2.0*x)-width())/ height();
yo = (height()-(2.0*y))/width();
......@@ -271,7 +303,7 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){
QVector3D *pos = new QVector3D(xo,yo,zo);
pos->normalize();
qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
// qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return pos;
}
......
......@@ -46,6 +46,10 @@ private:
QVector3D *lastPos;
bool mousDown = false;
QGLShaderProgram *shader;
void prepareShader();
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
void setMaterial(GLfloat *color );
void move(QVector3D *newPos );
......
......@@ -20,3 +20,7 @@ HEADERS += mainwindow.h \
RESOURCES += \
hellocube.qrc
DISTFILES += \
phong.frag \
phong.vert
......@@ -5,5 +5,7 @@
<file>img/gouraud.png</file>
<file>img/phong.png</file>
<file>img/wireframe.png</file>
<file>phong.frag</file>
<file>phong.vert</file>
</qresource>
</RCC>
uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
uniform gl_MaterialParameters gl_FrontMaterial;
uniform gl_LightModelProducts gl_FrontLightModelProduct;
varying vec3 position;
varying vec3 normal;
void main(void)
{
vec3 N = normalize(normal);
vec3 L = normalize(vec3(gl_LightSource[0].position)-position);
vec3 V = normalize(-position.xyz);
vec3 R = reflect(-L,N);
vec4 diffuse = vec4(max(dot(L,N),0.0));
vec4 specular = vec4(pow(max(dot(R,V),0.0),gl_FrontMaterial.shininess));
vec4 Iamb = gl_FrontLightProduct[0].ambient;
vec4 Idiff = gl_FrontLightProduct[0].diffuse*diffuse;
Idiff = clamp(Idiff, 0.0, 1.0);
vec4 Ispec = gl_FrontLightProduct[0].specular*specular;
Ispec = clamp(Idiff, 0.0, 1.0);
vec4 color = /*gl_FrontLightModelProduct.sceneColor +*/ Iamb + Idiff + Ispec;
gl_FragColor = vec4(color);
}
attribute vec4 gl_Vertex;
attribute vec3 gl_Normal;
uniform mat4 gl_ModelViewProjectionMatrix;
uniform mat4 gl_ModelViewMatrix;
uniform mat3 gl_NormalMatrix;
varying vec3 position;
varying vec3 normal;
void main(void)
{
normal = normalize(gl_NormalMatrix * gl_Normal);
position = vec3(gl_ModelViewMatrix *gl_Vertex);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
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