OpenGL doesn't draw lines
-
HI,
I've a QGLWidget that draws a grid of lines, and it works very well.
The problem is after the grid drawing process has finished:
infact, in my app, I'm listening for keyboard events and I've reimplemented the QGLWidget has showned below
@void MyGLWidget::keyPressEvent(QKeyEvent * e)
{
switch (e->key())
{
case Qt::Key_Escape:
qDebug()<<"Escape pressed!";
glLineWidth(6.0);
glColor3f (1,0,0);
glBegin (GL_LINES);
glVertex3f (0,0,0);
glVertex3f (6,6,6);
glEnd();
updateGL();
break;
default:
e->ignore();
}
}@but, on the console I can see that the escape press event is intercepted, but the new line that I want to draw doesn't appear! The drawing space shows only the grid, but no new lines!
What is the problem? Why the scene is not updated?
I'll show the QGLWidget code for a complete documentation of the problem:
@#include "myglwidget.h"/*
- Sets up the OpenGL rendering context, defines display lists, etc.
- Gets called once before the first time resizeGL() or paintGL() is called.
*/
void MyGLWidget::initializeGL()
{
//tutti i valori sono tra 0 e 1
glClearColor(0,0,0,0); //specifica il colore quando chiamerò glClear (nero)
angle_x=0;
angle_y=0;
angle_z=0;
scale=3;
}void MyGLWidget::resizeGL (int w, int h)
{
qDebug()<<"resizeGL called";
qDebug()<<"w:"<<w<<"h"<<h;double aspect; // Prevent a divide by zero, when window is too short // (you cant make a window of zero width) if(h == 0)//altura h = 1; // Set the viewport to be the entire window glViewport(0,0 , w, h); aspect=(double)w/(double)h; //Reset projection matrix stack glMatrixMode(GL_PROJECTION); // Reset the coordinate system before modifying glLoadIdentity(); double range = 100; if (w <= h) glOrtho(-(range),range,-range/aspect,range/aspect,-(range*2*scale),range*2*scale); else glOrtho(-(range*aspect),range*aspect,-range,range,-(range*2*scale),range*2*scale); glMatrixMode(GL_MODELVIEW); glLoadIdentity();
}
void MyGLWidget::keyPressEvent(QKeyEvent * e)
{
switch (e->key())
{
case Qt::Key_Escape:
qDebug()<<"Escape pressed!";
glLineWidth(6.0);
glColor3f (1,0,0);
glBegin (GL_LINES);
glVertex3f (0,0,0);
glVertex3f (6,6,6);
glEnd();
updateGL();
break;
default:
e->ignore();
}
}void MyGLWidget::paintGL()
{
qDebug()<<"paintGL called";
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glTranslatef(0,0,-6); //traslo tutti gli assi all'interno del viewing volume
//ruota la camera di 30° rispetto agli assi x,y,z
glRotatef( angle_x, 1.0f, 0.0f, 0.0f );
glRotatef( angle_y, 0.0f, 1.0f, 0.0f );
glRotatef( angle_z, 0.0f, 0.0f, 1.0f );
//prova scalatura
glScalef(scale, scale, scale); // Make the shape half as wide, the same height and twice as deepdrawAxis(); //disegno gli assi glLineWidth(5.0); glColor3f(0.0,0.0,0.0); glBegin(GL_QUADS); glVertex3f( 0,0, 0); glVertex3f( 0,100,0); glVertex3f(100,100,0); glVertex3f(100,0, 0); glEnd(); glLineWidth(1.0); glColor3f(0.0,1.0,0.0); glBegin(GL_LINES); for(int i=0;i<=100;i++) { glVertex3f(i,0,0); glVertex3f(i,100,0); glVertex3f(0,i,0); glVertex3f(100,i,0); }; glEnd(); //swapBuffers(); //scambia il buffer di backend, con quello di frontend (a vista)
}
void MyGLWidget::drawAxis()
{
float ORG[3] = {0,0,0};float XP[3] = {2,0,0}, YP[3] = {0,2,0}, ZP[3] = {0,0,2}; glLineWidth(6.0); glBegin (GL_LINES); glColor3f (1,0,0); glVertex3fv (ORG); glVertex3fv (XP); // X axis is red. glColor3f (0,1,0); glVertex3fv (ORG); glVertex3fv (YP); // Y axis is green. glColor3f (0,0,1); glVertex3fv (ORG); glVertex3fv (ZP); // z axis is blue. glEnd();
}
@ -
See this: http://8observer8.somee.com/QtLine.aspx
-
Hi,
AFAIK, calling gl code outside of xxxGL will not work. You should do all rendering inside paintGL.
What you could do is to update a list of pair of points (i.e QList<QPair< QPoint, QPoint>> and draw them in your paintGL function.
Hope it helps
-
Thank you so much, I've resolve the problem.
I've a question for you:
since I've put in the paintGL() method a Qlist<QVector3d> for store the vertex coords, if I have a lot a points in my drawing (like 50000), I'm going to use a lot of memory (ram)... but if I see the memory usage, it is constantly about 20 mb, notwithstanding there are a lot of points in QList<Qvector3d> list! How is possible? Has OpenGL a particulare storing method to prevent (i think so) the excessive memory usage?
my fear is also that having big, very big drawing, memory could crash...
[quote author="8Observer8" date="1378055998"]See this: http://8observer8.somee.com/QtLine.aspx[/quote] -
Thank you so mushììch, I've also premeditate to use a Qlist in order to storing vertexs, but my fear is about the memory usage... could it be excessive?
[quote author="SGaist" date="1378070317"]Hi,AFAIK, calling gl code outside of xxxGL will not work. You should do all rendering inside paintGL.
What you could do is to update a list of pair of points (i.e QList<QPair< QPoint, QPoint>> and draw them in your paintGL function.
Hope it helps[/quote]
-
Then you should have a look at the QGLBuffer class to store your vertices and draw them
-
Thank for the advice, but as I just read, this class does the same job of the QList: so, where is the advantage?
[quote author="SGaist" date="1378117757"]Then you should have a look at the QGLBuffer class to store your vertices and draw them[/quote]
-
It's a bit more complex than that. But it can indeed contain the same data as a QList in your case, but the idea is to have the all the vertices stored on the GPU side and let the GPU handle the drawing "in mass" rather than one point after the other.