PDA

View Full Version : About QGLWidget::renderText



showhand
18th January 2006, 08:39
Hi,all
Here is my problem about renderText(). As we all know,the QGLWidget int Qt is very convience for us to write OpenGL command. Now I have to annotate some text on the GL scence and I want to use renderText() like the following attached image(please click on it). Who can give me some good advices about it. Thanks a lot!

guilugi
18th January 2006, 11:46
Hello,

Well this method is quite simple to use, you just have to create a QString containing your text,
reshape it with a QFont....and place it in the scene :)

I see in your image that it must be vertical, so, you can use the second method, with 3 parameters for coordinates...

These coordinates are relative to the current transformations applied to the projection matrix, and you could do something like this :

glPushMatrix();
glRotatef(90, 0, 0, 1);
renderText(x, y, z, QString("My Text"), ...)
glPopMatrix();

I haven't written OpenGL for ages, so there may be some syntax errors :)

Does that help ?

Guilugi.

showhand
18th January 2006, 14:47
That's a good idear! Thank you very much! I'll try it at once. But now, I have another problem about renderText() and glCallList().Please look at my following code:

void NgiDrawWiggle::initializeGL()
{
z=(this->h/2)/(tan((45.0/2.0)*(3.1415926/180.0)))+0.5;
glShadeModel( GL_SMOOTH );
glClearColor( 1.0, 1.0, 1.0, 0.0 );
glClearDepth( 1.0 );
glDisable( GL_DEPTH_TEST );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
void NgiDrawWiggle::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCallList(objectRectRegion);
drawText("ffffffffffff");
glColor3ub(0,255,0);
renderText(170,140,"111");
glColor3ub(0,255,255);
renderText(100,150,"222");
drawText("ggggg");

}
void NgiDrawWiggle::resizeGL(int width,int height)
{
if ( height == 0 )
{
height = 1;
}
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 1000 );//
glMatrixMode( GL_MODELVIEW );
}
void NgiDrawWiggle::drawText(QString str)
{
glColor3ub(1,10,10);
this->renderText(10,10,str);
}
GLuint NgiDrawWiggle::drawRectRegion()
{
GLuint list = glGenLists(1);
glNewList(list,GL_COMPILE);
glLoadIdentity();
glTranslatef( -w/2, -h/2, -z );
glBegin(GL_LINES);
glVertex3f(w/10,5,0);
glVertex3f(9*w/10,5,0);
glVertex3f(9*w/10,5,0);
glVertex3f(9*w/10,7*h/10,0);
glVertex3f(9*w/10,7*h/10,0);
glVertex3f(w/10,7*h/10,0);
glVertex3f(w/10,7*h/10,0);
glVertex3f(w/10,5,0);
glEnd();
glEndList();
return list;
}
void NgiDrawWiggle::startDrawWiggle()
{
z=(this->h/2)/(tan((45.0/2.0)*(3.1415926/180.0)))+0.5; //弧度运算
objectRectRegion=drawRectRegion();
updateGL();
}
void main(void)
{
NgiDrawWiggle* p=new NgiDrawWiggle();
p->startDrawWiggle();
}
Question:Look at paintGL(),both drawText() do not work, so the scene show "111" and "222" at the valid position. But when I glCallList() at last(ie. all the drawText and renderText before glCallList), all the string can be shown.why? I'm very perplex about it. Thanks for your help!

guilugi
18th January 2006, 15:12
Well, maybe that's because you reset your matrix with glLoadIdentity() in your display list,
and it messes up coordinates for your renderText & drawText calls...

I'm sorry, but I have no time to test something now...

Good luck ;)

Guilugi.

showhand
19th January 2006, 01:44
Hi,thank you for your help firstly.This is still the first problem for me.Could you please give me detailed information about the second method of renderText(). The parameter "z" puzzle me .I have tried the method that you told me yesterday and the coordinates rotated indeed,but the direction of the text was still horizontal (ie. not vertiacal) and its position changed as the coordinates were rotated. Can you help me again,thank you!

guilugi
19th January 2006, 09:54
Well, 'z' is the third dimension of the space, the depth of your scene.

In the small code I posted earlier, I did a rotation around this z-axis, to have the x-axis vertical and the y-axis horizontal...to have the text rendered vertical.

That's strange it doesn't work...
Can you post me a full sample of code I could compile and debug ?

Guilugi.

showhand
20th January 2006, 08:52
Sure,following are complete code, plese help me !

draw.h
#include<QtOpenGL>
#include<QString>
class Draw:public QGLWidget
{
public:
Draw(QWidget* parent=0);
Draw(void);
void drawText(QString,int,int);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width,int height);
}
draw.cpp
#include "draw.h"
void NgiDrawWiggle::initializeGL()//called firstly
{
z=(this->h/2)/(tan((45.0/2.0)*(3.1415926/180.0)))+0.5;
glShadeModel( GL_SMOOTH );
glClearColor( 1.0, 1.0, 1.0, 0.0 );
glClearDepth( 1.0 );
glDisable( GL_DEPTH_TEST );
// glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
void NgiDrawWiggle::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3ub(0,10,250);
drawText("Time",-65,(50-7*h)/20.0);
}
void NgiDrawWiggle::resizeGL(int width,int height)
{
if ( height == 0 )
{
height = 1;
}
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 1000 );//
glMatrixMode( GL_MODELVIEW );
}
void Draw::drawText(QString str,int x,int y)
{
glLoadIdentity();
glTranslatef(30.0,30.0,-z);//"z" is the depth of the scene through the "z=(this->h/2)/(tan((45.0/2.0)*(3.1415926/180.0)))+0.5","h" is the height of the scene.
glRotatef(90,0,0,1);
renderText(x,y,0,str);
}

I really don't know what I should do in the drawText(),thanks for your helping. Would you please give the complete debugged code?By the way, can I use "show list" in which renderText() is included?

guilugi
23rd January 2006, 12:36
Okay,

So a did some modifications to your code in order to compile it...
Strangely, I can't see any string drawn...I have to check all the code but I can't do it now :)

Meantime, did you solve you problem ?

Guilugi.

showhand
24th January 2006, 02:21
Hi, thank you for your help these days. I have still not solve my problem. Would you please check my all code which I sent you some days ago. I was perplexed about the method. I will wait for your replying for ever. Would you mind telling me your email and I think I could send the message about other questions. Thank you !

guilugi
24th January 2006, 10:46
Sure,

(One of) my e-mail addresses : guiluge@hotmail.com
If you use MSN, you can add, me it will be easier to talk ;)

Good luck, I'll keep you in touch

Guilugi.

showhand
7th February 2006, 02:24
Hi, I'm so sorry for not keeping touch you because of some reasons these days. This is my MSN: ccc_xiaohan@hotmail.com. I'm afraid we have different time because you are in France and I am in China. But I'll try my best to keep touch with you as possible as I can.
Thank you.

fanat9
8th March 2006, 14:50
What's in the result, guys ?