Originally Posted by
fullmetalcoder
This is not a bad idea if you know how to handle the OpenGL-based 3D drawing but consider using the
QGraphicsScene object instead.
Jacek--> Can you please tell me how do I use QTranform as specified in the link above in qt4.2.2. I think QTranform should make work easier.
fullmetalcoder--> I have written a sample program can you please help me to represent a 2D object to 3D.
//GraphicsView
s->setSceneRect(-200, -200, 400, 400);
btnOPenGl->setGeometry( 300, 300, 50, 30 );
connect( btnOPenGl, SIGNAL( clicked() ), this, SLOT( onbtnOPenGlClick() ) );
}
graphicsView::~graphicsView()
{}
void graphicsView::onbtnOPenGlClick(){
GLWidget* GLwid = new GLWidget(this);
GLwid->show();
}
void graphicsView
::drawBackground(QPainter *painter,
const QRectF &rect
){ Q_UNUSED(rect);
paint(painter);
}
void graphicsView
::paint(QPainter* painter
){ static const QPoint points
[6] = { };
painter->drawPolygon(points, 6);
}
graphicsView::graphicsView(QGraphicsScene* s, QWidget *parent)
: QGraphicsView(s, parent), m_scene(s){
s->setItemIndexMethod(QGraphicsScene::NoIndex);
s->setSceneRect(-200, -200, 400, 400);
QPushButton* btnOPenGl = new QPushButton("OpenGL", this);
btnOPenGl->setGeometry( 300, 300, 50, 30 );
connect( btnOPenGl, SIGNAL( clicked() ), this, SLOT( onbtnOPenGlClick() ) );
}
graphicsView::~graphicsView()
{}
void graphicsView::onbtnOPenGlClick(){
GLWidget* GLwid = new GLWidget(this);
GLwid->show();
}
void graphicsView::drawBackground(QPainter *painter, const QRectF &rect){
Q_UNUSED(rect);
paint(painter);
}
void graphicsView::paint(QPainter* painter){
static const QPoint points[6] = {
QPoint(-30, -20),
QPoint(-70, 30),
QPoint(-30, 80),
QPoint(30, 80),
QPoint(70, 30),
QPoint(30, -20)
};
painter->drawPolygon(points, 6);
}
To copy to clipboard, switch view to plain text mode
//QGLWidget
#include <QGLWidget>
#include "graphicsview.h"
class graphicsView;
{
public:
GLWidget
(graphicsView
* gv,
QWidget *parent
=0 ) {}
protected:
paint.begin(this);
m_graphicsView->paint(&paint);
paint.end();
}
/*void initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void resizeGL( int width, int height )
{
height = height?height:1;
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
}*/
private:
graphicsView* m_graphicsView;
};
#include <QGLWidget>
#include "graphicsview.h"
class graphicsView;
class GLWidget : public QGLWidget
{
public:
GLWidget(graphicsView* gv, QWidget *parent=0 )
: QGLWidget( parent ), m_graphicsView(gv)
{}
protected:
void paintEvent( QPaintEvent* event ){
QPainter paint;
paint.begin(this);
m_graphicsView->paint(&paint);
paint.end();
}
/*void initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void resizeGL( int width, int height )
{
height = height?height:1;
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
}*/
private:
graphicsView* m_graphicsView;
};
To copy to clipboard, switch view to plain text mode
Thanks
Bookmarks