habe folgendes Problem:
Ich möchte auf einem Würfel ein Bild auf alle seine Aussenseiten laden, der Würfel funktioniert ja und die Beleuchtung auch (nur die Farben, welche ja durch das Bild überdeckt werden sollen sind durch die Lichteinstellung nicht zu sehen). Das ganze will ich mit openGL machen, hier mal meine Klasse in der es eingebaut werden soll.
Der Würfel wird mit dem Aufruf von draw() erzeugt und da will ich ja dann das Bild (QImage) auf den Würfel packen.
Habe schon einige Dinge ausprobiert, daher ist einiges auskommentiert.
Wer kann mir da weiterhelfen?
Code: Alles auswählen
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QtGui>
#include <QtOpenGL>
#include <QWidget>
#include <QPainter>
#include <QtDebug>
#include <QMouseEvent>
#include <math.h>
#include <QKeyEvent>
#include <GL/glut.h>
#include "glwidget.h"
#include "glvector.h"
#include "gllight.h"
#include "settingsdialog.h"
#include "glperspective.h"
/**
* @author
*
*
*/
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget ( QWidget *parent = 0 );
virtual ~GLWidget();
void paintCoordinateAxes();
void keyPressEvent(QKeyEvent * ke);
protected:
void initializeGL();
void resizeGL ( int width, int height );
void paintGL();
void mousePressEvent ( QMouseEvent *event );
void mouseMoveEvent ( QMouseEvent *event );
private:
void draw();
GLfloat rotationX;
GLfloat rotationY;
GLfloat rotationZ;
GLuint texture[1];
QPoint lastPos;
GLPerspective * _Perspective;
GLLight * _Light1;
GLuint texture_id;
};
#endif
Code: Alles auswählen
#include "glwidget.h"
/**
* @author
*
*/
/** \brief constructor
*/
GLWidget::GLWidget ( QWidget *parent ) : QGLWidget ( parent )
{
setFormat ( QGLFormat ( QGL::DoubleBuffer | QGL::DepthBuffer ) );
rotationX = -21.0;
rotationY = -57.0;
rotationZ = 0.0;
//GLPerspective * _Perspective:
qDebug() << "create _Perspective";
_Perspective = new GLPerspective;
_Light1 = new GLLight();
setFocusPolicy ( Qt:: StrongFocus );
}
/** \brief initializeGL()
* Hintergrundfarbe auf rot(1.;0.;0.) oder weiß(1.;1.;1.) stellen mit glClearColor
*/
void GLWidget::initializeGL()
{
glShadeModel ( GL_SMOOTH );
glClearColor ( 1.0f, 1.0f, 1.0f, 0.5f );
glClearDepth ( 1.0f );
glEnable ( GL_DEPTH_TEST );
glDepthFunc ( GL_LEQUAL );
//glEnable ( GL_CULL_FACE );
glHint ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glEnable ( GL_TEXTURE_2D );
glGenTextures ( 1, &texture_id );
qDebug() <<texture_id;
if ( texture_id != 0 )
{
QImage image ( "src/bilder/wip_katana_3_render.jpg" );
if ( image.format() != QImage::Format_ARGB32 )
{
image = image.convertToFormat ( QImage::Format_ARGB32 );
image = QGLWidget::convertToGLFormat ( image );
}
glBindTexture ( GL_TEXTURE_2D, texture_id );
{
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits() );
}
glBindTexture ( GL_TEXTURE_2D, 0 );
}
}
void GLWidget::resizeGL ( int width, int height )
{
//Stellen Sie jetzt, wie in der Vorlesung gezeigt, die Viewport auf die Fenstergroesse ein:
glViewport ( 0,0,width,height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
//Richten Sie die Kamera mit gluLookAt aus:
//Stellen Sie mit gluPerspective die Kamera auf 45° Oeffnungswinkel, zur Viewport passendes
//Seitenverhaeltnis (aspect) near clippingplane auf 1.0, far clippingplane auf 10.0 ein:
//gluPerspective ( 45.0f, ( GLfloat ) width/ ( GLfloat ) height, 1.0f, 10.0f );
gluPerspective ( 45.0f, ( GLfloat ) width/ ( GLfloat ) height, 0.1f, 100.0f );
_Perspective->setViewport ( width,height );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
}
/** \brief paintGL()
* Aufruf von glClearColor()
* Als letzte Zeile in paintGL sollte im Folgenden immer glFlush(); vorhanden sein.
*/
void GLWidget::paintGL()
{
//glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//_Perspective->apply();
draw();
//glFlush()-Aufruf:
glFlush();
/*
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
_Perspective->apply();
paintCoordinateAxes();
glEnable ( GL_LIGHTING );
_Light1->switchOn();
glutSolidSphere ( 1.0,256,256 );
glDisable ( GL_LIGHTING );
glFlush();*/
}
/*------------------------------------------------------
Zeichnet die Koordinaten Achsen
------------------------------------------------------*/
void GLWidget:: paintCoordinateAxes()
{
QVector <GLVector> points ( 6 );
QVector<GLColorRGBA> colors ( 6 );
points[0] = GLVector ( 0.0,0.0,0.0 ); colors[0] = cl_Red;
points[1] = GLVector ( 3.0,0.0,0.0 ); colors[1] = cl_Red;
points[2] = GLVector ( 0.0,0.0,0.0 ); colors[2] = cl_Green;
points[3] = GLVector ( 0.0,3.0,0.0 ); colors[3] = cl_Green;
points[4] = GLVector ( 0.0,0.0,0.0 ); colors[4] = cl_Blue;
points[5] = GLVector ( 0.0,0.0,3.0 ); colors[5] = cl_Blue;
glViewport ( 0,0,width(),height() );
_Perspective->apply();
glColor3f ( 1.0,0.0,0.0 );
int stride = sizeof ( GLVector );
glEnableClientState ( GL_VERTEX_ARRAY );
glVertexPointer ( 3, GL_DOUBLE, stride, points[0].dv() );
glEnableClientState ( GL_COLOR_ARRAY );
glColorPointer ( 4,GL_FLOAT,sizeof ( GLColorRGBA ),colors[0].fv() );
glDrawArrays ( GL_LINES,0,6 );
glFlush();
}
void GLWidget::mousePressEvent ( QMouseEvent *event )
{
lastPos = event->pos();
}
void GLWidget::mouseMoveEvent ( QMouseEvent *event )
{
GLfloat dx = GLfloat (
event->x() - lastPos.x() ) / width();
GLfloat dy = GLfloat (
event->y() - lastPos.y() ) / height();
if ( event->buttons() & Qt::LeftButton )
{
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
}
else if ( event->buttons() & Qt::RightButton )
{
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL();
}
lastPos = event->pos();
}
void GLWidget::draw()
{
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//Model angeben
glLoadIdentity();
glEnable ( GL_LIGHTING );
_Light1->switchOn();
glTranslatef ( 0.0f,0.0f,-5.0f );
glRotatef ( rotationX,1.0f,0.0f,0.0f );
glRotatef ( rotationY,0.0f,1.0f,0.0f );
glRotatef ( rotationZ,0.0f,0.0f,1.0f );
rotationX+=0.4f; // X Achsen Rotation
rotationY+=0.6f; // Y Achsen Rotation
rotationZ+=0.1f; // Z Achsen Rotation
QVector <GLVector> points ( 6 );
points[0]=GLVector ( 0.0,0.0,0.0 );
points[1]=GLVector ( 1.0,0.0,0.0 );
points[2]=GLVector ( 0.0,0.0,0.0 );
points[3]=GLVector ( 0.0,1.0,0.0 );
points[4]=GLVector ( 0.0,0.0,0.0 );
points[5]=GLVector ( 0.0,0.0,1.0 );
QVector <GLVector> plan1 ( 4 );
plan1[0]=GLVector ( -1.0,-1.0,1.0 );
plan1[1]=GLVector ( 1.0,-1.0,1.0 );
plan1[2]=GLVector ( 1.0,1.0,1.0 );
plan1[3]=GLVector ( -1.0,1.0,1.0 );
int stride = sizeof ( GLVector );
glEnableClientState ( GL_VERTEX_ARRAY );
glVertexPointer ( 3, GL_DOUBLE, stride, plan1[0].dv() );
GLfloat texCords[] = {
0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0
};
glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer ( 2, GL_FLOAT, 0, texCords );
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glColor3f ( 1.0f,0.0f,0.0f );
QVector <GLVector> plan2 ( 4 );
glBindTexture(GL_TEXTURE_2D, texture_id);
{
glDrawArrays ( GL_QUADS, 0, 4 );
plan2[0]=GLVector ( -1.0,-1.0,-1.0 );
plan2[1]=GLVector ( -1.0,1.0,-1.0 );
plan2[2]=GLVector ( 1.0,1.0,-1.0 );
plan2[3]=GLVector ( 1.0,-1.0,-1.0 );
}
//glBindTexture( GL_TEXTURE_2D, 0);
glVertexPointer ( 3, GL_DOUBLE, stride, plan2[0].dv() );
glColor3f ( 1.2f,0.5f,0.0f );
glDrawArrays ( GL_QUADS, 0, 4 );
QVector <GLVector> plan3 ( 4 );
plan3[0]=GLVector ( -1.0,1.0,-1.0 );
plan3[1]=GLVector ( -1.0,1.0,1.0 );
plan3[2]=GLVector ( 1.0,1.0,1.0 );
plan3[3]=GLVector ( 1.0,1.0,-1.0 );
glVertexPointer ( 3, GL_DOUBLE, stride, plan3[0].dv() );
glColor3f ( 1.2f,0.5f,0.7f );
glDrawArrays ( GL_QUADS, 0, 4 );
QVector <GLVector> plan4 ( 4 );
plan4[0]=GLVector ( -1.0,-1.0,-1.0 );
plan4[1]=GLVector ( 1.0,-1.0,-1.0 );
plan4[2]=GLVector ( 1.0,-1.0,1.0 );
plan4[3]=GLVector ( -1.0,-1.0,1.0 );
glVertexPointer ( 3, GL_DOUBLE, stride, plan4[0].dv() );
glColor3f ( 1.2f,0.8f,0.9f );
glDrawArrays ( GL_QUADS, 0, 4 );
QVector <GLVector> plan5 ( 4 );
plan5[0]=GLVector ( 1.0,-1.0,-1.0 );
plan5[1]=GLVector ( 1.0,1.0,-1.0 );
plan5[2]=GLVector ( 1.0,1.0,1.0 );
plan5[3]=GLVector ( 1.0,-1.0,1.0 );
glVertexPointer ( 3, GL_DOUBLE, stride, plan5[0].dv() );
glColor3f ( 0.5f,0.5f,0.9f );
glDrawArrays ( GL_QUADS, 0, 4 );
QVector <GLVector> plan6 ( 4 );
plan6[0]=GLVector ( -1.0,-1.0,-1.0 );
plan6[1]=GLVector ( -1.0,-1.0,1.0 );
plan6[2]=GLVector ( -1.0,1.0,1.0 );
plan6[3]=GLVector ( -1.0,1.0,-1.0 );
glVertexPointer ( 3, GL_DOUBLE, stride, plan6[0].dv() );
glColor3f ( 0.2f,0.5f,0.9f );
glDrawArrays ( GL_QUADS, 0, 4 );
glVertexPointer ( 3, GL_DOUBLE, stride, points[0].dv() );
glColor3f ( 1.0f,0.0f,0.5f );
glDrawArrays ( GL_LINES, 0, 6 );
glDisable ( GL_LIGHTING );
}
/*------------------------------------------------------
Fängt die Tastatureingaben ab und führt sie aus.
------------------------------------------------------*/
void GLWidget:: keyPressEvent ( QKeyEvent * ke )
{
if ( ke->modifiers() == Qt::NoModifier )
{
switch ( ke->key() )
{
case Qt:: Key_Up : _Perspective -> moveCameraNorthSouth ( 1.0 ); break;
case Qt:: Key_Down : _Perspective -> moveCameraNorthSouth ( -1.0 ); break;
case Qt:: Key_Left : _Perspective -> moveCameraEastWest ( 1.0 ); break;
case Qt:: Key_Right : _Perspective -> moveCameraEastWest ( -1.0 ); break;
case Qt:: Key_PageUp : _Perspective -> multiplyDistance ( 0.99 ); break;
case Qt:: Key_PageDown : _Perspective -> multiplyDistance ( 1.0/0.99 ); break;
default : ke->ignore();
}
update();
}
else ke->ignore();
}
/** \brief destructor
*/
GLWidget::~GLWidget()
{
qDebug() << "delete _Perspective";
delete _Perspective;
}