pastispast
8th October 2010, 18:15
Hi Friends,
I am getting problem using the render text function in openGL.
Actually I want to use openGL with QGraphicsView in my application. So I am using class QGraphicsItem to achieve this. I have used renderPixmap() function. But when I execute my function I got the text but soon there is a crash.
I have search across the forum and found that
"renderText is 8-bit and can only use "OpenGL compatible fonts".
So I have used the following code to do this:
QFont myFont("Verdana", 24);
myFont.setStyleStrategy(QFont.OpenGLCompatible);
renderText(20,20, 0,"test", myFont);
What can be cause of the problem...
I have also attached the code [Older one already posted in previous thread].
My QT version is "Qt by Nokia v4.7.0 (VS2008)"
john_god
11th October 2010, 00:13
What happens if you use
renderText(20,20, 0,"test");
pastispast
11th October 2010, 06:49
What happens if you use
renderText(20,20, 0,"test");
I have tried this["renderText(20,20, 0,"test")"] also and even renderText(20,20,"test"), but no result...again application crashed....
Is anything I am missing????
tbscope
11th October 2010, 13:05
Can you post the crash report?
pastispast
11th October 2010, 14:26
Can you post the crash report?
Hi tbscope,
Thanks for the reply...I have attached the crash report...[Image + Call Stack + Error Report]
530753085309
tbscope
11th October 2010, 15:25
I can't see a problem in the stack trace.
Can you post the full code please (make it minimal and compilable)?
pastispast
11th October 2010, 17:29
I can't see a problem in the stack trace.
Can you post the full code please (make it minimal and compilable)?
Once again thanks for the reply...
I have attached the code along with this mail, and also I have added the snippet of code for your reference...
//main.cpp
#include "openglsceneexample.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OpenGLSceneExample w;
w.show();
return a.exec();
}
//openglsceneexample.cpp
#include "openglsceneexample.h"
#include <QtGui>
#include <QGLWidget>
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// OPEN GL WIDGET
CEasyGLWidget::CEasyGLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers/*|QGL::AlphaChannel|QGL::HasOverlay|QGL::Rgba*/),parent)
{
m_pOGLObject = 0;
}
CEasyGLWidget::~CEasyGLWidget()
{
}
/*! \brief Draws a single circular or elliptical arc.
*
*\param cx Upper Left corner of the in x coordinates
*\param cy Upper Left corner of the in y coordinates
*\param r Radius of the arc.
*\param start_angle Start position in angles (radian)
*\param arc_angle Angle of the Arc in radian
*\param num_segments Point count, which represent the arc
*/
void CEasyGLWidget::DrawArc(GLdouble cx, GLdouble cy, GLdouble r, GLdouble start_angle, GLdouble arc_angle, int num_segments)
{
//Theta is calculated from the arc angle, the -1 bit comes from the fact that the arc is open
GLdouble theta = arc_angle / GLdouble(num_segments -1);
GLdouble tangetial_factor = tanf(theta);
GLdouble radial_factor = cosf(theta);
//start at the start angle
GLdouble x = r * cosf(start_angle);
GLdouble y = r * sinf(start_angle);
glBegin(GL_LINE_STRIP);
for(int i = 0; i < num_segments; i++)
{
glVertex2f(x + cx + r, y + cy +r); //, 0.5);
GLdouble tx = -y;
GLdouble ty = x;
x += tx * tangetial_factor;
y += ty * tangetial_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}
/*! \brief Draws a single line.
*
* \param[in] cStartPoint Start Point of the line to be drawn.
* \param[in] cDestPoint Destination Point of the line to be drawn.
*/
void CEasyGLWidget::DrawLine(QPointF cStartPoint, QPointF cDestPoint)
{
glBegin(GL_LINE_STRIP);
glVertex2f(cStartPoint.rx(), cStartPoint.ry());
glVertex2f(cDestPoint.rx(), cDestPoint.ry());
glEnd();
}
void CEasyGLWidget::initializeGL()
{
qglClearColor( QColor(90,125,164,255)); //set background color
qglColor(QColor(255,0,0,255));
//create a display list containing information about the object we want to display
m_pOGLObject = glGenLists(1);
glNewList(m_pOGLObject, GL_COMPILE);
DrawArc(0.0, 0.0, 30.0, 0.0, 2*PI, 60);
DrawLine(QPointF(0.0,0.0), QPointF(200.0,200.0));
glEndList();
//activate alpha values for transparent colors of the bitmaps
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0.1);
//set up the rendering process to use a particular shading model and rendering flags:
glShadeModel(GL_FLAT);
}
void CEasyGLWidget::resizeGL(int iWidth, int iHeight)
{
// Set size of the view port (display size)
glViewport(0,0,(int)MIN(iWidth,iHeight), (int)MIN(iWidth,iHeight)); // always use width=height
glMatrixMode(GL_PROJECTION); //select protection matrix
glLoadIdentity(); //reset matrix position to start
// Open gl always draws on the same matrix (width x height = fix defined hud size)
// the size is defined by the Ortho Matrix.
// This matrix is scaled to the destination size.
glOrtho(0, 400,400, 0, -1,1);
glMatrixMode(GL_MODELVIEW); //select model view (result) matrix
}
void CEasyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear background using background color
glLoadIdentity(); //reset matrix position to start
glTranslatef(0.0, 0.0, 0.0) ; // move object to dest position
glCallList(m_pOGLObject); // call the display list containing the rendering commands for the object
QFont myFont("Verdana", 24);
myFont.setStyleStrategy(QFont.OpenGLCompatible);
renderText(20,20, 0,"test", myFont);
}
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// ITEM
CEasyGraphicsItem::CEasyGraphicsItem(QWidget *parent)
{
m_OGLItem = new CEasyGLWidget();
m_OGLItem->resize(200,200);
m_Pixmap = m_OGLItem->renderPixmap(600,600);
}
CEasyGraphicsItem::~CEasyGraphicsItem()
{
}
void CEasyGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
QRectF rect(0.0,0.0,200.0,200.0);
//Works fine !!
//painter->drawArc(rect,0,16*360);
// Open gl element is not displayed!!!
//m_pPixmap = QPixmap::grabWidget(m_OGLItem,0,0,600,600);
m_Pixmap = m_OGLItem->renderPixmap(600,600);
painter->drawPixmap(0,0,600,600,m_Pixmap);
//m_Image = m_OGLItem->grabFrameBuffer();
//painter->drawImage(rect, m_Image);
}
QRectF CEasyGraphicsItem::boundingRect() const
{
return QRectF(0,0,600,600); //m_CurrWindowWidth,m_CurrWindowHeight);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////// MAIN Window
OpenGLSceneExample::OpenGLSceneExample(QWidget *parent)
: QMainWindow(parent)
{
// ui.setupUi(this);
m_pMainGraphicsScene = new QGraphicsScene();
m_pMainGraphicsScene->setSceneRect(QRectF(0,0,630,630)); // Dummy window size
m_pMainGraphicsView = new QGraphicsView(m_pMainGraphicsScene);
m_pMainGLWidget = new QGLWidget();
//m_pMainGraphicsView->setViewport(m_pMainGLWidget);
setCentralWidget(m_pMainGraphicsView);
// Add open gl item to view (not part of the scene!!) works fine!
/*m_OGLItem = new CEasyGLWidget(m_pMainGraphicsView);
m_OGLItem->resize(400,400);
m_pMainGraphicsScene->addWidget(m_OGLItem);
*/
// add graphics item which contain a open gl element
m_pItem = new CEasyGraphicsItem();
m_pMainGraphicsScene->addItem(m_pItem);
}
OpenGLSceneExample::~OpenGLSceneExample()
{
}
//openglsceneexample.h
#ifndef OPENGLSCENEEXAMPLE_H
#define OPENGLSCENEEXAMPLE_H
#include <QtGui>
#include <QGLWidget>
//#include "ui_openglsceneexample.h"
#define MIN(a,b) ((a < b)? a : b)
#define PI 3.14159265358979323846 /*!< constant PI */
class CEasyGLWidget : public QGLWidget
{
Q_OBJECT
public:
CEasyGLWidget(QWidget *parent = 0);
~CEasyGLWidget();
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void DrawArc(GLdouble cx, GLdouble cy, GLdouble r, GLdouble start_angle, GLdouble arc_angle, int num_segments);
void DrawLine(QPointF cStartPoint, QPointF cDestPoint);
private:
GLuint m_pOGLObject;
};
class CEasyGraphicsItem : public QGraphicsItem
{
//Q_OBJECT
public:
CEasyGraphicsItem(QWidget *parent = 0);
~CEasyGraphicsItem();
virtual QRectF boundingRect() const;
virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
private:
CEasyGLWidget *m_OGLItem;
QPixmap m_Pixmap;
QImage m_Image;
};
class OpenGLSceneExample : public QMainWindow
{
Q_OBJECT
public:
OpenGLSceneExample(QWidget *parent = 0);
~OpenGLSceneExample();
private:
//Ui::OpenGLSceneExampleClass ui;
QGraphicsScene *m_pMainGraphicsScene;
QGraphicsView *m_pMainGraphicsView;
QGLWidget *m_pMainGLWidget;
CEasyGraphicsItem *m_pItem;
CEasyGLWidget *m_OGLItem;
};
#endif // OPENGLSCENEEXAMPLE_H
5315
tbscope
11th October 2010, 18:21
Your problem is the use of m_pOGLObject and gl...list functions.
I don't know enough of OpenGl to help you with that. But I guess switching displays inside the paint function is the problem (if that is what glCallList does)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.