[Moved]Not able to use updateGL()
-
I am giving the whole code of this small program which I have written. It is very necessary to use the loop as my data is supposed to kept in the loops.
I do not want to use the qTimer() function ....
There is no output of the program. I do not know where do I keep the updateGL().
The program is only supposed to draw some squares in two different colors ....
I am using Netbeans IDE.
The GUI is shown but only with a black screen with the code.
Give me some advise on where I should make changes ......Please help .....
;( ;( ;( ;(@
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
#include <QtGui/QColor>
#include <QtOpenGL/qgl.h>
#include <iostream>
#include <time.h>
#include "Quad.h"int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);QApplication app(argc, argv); // create and show your widgets here Quad *w = new Quad(); w->show(); return app.exec();
}
// =================================================================================================
#include "Point.h"
class Quad : public QGLWidget {
Q_OBJECTprivate:
Point *Pt;
int total_objects;
protected:void InitializeGL() { // Set the way we want things to look glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); }; void resizeGL(int Height, int Width) { glViewport(Width, Height, 0, 0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); }; void paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); int counter; for(counter=0;counter<100;counter+=4){ if(Pt[counter].getcol()==true){ qglColor(Qt::red); glBegin(GL_QUADS); glVertex2f(Pt[counter].getsx(),Pt[counter].getsy()); glVertex2f(Pt[counter+1].getsx(),Pt[counter].getsy()); glVertex2f(Pt[counter+2].getsx(),Pt[counter].getsy()); glVertex2f(Pt[counter+3].getsx(),Pt[counter].getsy()); glEnd(); }else{ qglColor(Qt::white); glBegin(GL_QUADS); glVertex2f(Pt[counter].getsx(),Pt[counter].getsy()); glVertex2f(Pt[counter+1].getsx(),Pt[counter].getsy()); glVertex2f(Pt[counter+2].getsx(),Pt[counter].getsy()); glVertex2f(Pt[counter+3].getsx(),Pt[counter].getsy()); glEnd(); } }
// updateGL();
};public:
Quad(QWidget *parent = 0) { srand(time(NULL)); int counter; total_objects = 100; Pt = new Point[total_objects](); for (counter = 0; counter < 100; counter++) { Pt[counter].init((float) (rand() % 100), (float) (rand() % 100), ((rand() % 2) == 0 ? true : false)); } };
};
//===============================================================================================================================
class Point {
private:
float s_x;
float s_y;
bool col;
public:Point(float sx, float sy, bool co) { s_x = sx; s_y = sy; col = co; }; Point() {}; void init(float sx, float sy, bool co) { s_x = sx; s_y = sy; col = co; }; float getsx() { return s_x; }; float getsy() { return s_y; }; bool getcol() { return col; };
};
@ -
Hi chintu,
there are two problems.
Change resizeGL() to:
@
void resizeGL(int Height, int Width) {
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-200, 200, -200, 200, -1, 1);
glMatrixMode(GL_MODELVIEW);
};
@
OpenGL need a box for the scenario (glOrtho) and viewport was wrong.In paintGL(), your lines for drawing are wrong, change like this:
@
glVertex2f(Pt[counter+1].getsx(),Pt[counter+1].getsy());
@
As hint: you need counter+i in x and y coordinate!