Commit 9af11d94 by Kai Westerkamp

Added Multiple Views

parent 46c09afb
#include "cubewidget.h"
CubeWidget::CubeWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
}
QSize CubeWidget::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize CubeWidget::sizeHint() const
{
return QSize(600, 400);
}
void CubeWidget::initializeGL ()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
tesselation = 0;
prepareShader();
// enable default shading
home();
showFlat();
glEnable(GL_LIGHTING);
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);
//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 2.0f);
}
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);
GLfloat white[] = {1.0,1.0,1.0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
glMaterialfv(GL_FRONT,GL_SPECULAR,white);
GLfloat mShininess[] = {128};
glMaterialfv(GL_FRONT,GL_SHININESS,mShininess);
}
void CubeWidget::paintGL ( )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMultMatrixf(translation->data());
QMatrix4x4 mat = QMatrix4x4();
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};
float increment = 1.0/(pow(2,tesselation));
setMaterial(red);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, 0.5f,y);
glVertex3f(x, 0.5f,y);
glVertex3f(x, 0.5f, y+increment);
glVertex3f( x+increment, 0.5f, y+increment);
}
}
glEnd();
setMaterial(magenta);
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, -0.5f,y+increment);
glVertex3f(x, -0.5f,y+increment);
glVertex3f(x, -0.5f, y);
glVertex3f( x+increment, -0.5f, y);
}
}
glEnd();
setMaterial(green);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y+increment, 0.5f);
glVertex3f(x, y+increment, 0.5f);
glVertex3f(x, y, 0.5f);
glVertex3f( x+increment, y, 0.5f);
}
}
glEnd();
setMaterial(yellow);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y, -0.5f);
glVertex3f(x, y,-0.5f);
glVertex3f(x, y+increment,-0.5f);
glVertex3f( x+increment,y+increment, -0.5f);
}
}
glEnd();
setMaterial(blue);
glBegin(GL_QUADS);
glNormal3f(-1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( -0.5f, x+increment,y+increment);
glVertex3f( -0.5f,x+increment,y);
glVertex3f( -0.5f,x, y);
glVertex3f( -0.5f,x, y+increment);
}
}
glEnd();
setMaterial(cyan);
glBegin(GL_QUADS);
glNormal3f(1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( 0.5f, x+increment,y);
glVertex3f( 0.5f,x+increment,y+increment);
glVertex3f( 0.5f,x, y+increment);
glVertex3f( 0.5f,x, y);
}
}
glEnd();
}
void CubeWidget::resizeGL(int width , int height )
{
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, width, height);
glLoadIdentity();
perspective(45.0,1.0*width/height,0.01,100.0);
glMatrixMode(GL_MODELVIEW);
}
void CubeWidget::perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}
void CubeWidget::showWireframe()
{
qDebug("show Wireframe");
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
shader->release();
updateGL();
}
void CubeWidget::showFlat()
{
qDebug("show Flat");
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
shader->release();
updateGL();
}
void CubeWidget::showGouraut()
{
qDebug("show Gouraut");
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
shader->release();
updateGL();
}
void CubeWidget::showPhong()
{
qDebug("show Phong");
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
shader->bind();
updateGL();
}
void CubeWidget::setTessellation(int t)
{
tesselation = t;
updateGL();
qDebug()<<"Tesselation" << t;
}
void CubeWidget::home()
{
translation = new QMatrix4x4();
translation->translate(0.0,0.0,-3.0);
rotation = new QQuaternion();
updateGL();
}
void CubeWidget::mousePressEvent(QMouseEvent *event )
{
lastSpeherePos = trackballPoint(event->pos().x(),event->pos().y());
lastScreenPos = new QPointF(event->screenPos());
event->accept();
}
void CubeWidget::mouseMoveEvent(QMouseEvent *event )
{
if (event->buttons() & Qt::LeftButton) {
rotate(trackballPoint(event->pos().x(),event->pos().y()));
event->accept();
} else if (event->buttons() & Qt::RightButton) {
move(new QPointF(event->screenPos()));
event->accept();
}
}
void CubeWidget::rotate(QVector3D *newPos )
{
QVector3D axis = QVector3D::crossProduct(*lastSpeherePos,*newPos);
//warum so besser
float angle = 180 / M_PI * asin(sqrt(QVector3D::dotProduct(axis, axis)));
axis.normalize();
//axis = rotation->conjugate().rotatedVector(axis);
QQuaternion newRot = QQuaternion::fromAxisAndAngle(axis, angle) * *rotation;
rotation = new QQuaternion(newRot.toVector4D());
lastSpeherePos = newPos;
updateGL();
}
void CubeWidget::move(QPointF * newPos){
QPointF dt = *newPos;
dt -= *lastScreenPos;
dt *= 0.01f;
float dx = dt.x();
float dy = dt.y()*-1.0;
translation->translate(dx,dy,0.0);
lastScreenPos = newPos;
updateGL();
}
QVector3D* CubeWidget::trackballPoint(int x, int y){
float xo,yo,zo;
// qDebug()<<"x:"<< x << " y:"<<y;
xo = ((2.0*x)-width())/ height();
yo = (height()-(2.0*y))/width();
float d = sqrt(xo*xo+yo*yo);
zo = qMax(qCos(M_PI_2*d),qreal(0.0)); //qMin(d,1.0f)
QVector3D *pos = new QVector3D(xo,yo,zo);
pos->normalize();
// qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return pos;
}
void CubeWidget::wheelEvent(QWheelEvent *event )
{
if(event->delta()<0)
//TODO
translation->translate(0.0,0.0,1.0);
else
translation->translate(0.0,0.0,-1.0);
updateGL();
}
#ifndef CUBEWIDGET_H
#define CUBEWIDGET_H
#include <QtGui>
#include <QtOpenGL>
#include <QGLFunctions>
#include <QOpenGLFunctions>
#include <math.h>
#include <QMatrix4x4>
#include <QQuaternion>
#include <QVector3D>
#include <algorithm>
class CubeWidget : public QGLWidget
{
Q_OBJECT
protected :
void initializeGL ( ) ;
void paintGL ( ) ;
void resizeGL(int width , int height ) ;
void mousePressEvent(QMouseEvent *event ) ;
void mouseMoveEvent(QMouseEvent *event ) ;
void wheelEvent(QWheelEvent *event ) ;
public:
CubeWidget(QWidget *parent = 0);
QSize minimumSizeHint() const;
QSize sizeHint() const;
public slots:
void showWireframe();
void showFlat();
void showGouraut();
void showPhong();
void home();
void setTessellation(int t );
private:
int tesselation;
int zoom;
QVector3D *lastSpeherePos;
QPointF * lastScreenPos;
QQuaternion *rotation;
QMatrix4x4 * translation;
QGLShaderProgram *shader;
void prepareShader();
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
void setMaterial(GLfloat *color );
void rotate(QVector3D *newPos );
void move(QPointF *newPos );
QVector3D *trackballPoint(int x, int y);
};
#endif // CUBEWIDGET_H
#include "glview.h"
GLView::GLView(Scene *scene,bool perspective)
{
this->persp = perspective;
this->scene = scene;
setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0));
}
QSize GLView::minimumSizeHint() const
{
return QSize(50, 50);
}
QSize GLView::sizeHint() const
{
return QSize(600, 400);
}
void GLView::initializeGL ( ) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
static GLfloat lightPosition[4] = { 0.5, 0.0, 2.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
//Shader Setup
initShader();
shader->bind();
}
void GLView::initShader()
{
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 GLView::setHome(QQuaternion *rotation, QVector3D *translation)
{
//qDebug()<<*rotation<<" trans:"<<*translation;
this->homeRotation = rotation;
this->homeTranslation = translation;
home();
}
void GLView::home()
{
this->rotation = homeRotation;
this->translation = homeTranslation;
}
void GLView::paintGL ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//set Projection
resizeGL(0,0);
glLoadIdentity();
//Camera Tranforations
QMatrix4x4 mat = QMatrix4x4();
mat.translate(*translation);
glMultMatrixf(mat.data());
// glRotated(45,0,1,0);
mat = QMatrix4x4();
mat.rotate(*rotation);
glMultMatrixf(mat.data());
//draw Scene
scene->draw();
}
void GLView::resizeGL(int width , int height )
{
glViewport(0, 0, this->width(), this->height());
GLdouble aspect = 1.0*this->width()/this->height();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(persp){
perspective(45.0,aspect,0.01,100.0);
} else {
int size = 1;
glOrtho(-size*aspect,size*aspect,-size,size,0.01,100.0);
}
glMatrixMode(GL_MODELVIEW);
}
void GLView::perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}
void GLView::mousePressEvent(QMouseEvent *event ) {}
void GLView::mouseMoveEvent(QMouseEvent *event ){}
void GLView::wheelEvent(QWheelEvent *event ) {}
#ifndef GLVIEW_H
#define GLVIEW_H
#include <QtGui>
#include <QtOpenGL>
#include <scene.h>
class GLView : public QGLWidget
{
Q_OBJECT
protected :
void initializeGL ( ) ;
void paintGL ( ) ;
void resizeGL(int width , int height ) ;
void mousePressEvent(QMouseEvent *event ) ;
void mouseMoveEvent(QMouseEvent *event ) ;
void wheelEvent(QWheelEvent *event ) ;
public:
GLView(Scene *scene,bool perspective = true);
void setHome(QQuaternion *rotation, QVector3D *translation);
QSize minimumSizeHint() const;
QSize sizeHint() const;
void rotate(QVector3D *newPos );
void move(QPointF *newPos );
public slots:
void home();
private:
bool persp;
QQuaternion *rotation;
QVector3D *translation;
QQuaternion *homeRotation;
QVector3D *homeTranslation;
QGLShaderProgram *shader;
Scene *scene;
void initShader();
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
};
#endif // GLVIEW_H
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-20T21:54:27
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hellocube
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
cubewidget.cpp \
glview.cpp \
scene.cpp
HEADERS += mainwindow.h \
cubewidget.h \
glview.h \
scene.h
RESOURCES += \
hellocube.qrc
DISTFILES += \
phong.frag \
phong.vert
<RCC>
<qresource prefix="/">
<file>img/cam_home.png</file>
<file>img/flat.png</file>
<file>img/gouraud.png</file>
<file>img/phong.png</file>
<file>img/wireframe.png</file>
<file>phong.frag</file>
<file>phong.vert</file>
<file>img/box.png</file>
<file>img/camera.png</file>
<file>img/cone.png</file>
<file>img/cylinder.png</file>
<file>img/select.png</file>
<file>img/sphere.png</file>
<file>img/torus.png</file>
<file>img/view-dual.png</file>
<file>img/viewports.png</file>
<file>img/view-single.png</file>
</qresource>
</RCC>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Hello Cube");
menuBar = new QMenuBar();
toolBar = new QToolBar("Shading",this);
statusBar = new QStatusBar(this);
//Views
scene = new Scene();
perspectiveView = new GLView(scene,true);
frontView = new GLView(scene,false);
frontView->setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0));
leftView = new GLView(scene,false);
leftView->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(0.0,1.0,0.0,90.0).toVector4D()), new QVector3D(0.0,0.0,-3.0));
topView = new GLView(scene,false);
topView->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(1.0,0.0,0.0,90.0).toVector4D()), new QVector3D(0.0,0.0,-3.0));
topSplit = new QSplitter(Qt::Horizontal,this);
bottomSplit = new QSplitter(Qt::Horizontal,this);
verticalSplit = new QSplitter(Qt::Vertical,this);
activeView = perspectiveView;
topSplit->addWidget(perspectiveView);
topSplit->addWidget(frontView);
bottomSplit->addWidget(leftView);
bottomSplit->addWidget(topView);
verticalSplit->addWidget(topSplit);
verticalSplit->addWidget(bottomSplit);
setCentralWidget(verticalSplit);
showQuad();
//showSingle();
//showDual();
//
//File Menu Actions
fileMenu = new QMenu("&File");
viewGoup = new QActionGroup(this);
viewMenu = new QMenu("View",this);
interactionGroup = new QActionGroup(this);
interactionMenu = new QMenu("Interaction",this);
exitAction = new QAction("E&xit",fileMenu);
exitAction->setShortcut(QKeySequence::Quit);
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
//Interaction Actions
cameraMode = new QAction("Camera",this);
cameraMode->setCheckable(true);
cameraMode->setIcon(QIcon(":/img/camera.png"));
cameraMode->setChecked(true);
// connect(cameraMode, SIGNAL(triggered()), mainWidget, SLOT(showWireframe()));
interactionGroup->addAction(cameraMode);
editMode = new QAction("Edit",this);
editMode->setCheckable(true);
editMode->setIcon(QIcon(":/img/select.png"));
// connect(editMode, SIGNAL(triggered()), mainWidget, SLOT(showWireframe()));
interactionGroup->addAction(editMode);
//View Actions
singleView = new QAction("Single View",this);
singleView->setShortcut(QKeySequence("1"));
singleView->setCheckable(true);
singleView->setIcon(QIcon(":/img/view-single.png"));
connect(singleView, SIGNAL(triggered()), this, SLOT(showSingle()));
viewGoup->addAction(singleView);
dualView = new QAction("Dual View",this);
dualView->setShortcut(QKeySequence("2"));
dualView->setCheckable(true);
dualView->setIcon(QIcon(":/img/view-dual.png"));
connect(dualView, SIGNAL(triggered()), this, SLOT(showDual()));
viewGoup->addAction(dualView);
quadView = new QAction("Quad View",this);
quadView->setShortcut(QKeySequence("4"));
quadView->setCheckable(true);
quadView->setChecked(true);
quadView->setIcon(QIcon(":/img/viewports.png"));
connect(quadView, SIGNAL(triggered()), this, SLOT(showQuad()));
viewGoup->addAction(quadView);
viewMenu->addAction(singleView);
viewMenu->addAction(dualView);
viewMenu->addAction(quadView);
//Other Actions
aboutAction = new QAction("About",menuBar);
connect(aboutAction,SIGNAL(triggered()),this,SLOT(showAboutBox()));
camHome = new QAction(QIcon(":/img/cam_home.png"),"Cam Home", toolBar);
connect(camHome, SIGNAL(triggered(bool)),activeView,SLOT(home()));
// Assemble Menus
fileMenu->addAction(exitAction);
menuBar->addMenu(fileMenu);
menuBar->addAction(aboutAction);
setMenuBar(menuBar);
//Assemble Tool Bar
toolBar->addAction(cameraMode);
toolBar->addAction(editMode);
toolBar->addSeparator();
toolBar->addAction(camHome);
toolBar->addSeparator();
QToolButton* toolButton = new QToolButton();
toolButton->setIcon(QIcon(":/img/viewports.png"));
toolButton->setMenu(viewMenu);
toolButton->setPopupMode(QToolButton::InstantPopup);
QWidgetAction* toolButtonAction = new QWidgetAction(this);
toolButtonAction->setDefaultWidget(toolButton);
toolBar->addAction(toolButtonAction);
addToolBar( toolBar);
statusBar->showMessage("Hello");
setStatusBar(statusBar);
}
void MainWindow::showSingle(){
perspectiveView->show();
frontView->hide();
leftView->hide();
topView->hide();
}
void MainWindow::showDual(){
perspectiveView->show();
frontView->show();
leftView->hide();
topView->hide();
}
void MainWindow::showQuad(){
perspectiveView->show();
frontView->show();
leftView->show();
topView->show();
}
MainWindow::~MainWindow()
{
}
void MainWindow::showAboutBox()
{
QMessageBox msgBox;
msgBox.setWindowTitle("About Hello Cube!");
msgBox.setText("Written by Kai Westerkamp");
msgBox.exec();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QActionGroup>
#include <QIcon>
#include <QMessageBox>
#include <QToolBar>
#include <QStatusBar>
#include <QSlider>
#include <QSplitter>
#include <glview.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QMenuBar *menuBar;
QMenu *fileMenu;
QAction *exitAction;
QMenu *interactionMenu;
QActionGroup *interactionGroup;
QAction *cameraMode;
QAction *editMode;
QMenu *viewMenu;
QActionGroup *viewGoup;
QAction *singleView;
QAction *dualView;
QAction *quadView;
QAction *aboutAction;
QAction *camHome;
QToolBar *toolBar;
QStatusBar *statusBar;
Scene* scene;
QSplitter *topSplit;
QSplitter *bottomSplit;
QSplitter *verticalSplit;
GLView *activeView;
GLView *perspectiveView;
GLView *frontView;
GLView *leftView;
GLView *topView;
void setActiveView();
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void showAboutBox();
void showSingle();
void showDual();
void showQuad();
};
#endif // MAINWINDOW_H
varying vec3 position;
varying vec3 normal;
void main(void)
{
vec3 N = normalize(normal);
vec3 L = normalize(gl_LightSource[0].position.xyz-position);
vec4 diffuse = vec4(max(dot(L,N),0.0));
vec4 Iamb = gl_FrontLightProduct[0].ambient;
vec4 Idiff = gl_FrontLightProduct[0].diffuse*diffuse;
Idiff = clamp(Idiff, 0.0, 1.0);
vec3 V = normalize(-position);
vec3 R = normalize(reflect(-L,N));
float specular = pow(max(dot(R,V),0.0),gl_FrontMaterial.shininess);//);
vec4 Ispec = gl_FrontLightProduct[0].specular * specular;
Ispec = clamp(Ispec, 0.0, 1.0);
vec4 color = (
Iamb
+ Idiff
+ Ispec
);
gl_FragColor = vec4(specular);
gl_FragColor = 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;
}
#include "scene.h"
Scene::Scene()
{
}
void Scene::draw()
{
drawCube();
}
void Scene::drawCube()
{
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};
float increment = 1.0/(pow(2,4));
setMaterial(red);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, 0.5f,y);
glVertex3f(x, 0.5f,y);
glVertex3f(x, 0.5f, y+increment);
glVertex3f( x+increment, 0.5f, y+increment);
}
}
glEnd();
setMaterial(magenta);
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, -0.5f,y+increment);
glVertex3f(x, -0.5f,y+increment);
glVertex3f(x, -0.5f, y);
glVertex3f( x+increment, -0.5f, y);
}
}
glEnd();
setMaterial(green);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y+increment, 0.5f);
glVertex3f(x, y+increment, 0.5f);
glVertex3f(x, y, 0.5f);
glVertex3f( x+increment, y, 0.5f);
}
}
glEnd();
setMaterial(yellow);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y, -0.5f);
glVertex3f(x, y,-0.5f);
glVertex3f(x, y+increment,-0.5f);
glVertex3f( x+increment,y+increment, -0.5f);
}
}
glEnd();
setMaterial(blue);
glBegin(GL_QUADS);
glNormal3f(-1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( -0.5f, x+increment,y+increment);
glVertex3f( -0.5f,x+increment,y);
glVertex3f( -0.5f,x, y);
glVertex3f( -0.5f,x, y+increment);
}
}
glEnd();
setMaterial(cyan);
glBegin(GL_QUADS);
glNormal3f(1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( 0.5f, x+increment,y);
glVertex3f( 0.5f,x+increment,y+increment);
glVertex3f( 0.5f,x, y+increment);
glVertex3f( 0.5f,x, y);
}
}
glEnd();
}
void Scene::setMaterial(GLfloat *color )
{
glMaterialfv(GL_FRONT,GL_AMBIENT,color);
GLfloat white[] = {1.0,1.0,1.0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
glMaterialfv(GL_FRONT,GL_SPECULAR,white);
GLfloat mShininess[] = {128};
glMaterialfv(GL_FRONT,GL_SHININESS,mShininess);
}
#ifndef SCENE_H
#define SCENE_H
#include <QtOpenGL>
#include <QGLFunctions>
#include <QOpenGLFunctions>
class Scene
{
public:
Scene();
void draw();
private:
void drawCube();
void setMaterial(GLfloat *color );
};
#endif // SCENE_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