Commit d5b0a410 by Alisa Jung

translate via matrix, not via tx+, ty+

also, old stuff from first assignment
parent f7a1ac58
...@@ -14,10 +14,16 @@ TEMPLATE = app ...@@ -14,10 +14,16 @@ TEMPLATE = app
SOURCES += main.cpp\ SOURCES += main.cpp\
mainwindow.cpp \ mainwindow.cpp \
myglwidget.cpp myglwidget.cpp \
scenegraph.cpp \
primitive.cpp \
rigidbodytransformation.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
myglwidget.h myglwidget.h \
scenegraph.h \
primitive.h \
rigidbodytransformation.h
RESOURCES += \ RESOURCES += \
helloqube.qrc helloqube.qrc
......
<?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-02T15:23:12. --> <!-- Written by QtCreator 3.5.1, 2015-11-10T14:37:55. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
......
...@@ -5,8 +5,8 @@ MyGLWidget::MyGLWidget(QWidget *parent) ...@@ -5,8 +5,8 @@ MyGLWidget::MyGLWidget(QWidget *parent)
{ {
tesselation = 1; tesselation = 1;
qubeRotationQuaternion = QQuaternion(); qubeRotationQuaternion = QQuaternion();
tx = 0; translatex = 0;
ty = 0; translatey = 0;
cameraZoom = cameraZoomDefault; cameraZoom = cameraZoomDefault;
clickStartPosition = QPoint(0,0); clickStartPosition = QPoint(0,0);
rightButtonPressed = false; rightButtonPressed = false;
...@@ -19,10 +19,11 @@ MyGLWidget::~MyGLWidget() ...@@ -19,10 +19,11 @@ MyGLWidget::~MyGLWidget()
* data structures are deleted when the widget is no longer * data structures are deleted when the widget is no longer
* needed (although in this case nothing needs cleaning up). * needed (although in this case nothing needs cleaning up).
*/ */
//In helloGL example: nothin needs cleaning up. //In helloGL example: nothing needs cleaning up.
} }
/* /*
* From Example
* We provide size hint functions to ensure that * We provide size hint functions to ensure that
* the widgetis shown at a reasonable size: * the widgetis shown at a reasonable size:
*/ */
...@@ -32,6 +33,7 @@ QSize MyGLWidget::minimumSizeHint() const ...@@ -32,6 +33,7 @@ QSize MyGLWidget::minimumSizeHint() const
} }
/* /*
* From Example
* We provide size hint functions to ensure that * We provide size hint functions to ensure that
* the widgetis shown at a reasonable size: * the widgetis shown at a reasonable size:
*/ */
...@@ -43,9 +45,8 @@ QSize MyGLWidget::sizeHint() const ...@@ -43,9 +45,8 @@ QSize MyGLWidget::sizeHint() const
void MyGLWidget::initializeGL() 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);
glEnable(GL_DEPTH_TEST);//TODO warum genau? glEnable(GL_DEPTH_TEST);
//Phong Shader: //Phong Shader:
QGLShader* vert = new QGLShader(QGLShader::Vertex); QGLShader* vert = new QGLShader(QGLShader::Vertex);
...@@ -117,17 +118,19 @@ void MyGLWidget::paintGL(){ ...@@ -117,17 +118,19 @@ void MyGLWidget::paintGL(){
//for cube rotation //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,translatex,0,1,0,translatey,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,-translatex,0,1,0,-translatey,0,0,1,0,0,0,0,1);
QMatrix4x4 translateText = QMatrix4x4(1,0,0,2,0,1,0,0,0,0,1,0,0,0,0,1);
QMatrix4x4 m = QMatrix4x4(); QMatrix4x4 m = QMatrix4x4();
m.rotate(qubeRotationQuaternion); m.rotate(qubeRotationQuaternion);
glMultMatrixf(translateRot1.data());//Punkte zurück schieben 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 ins Zentrum schieben
//4.1.1 Unit Qube + Tesselation + Diffuse Material //4.1.1 Unit Qube + Tesselation + Diffuse Material
{ {
glBegin(GL_QUADS);
GLfloat specularColor[] = {1,1,1}; GLfloat specularColor[] = {1,1,1};
GLfloat shininess[] = {128};//specular exponent GLfloat shininess[] = {128};//specular exponent
...@@ -135,18 +138,20 @@ void MyGLWidget::paintGL(){ ...@@ -135,18 +138,20 @@ void MyGLWidget::paintGL(){
glMaterialfv(GL_FRONT,GL_SHININESS,shininess); glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
//vorne // z-Achse //vorne // z-Achse
// glColor3f(0,0,1);//blau // glColor3f(0,0,1);//blau //alt und doof
GLfloat blue[] = {0,0,1}; GLfloat blue[] = {0,0,1};
glMaterialfv(GL_FRONT,GL_DIFFUSE,blue); glMaterialfv(GL_FRONT,GL_DIFFUSE,blue);
glNormal3f(0,0,1); glNormal3f(0,0,1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){ for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){ for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*i,ty+0.5-dist*j,0.5); glVertex3f(0.5-dist*i,0.5-dist*j,0.5);
glVertex3f(tx+0.5-dist*(i+1),ty+0.5-dist*j,0.5); glVertex3f(0.5-dist*(i+1),0.5-dist*j,0.5);
glVertex3f(tx+0.5-dist*(i+1),ty+0.5-dist*(j+1),0.5); glVertex3f(0.5-dist*(i+1),0.5-dist*(j+1),0.5);
glVertex3f(tx+0.5-dist*i,ty+0.5-dist*(j+1),0.5); glVertex3f(0.5-dist*i,0.5-dist*(j+1),0.5);
} }
} }
glEnd();
//reference for previous front //reference for previous front
// glVertex3f(0.5, 0.5, 0.5);//4 // glVertex3f(0.5, 0.5, 0.5);//4
// glVertex3f(-0.5, 0.5, 0.5);//3 // glVertex3f(-0.5, 0.5, 0.5);//3
...@@ -157,70 +162,75 @@ void MyGLWidget::paintGL(){ ...@@ -157,70 +162,75 @@ void MyGLWidget::paintGL(){
GLfloat green[] = {0,1,0}; GLfloat green[] = {0,1,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,green); glMaterialfv(GL_FRONT,GL_DIFFUSE,green);
glNormal3f(0,1,0); glNormal3f(0,1,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){ for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){ for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*(i+1), ty+0.5, 0.5-dist*(j+1)); glVertex3f(0.5-dist*(i+1), 0.5, 0.5-dist*(j+1));
glVertex3f(tx+0.5-dist*(i+1), ty+0.5, 0.5-dist*j); glVertex3f(0.5-dist*(i+1), 0.5, 0.5-dist*j);
glVertex3f(tx+0.5-dist*i, ty+0.5, 0.5-dist*j); glVertex3f(0.5-dist*i, 0.5, 0.5-dist*j);
glVertex3f(tx+0.5-dist*i, ty+0.5, 0.5-dist*(j+1)); glVertex3f(0.5-dist*i, 0.5, 0.5-dist*(j+1));
} }
} }
glEnd();
//rechts //x-Achse //rechts //x-Achse
GLfloat red[] = {1,0,0}; GLfloat red[] = {1,0,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,red); glMaterialfv(GL_FRONT,GL_DIFFUSE,red);
glNormal3f(1,0,0); glNormal3f(1,0,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){ for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){ for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5, ty+0.5-dist*i, 0.5-dist*j); glVertex3f(0.5, 0.5-dist*i, 0.5-dist*j);
glVertex3f(tx+0.5, ty+0.5-dist*(i+1), 0.5-dist*j); glVertex3f(0.5, 0.5-dist*(i+1), 0.5-dist*j);
glVertex3f(tx+0.5, ty+0.5-dist*(i+1), 0.5-dist*(j+1)); glVertex3f(0.5, 0.5-dist*(i+1), 0.5-dist*(j+1));
glVertex3f(tx+0.5, ty+0.5-dist*i, 0.5-dist*(j+1)); glVertex3f(0.5, 0.5-dist*i, 0.5-dist*(j+1));
} }
} }
glEnd();
//hinten // z-Achse //hinten // z-Achse
GLfloat yellow[] = {1,0.9f,0}; GLfloat yellow[] = {1,0.9f,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,yellow); glMaterialfv(GL_FRONT,GL_DIFFUSE,yellow);
glNormal3f(0,0,-1); glNormal3f(0,0,-1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){ for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){ for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*i, ty+0.5-dist*(j+1), -0.5);//4 glVertex3f(0.5-dist*i, 0.5-dist*(j+1), -0.5);//4
glVertex3f(tx+0.5-dist*(i+1), ty+0.5-dist*(j+1), -0.5);//3 glVertex3f(0.5-dist*(i+1), 0.5-dist*(j+1), -0.5);//3
glVertex3f(tx+0.5-dist*(i+1), ty+0.5-dist*j, -0.5);//2 glVertex3f(0.5-dist*(i+1), 0.5-dist*j, -0.5);//2
glVertex3f(tx+0.5-dist*i,ty+ 0.5-dist*j, -0.5);//1 glVertex3f(0.5-dist*i, 0.5-dist*j, -0.5);//1
} }
} }
glEnd();
//links //links
GLfloat cyan[] = {0,1,1}; GLfloat cyan[] = {0,1,1};
glMaterialfv(GL_FRONT,GL_DIFFUSE,cyan); glMaterialfv(GL_FRONT,GL_DIFFUSE,cyan);
glNormal3f(-1,0,0); glNormal3f(-1,0,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){ for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){ for (int j = 0; j < tesselation; j++){
glVertex3f(tx-0.5, ty+0.5-dist*i, 0.5-dist*(j+1)); glVertex3f(-0.5, 0.5-dist*i, 0.5-dist*(j+1));
glVertex3f(tx-0.5, ty+0.5-dist*(i+1), 0.5-dist*(j+1)); glVertex3f(-0.5, 0.5-dist*(i+1), 0.5-dist*(j+1));
glVertex3f(tx-0.5, ty+0.5-dist*(i+1), 0.5-dist*j); glVertex3f(-0.5, 0.5-dist*(i+1), 0.5-dist*j);
glVertex3f(tx-0.5, ty+0.5-dist*i, 0.5-dist*j); glVertex3f(-0.5, 0.5-dist*i, 0.5-dist*j);
} }
} }
glEnd();
//unten //-y //unten //-y
GLfloat magenta[] = {1,0,1}; GLfloat magenta[] = {1,0,1};
glMaterialfv(GL_FRONT,GL_DIFFUSE,magenta); glMaterialfv(GL_FRONT,GL_DIFFUSE,magenta);
glNormal3f(0,-1,0); glNormal3f(0,-1,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){ for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){ for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*i, ty-0.5, 0.5-dist*(j+1)); glVertex3f(0.5-dist*i, -0.5, 0.5-dist*(j+1));
glVertex3f(tx+0.5-dist*i, ty-0.5, 0.5-dist*j); glVertex3f(0.5-dist*i, -0.5, 0.5-dist*j);
glVertex3f(tx+0.5-dist*(i+1), ty-0.5, 0.5-dist*j); glVertex3f(0.5-dist*(i+1), -0.5, 0.5-dist*j);
glVertex3f(tx+0.5-dist*(i+1), ty-0.5, 0.5-dist*(j+1)); glVertex3f(0.5-dist*(i+1), -0.5, 0.5-dist*(j+1));
} }
} }
glEnd(); glEnd();
} }
} }
...@@ -233,7 +243,7 @@ void MyGLWidget::resizeGL(int width, int height){ ...@@ -233,7 +243,7 @@ void MyGLWidget::resizeGL(int width, int height){
glMatrixMode(GL_PROJECTION);//from HelloGL example glMatrixMode(GL_PROJECTION);//from HelloGL example
glLoadIdentity(); glLoadIdentity();
gluPerspective(45.0,p,0.1,1000); gluPerspective(45.0,p,0.1,1000);//set vof to 45°
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
...@@ -256,13 +266,13 @@ void MyGLWidget::mousePressEvent(QMouseEvent *event){ ...@@ -256,13 +266,13 @@ void MyGLWidget::mousePressEvent(QMouseEvent *event){
} }
void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){
//This didn't work properly if (event->button() == Qt::RightButton){ //This didn't work properly, used bool to store state instead. if (event->button() == Qt::RightButton){
if (rightButtonPressed){ if (rightButtonPressed){
QPoint currentPosition = event->pos(); QPoint currentPosition = event->pos();
QPoint diff = currentPosition - clickStartPosition; QPoint diff = currentPosition - clickStartPosition;
clickStartPosition = currentPosition; clickStartPosition = currentPosition;
tx += 0.01*diff.x(); translatex += 0.01*diff.x();
ty -= 0.01*diff.y(); translatey -= 0.01*diff.y();
updateGL(); updateGL();
}else if (leftButtonPressed){ }else if (leftButtonPressed){
...@@ -272,11 +282,11 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){ ...@@ -272,11 +282,11 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){
//translate touches around screen center //translate touches around screen center
double xOld = ((double)(clickStartPosition.x() - screenCenter.x())); double xOld = ((double)(clickStartPosition.x() - screenCenter.x()));
double yOld = ((double)(clickStartPosition.y() - screenCenter.y())); double yNew = ((double)(clickStartPosition.y() - screenCenter.y()));
QPoint newPosition = event->pos(); QPoint newPosition = event->pos();
double xNew = ((double)(newPosition.x() - screenCenter.x())); double xNew = ((double)(newPosition.x() - screenCenter.x()));
double yNew = ((double)(newPosition.y() - screenCenter.y())); double yOld = ((double)(newPosition.y() - screenCenter.y()));
clickStartPosition = newPosition; clickStartPosition = newPosition;
...@@ -332,7 +342,7 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){ ...@@ -332,7 +342,7 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){
//4.1.2 User Input: Zooming //4.1.2 User Input: Zooming
void MyGLWidget::wheelEvent(QWheelEvent *event){ void MyGLWidget::wheelEvent(QWheelEvent *event){
cameraZoom += 0.01*event->delta(); cameraZoom += 0.01*event->delta();//0.01 seemed to be a good factor
qDebug() << "Cam zoom delta " << event->delta() << " stored " << cameraZoom; qDebug() << "Cam zoom delta " << event->delta() << " stored " << cameraZoom;
updateGL(); updateGL();
} }
...@@ -381,8 +391,8 @@ void MyGLWidget::setTessellation(int t){ ...@@ -381,8 +391,8 @@ void MyGLWidget::setTessellation(int t){
void MyGLWidget::resetCamera(){ void MyGLWidget::resetCamera(){
qubeRotationQuaternion = QQuaternion(); qubeRotationQuaternion = QQuaternion();
tx = 0; translatex = 0;
ty = 0; translatey = 0;
cameraZoom = cameraZoomDefault; cameraZoom = cameraZoomDefault;
updateGL(); updateGL();
} }
...@@ -21,8 +21,8 @@ class MyGLWidget : public QGLWidget ...@@ -21,8 +21,8 @@ class MyGLWidget : public QGLWidget
~MyGLWidget(); ~MyGLWidget();
//From HelloGL example. //From HelloGL example.
QSize minimumSizeHint() const;//do I need this? QSize minimumSizeHint() const;
QSize sizeHint() const;//do I need this? QSize sizeHint() const;
protected: protected:
//4.1 Core Functionality //4.1 Core Functionality
...@@ -44,7 +44,7 @@ private: ...@@ -44,7 +44,7 @@ private:
//translate camera //translate camera
QPoint clickStartPosition; QPoint clickStartPosition;
bool rightButtonPressed; bool rightButtonPressed;
double tx, ty; double translatex, translatey;
//rotate cube //rotate cube
bool leftButtonPressed; bool leftButtonPressed;
......
varying vec3 Normal; varying vec3 Normal;
varying vec4 Vertex; varying vec4 Vertex;
varying vec4 VertexColor;
uniform gl_MaterialParameters gl_FrontMaterial; uniform gl_MaterialParameters gl_FrontMaterial;
void main(void) void main(void)
...@@ -10,31 +8,30 @@ void main(void) ...@@ -10,31 +8,30 @@ void main(void)
/* /*
// vec4 white = vec4(1.0f,1.0f,1.0f,1.0f); // vec4 white = vec4(1.0f,1.0f,1.0f,1.0f);
// gl_FragColor = white; // gl_FragColor = white;
*/ */
vec3 V = -normalize(Vertex.xyz); vec3 V = -normalize(Vertex.xyz);
vec3 N = normalize(Normal); vec3 N = normalize(Normal);
vec3 L = normalize(gl_LightSource[0].position.xyz - Vertex.xyz); vec3 L = normalize(gl_LightSource[0].position.xyz - Vertex.xyz);
vec3 R = normalize(reflect(-L,N));//- oder +? vec3 R = normalize(reflect(-L,N));//- oder +?
float shininess = gl_FrontMaterial.shininess; //diffuse
float dot_LN = max(0.0,dot(L.xyz,N));
float kd = max(0.0,dot(L.xyz,N));
//specular
float n = gl_FrontMaterial.shininess;
float rdotv = max(dot(R,V.xyz),0); float rdotv = max(dot(R,V.xyz),0);
float dot_rv_n = pow(rdotv,n);
float ks = pow(rdotv,shininess); vec4 kd = gl_FrontMaterial.diffuse;//color diffuse
vec4 ks = gl_FrontMaterial.specular;//color specular
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 vec4 I_L_d = gl_LightSource[0].diffuse;
vec4 I_L_s = gl_LightSource[0].specular;
// gl_FragColor = vec4(gl_Color);//Nein. vec4 color = dot_LN*kd*I_L_d + dot_rv_n*ks*I_L_s;
// gl_FragColor = VertexColor;//Pink.
gl_FragColor = vec4(color);
} }
varying vec3 Normal; varying vec3 Normal;
varying vec4 Vertex; varying vec4 Vertex;
varying vec4 VertexColor;
void main(void) void main(void)
{ {
Vertex = gl_ModelViewMatrix * gl_Vertex; Vertex = gl_ModelViewMatrix * gl_Vertex;
Normal = normalize(gl_NormalMatrix*gl_Normal); Normal = normalize(gl_NormalMatrix*gl_Normal);
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
VertexColor = gl_FrontMaterial.diffuse;
} }
#include "primitive.h"
Primitive::Primitive()
{
}
#ifndef PRIMITIVE_H
#define PRIMITIVE_H
class Primitive
{
public:
Primitive();
};
#endif // PRIMITIVE_H
#include "rigidbodytransformation.h"
RigidBodyTransformation::RigidBodyTransformation(Primitive child)
{
tx = 0;
ty = 0;
tz = 0;
rotation = new QQuaternion();
this->child = child;
}
RigidBodyTransformation::setTranslation(double x, double y, double z=0){
tx = x;
ty = y;
tz = z;
}
RigidBodyTransformation::getRotation(){
return rotation;
}
RigidBodyTransformation::setRotation(QQuaternion newRotation){
rotation = newRotation;
}
#ifndef RIGIDBODYTRANSFORMATION_H
#define RIGIDBODYTRANSFORMATION_H
class RigidBodyTransformation
{
public:
RigidBodyTransformation();
void setTranslation(double x, double y, double z = 0);
void setRotation(QQuaternion newRotation);
QQuaternion getRotation();
private :
//translation
double tx, ty, tz;
//rotation
QQuaternion rotation;
//child in scene graph
Primitive child;
};
#endif // RIGIDBODYTRANSFORMATION_H
#include "scenegraph.h"
SceneGraph::SceneGraph()
{
}
#ifndef SCENEGRAPH_H
#define SCENEGRAPH_H
class SceneGraph
{
public:
SceneGraph();
private:
};
#endif // SCENEGRAPH_H
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