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
SOURCES += main.cpp\
mainwindow.cpp \
myglwidget.cpp
myglwidget.cpp \
scenegraph.cpp \
primitive.cpp \
rigidbodytransformation.cpp
HEADERS += mainwindow.h \
myglwidget.h
myglwidget.h \
scenegraph.h \
primitive.h \
rigidbodytransformation.h
RESOURCES += \
helloqube.qrc
......
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<data>
<variable>EnvironmentId</variable>
......
......@@ -5,8 +5,8 @@ MyGLWidget::MyGLWidget(QWidget *parent)
{
tesselation = 1;
qubeRotationQuaternion = QQuaternion();
tx = 0;
ty = 0;
translatex = 0;
translatey = 0;
cameraZoom = cameraZoomDefault;
clickStartPosition = QPoint(0,0);
rightButtonPressed = false;
......@@ -19,10 +19,11 @@ MyGLWidget::~MyGLWidget()
* data structures are deleted when the widget is no longer
* 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
* the widgetis shown at a reasonable size:
*/
......@@ -32,6 +33,7 @@ QSize MyGLWidget::minimumSizeHint() const
}
/*
* From Example
* We provide size hint functions to ensure that
* the widgetis shown at a reasonable size:
*/
......@@ -43,9 +45,8 @@ QSize MyGLWidget::sizeHint() const
void MyGLWidget::initializeGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0);//TODO brauch ich das?
glEnable(GL_DEPTH_TEST);//TODO warum genau?
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
//Phong Shader:
QGLShader* vert = new QGLShader(QGLShader::Vertex);
......@@ -117,17 +118,19 @@ void MyGLWidget::paintGL(){
//for cube rotation
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 translateRot1 = QMatrix4x4(1,0,0,translatex,0,1,0,translatey,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();
m.rotate(qubeRotationQuaternion);
glMultMatrixf(translateRot1.data());//Punkte zurück schieben damit rotation um qube zentrum ist
glMultMatrixf(m.data());
glMultMatrixf(translateRot2.data());//Punkte ins Zentrum schieben
//4.1.1 Unit Qube + Tesselation + Diffuse Material
{
glBegin(GL_QUADS);
GLfloat specularColor[] = {1,1,1};
GLfloat shininess[] = {128};//specular exponent
......@@ -135,18 +138,20 @@ void MyGLWidget::paintGL(){
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
//vorne // z-Achse
// glColor3f(0,0,1);//blau
// glColor3f(0,0,1);//blau //alt und doof
GLfloat blue[] = {0,0,1};
glMaterialfv(GL_FRONT,GL_DIFFUSE,blue);
glNormal3f(0,0,1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*i,ty+0.5-dist*j,0.5);
glVertex3f(tx+0.5-dist*(i+1),ty+0.5-dist*j,0.5);
glVertex3f(tx+0.5-dist*(i+1),ty+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,0.5);
glVertex3f(0.5-dist*(i+1),0.5-dist*j,0.5);
glVertex3f(0.5-dist*(i+1),0.5-dist*(j+1),0.5);
glVertex3f(0.5-dist*i,0.5-dist*(j+1),0.5);
}
}
glEnd();
//reference for previous front
// glVertex3f(0.5, 0.5, 0.5);//4
// glVertex3f(-0.5, 0.5, 0.5);//3
......@@ -157,70 +162,75 @@ void MyGLWidget::paintGL(){
GLfloat green[] = {0,1,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,green);
glNormal3f(0,1,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*(i+1), ty+0.5, 0.5-dist*(j+1));
glVertex3f(tx+0.5-dist*(i+1), ty+0.5, 0.5-dist*j);
glVertex3f(tx+0.5-dist*i, ty+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+1), 0.5, 0.5-dist*(j+1));
glVertex3f(0.5-dist*(i+1), 0.5, 0.5-dist*j);
glVertex3f(0.5-dist*i, 0.5, 0.5-dist*j);
glVertex3f(0.5-dist*i, 0.5, 0.5-dist*(j+1));
}
}
glEnd();
//rechts //x-Achse
GLfloat red[] = {1,0,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,red);
glNormal3f(1,0,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5, ty+0.5-dist*i, 0.5-dist*j);
glVertex3f(tx+0.5, ty+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(tx+0.5, ty+0.5-dist*i, 0.5-dist*(j+1));
glVertex3f(0.5, 0.5-dist*i, 0.5-dist*j);
glVertex3f(0.5, 0.5-dist*(i+1), 0.5-dist*j);
glVertex3f(0.5, 0.5-dist*(i+1), 0.5-dist*(j+1));
glVertex3f(0.5, 0.5-dist*i, 0.5-dist*(j+1));
}
}
glEnd();
//hinten // z-Achse
GLfloat yellow[] = {1,0.9f,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,yellow);
glNormal3f(0,0,-1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*i, ty+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(tx+0.5-dist*(i+1), ty+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+1), -0.5);//4
glVertex3f(0.5-dist*(i+1), 0.5-dist*(j+1), -0.5);//3
glVertex3f(0.5-dist*(i+1), 0.5-dist*j, -0.5);//2
glVertex3f(0.5-dist*i, 0.5-dist*j, -0.5);//1
}
}
glEnd();
//links
GLfloat cyan[] = {0,1,1};
glMaterialfv(GL_FRONT,GL_DIFFUSE,cyan);
glNormal3f(-1,0,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(tx-0.5, ty+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(tx-0.5, ty+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+1));
glVertex3f(-0.5, 0.5-dist*(i+1), 0.5-dist*(j+1));
glVertex3f(-0.5, 0.5-dist*(i+1), 0.5-dist*j);
glVertex3f(-0.5, 0.5-dist*i, 0.5-dist*j);
}
}
glEnd();
//unten //-y
GLfloat magenta[] = {1,0,1};
glMaterialfv(GL_FRONT,GL_DIFFUSE,magenta);
glNormal3f(0,-1,0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(tx+0.5-dist*i, ty-0.5, 0.5-dist*(j+1));
glVertex3f(tx+0.5-dist*i, ty-0.5, 0.5-dist*j);
glVertex3f(tx+0.5-dist*(i+1), ty-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, -0.5, 0.5-dist*(j+1));
glVertex3f(0.5-dist*i, -0.5, 0.5-dist*j);
glVertex3f(0.5-dist*(i+1), -0.5, 0.5-dist*j);
glVertex3f(0.5-dist*(i+1), -0.5, 0.5-dist*(j+1));
}
}
glEnd();
}
}
......@@ -233,7 +243,7 @@ void MyGLWidget::resizeGL(int width, int height){
glMatrixMode(GL_PROJECTION);//from HelloGL example
glLoadIdentity();
gluPerspective(45.0,p,0.1,1000);
gluPerspective(45.0,p,0.1,1000);//set vof to 45°
glMatrixMode(GL_MODELVIEW);
......@@ -256,13 +266,13 @@ void MyGLWidget::mousePressEvent(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){
QPoint currentPosition = event->pos();
QPoint diff = currentPosition - clickStartPosition;
clickStartPosition = currentPosition;
tx += 0.01*diff.x();
ty -= 0.01*diff.y();
translatex += 0.01*diff.x();
translatey -= 0.01*diff.y();
updateGL();
}else if (leftButtonPressed){
......@@ -272,11 +282,11 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){
//translate touches around screen center
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();
double xNew = ((double)(newPosition.x() - screenCenter.x()));
double yNew = ((double)(newPosition.y() - screenCenter.y()));
double yOld = ((double)(newPosition.y() - screenCenter.y()));
clickStartPosition = newPosition;
......@@ -332,7 +342,7 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){
//4.1.2 User Input: Zooming
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;
updateGL();
}
......@@ -381,8 +391,8 @@ void MyGLWidget::setTessellation(int t){
void MyGLWidget::resetCamera(){
qubeRotationQuaternion = QQuaternion();
tx = 0;
ty = 0;
translatex = 0;
translatey = 0;
cameraZoom = cameraZoomDefault;
updateGL();
}
......@@ -21,8 +21,8 @@ class MyGLWidget : public QGLWidget
~MyGLWidget();
//From HelloGL example.
QSize minimumSizeHint() const;//do I need this?
QSize sizeHint() const;//do I need this?
QSize minimumSizeHint() const;
QSize sizeHint() const;
protected:
//4.1 Core Functionality
......@@ -44,7 +44,7 @@ private:
//translate camera
QPoint clickStartPosition;
bool rightButtonPressed;
double tx, ty;
double translatex, translatey;
//rotate cube
bool leftButtonPressed;
......
varying vec3 Normal;
varying vec4 Vertex;
varying vec4 VertexColor;
uniform gl_MaterialParameters gl_FrontMaterial;
void main(void)
......@@ -10,31 +8,30 @@ 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));
//diffuse
float dot_LN = max(0.0,dot(L.xyz,N));
//specular
float n = gl_FrontMaterial.shininess;
float rdotv = max(dot(R,V.xyz),0);
float dot_rv_n = pow(rdotv,n);
float ks = pow(rdotv,shininess);
vec4 color = vec4(rdotv);
// gl_FragColor = color;
gl_FragColor = vec4(ks + 0.5f*kd);
vec4 kd = gl_FrontMaterial.diffuse;//color diffuse
vec4 ks = gl_FrontMaterial.specular;//color specular
// 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.
// gl_FragColor = VertexColor;//Pink.
vec4 color = dot_LN*kd*I_L_d + dot_rv_n*ks*I_L_s;
gl_FragColor = vec4(color);
}
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;
}
#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