QPainter break coordinate system in OpenGl - c++

I wanna select point on object by click
It successfully realized
this->camera()->convertClickToLine(point, orig, dir);
bool found;
selectedPoint = this->camera()->pointUnderPixel(point, found);
if (selectedName() >= 0) {
glColor3f(0.9f, 0.2f, 0.1f);
glBegin(GL_POINTS);
glVertex3f(selectedPoint.x, selectedPoint.y, selectedPoint.z);
glEnd();
}
example of selectable object:
glBegin(GL_TRIANGLES);
glColor3f(0.5,0,0);
glVertex3f(xmin,ymin,zmin);
glVertex3f(xmin + (xmax-xmin)/2,ymin+(ymax-ymin)/2, zmin+(zmax-zmin)/2);
glVertex3f(xmax,ymin,zmin);
glEnd();
But if i start using QPainter, selectedPoint change coords to smth wrong
QPainter painter(this);
painter.setPen(Qt::black);
painter.setFont(QFont("Helvetica", 8));
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.drawText(textPosX + 10, textPosY, text);
painter.setBrush(QBrush(Qt::black, Qt::SolidPattern));
painter.drawEllipse(QPoint(textPosX, textPosY), 2, 2);
painter.end();
what should i do?
my steps:
I draw box and object by OpenGl
I draw objects names by Qpainter
I draw point on object by click (doesn't work due to previous item, if i comment item 2, all work fine)

solved
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
/QPainter/
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();

Related

QWidget::repaint: Recursive repaint detected using pointers

I am getting the following errors:
QWidget::repaint: Recursive repaint detected.
QPainter::begin: A paint device can only be painted by one painter at a` time.
QPainter::beginNativePainting: Painter not active.
QPainter::setRenderHint: Painter must be active to set rendering hints.
QPainter::translate: Painter not active.
void Graficador::paintEvent(QPaintEvent *event){
QPainter painter;
painter.begin(this);
painter.beginNativePainting();
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(250, 250);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.2,0.4,0.3,1);
glClear(GL_COLOR_BUFFER_BIT);
std::vector<unsigned short> nodosaux;
glPushMatrix();
for (unsigned i=1;i<=automata->Dimension();i++)
{
nodosaux=(automata->operator [](i))->Nodos();
if((automata->operator [](i))->Estado()==0)
glColor3f(0.3,0.3,0.3);
else if((automata->operator [](i))->Estado()==1.0)
glColor3f(1.0,1.0,1.0);
else if((automata->operator [](i))->Estado()==2.0)
glColor3f(0.7,0.7,0.7);
glBegin(GL_POLYGON);
for(unsigned j=1;j<=nodosaux.size();j++)
{
glVertex2f(automata->Nodo(nodosaux[j-1]).first,
automata->Nodo(nodosaux[j-1]).second);
}
glEnd();
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
for(unsigned j=1;j<=nodosaux.size();j++)
{
glVertex2f(automata->Nodo(nodosaux[j-1]).first,
automata->Nodo(nodosaux[j-1]).second);
}
glEnd();
}
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_LINES);
glVertex2f(-500.0,0.0);
glVertex2f(500.0,0.0);
glVertex2f(0.0,-500.0);
glVertex2f(0.0,500.0);
glEnd();
glPopMatrix();
painter.endNativePainting();
painter.end();
}
(Sorry for the use of operator, I know that its not the proper use but I need to solve this problem first).
This is the paintEvent() of a QGLWidget. I had no problem until I started using a pointer of my class "automata". This class only have some vectors with the coordinates (x,y) of the vertices, so I have no idea why I have this problem.
The complete program has a big class System that has inside: a user interface, an automata and some other classes that are used for other tasks. The user interface has this QGLWidget inside, and the automata that I'm trying to use here is a pointer that points to the automata in the system class.
I'm passing that pointer like this:
void Cargar_automata(Automata_Celular* ac)
{
automata = new Automata_Celular();
automata =ac;
}
I have some other widgets but they are only buttons to manage files and timers.
When using an OpenGL widget in Qt all painting should be done in a member called paintGL(). You are using the paintEvent which is what is causing the error messages you are seeing.
So your code should look something like this:
void Graficador::paintGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.2,0.4,0.3,1);
glClear(GL_COLOR_BUFFER_BIT);
std::vector<unsigned short> nodosaux;
glPushMatrix();
for (unsigned i=1;i<=automata->Dimension();i++)
{
nodosaux=(automata->operator [](i))->Nodos();
if((automata->operator [](i))->Estado()==0)
glColor3f(0.3,0.3,0.3);
else if((automata->operator [](i))->Estado()==1.0)
glColor3f(1.0,1.0,1.0);
else if((automata->operator [](i))->Estado()==2.0)
glColor3f(0.7,0.7,0.7);
glBegin(GL_POLYGON);
for(unsigned j=1;j<=nodosaux.size();j++)
{
glVertex2f(automata->Nodo(nodosaux[j-1]).first,
automata->Nodo(nodosaux[j-1]).second);
}
glEnd();
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
for(unsigned j=1;j<=nodosaux.size();j++)
{
glVertex2f(automata->Nodo(nodosaux[j-1]).first,
automata->Nodo(nodosaux[j-1]).second);
}
glEnd();
}
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_LINES);
glVertex2f(-500.0,0.0);
glVertex2f(500.0,0.0);
glVertex2f(0.0,-500.0);
glVertex2f(0.0,500.0);
glEnd();
glPopMatrix();
}

How to rotate a computer drawing in 90 degrees

I have created a bunny and a duck image in my blank window. My plan was to display it twice: once by itself and another image like it rotated at a 90 degrees. I've tried creating the image a second time and turning the image by changing values, but was difficult and couldn't work at all. Which axes needs to be used to rotate an image in a plane and the right way to accomplish it.
void myInit(void){
glClearColor(1.0, 1.0, 1.0, 0); // the background is white
glColor3f(0.0f, 0.0f, 0.0f); // set drawing color
gluOrtho2D(0.0, (GLdouble) screenWidth, 0.0, (GLdouble) screenHeight);
}
void drawBunny(){
glClear(GL_COLOR_BUFFER_BIT);
// draw the outline of box (bunny)
glLineWidth(2);
glBegin(GL_LINE_LOOP);
glVertex2i(50,50);
glVertex2i(150,50);
glVertex2i(150,100);
glVertex2i(50,100);
glEnd();
//draw bunny tail
glLineWidth(1);
glBegin(GL_LINE_STRIP);
glVertex2i(50,50);
glVertex2i(50,35);//2nd wider top/bottom
glVertex2i(70,35);//1st- shrink tail left/right
glVertex2i(70,50);//1st- shrink tail left/right
glEnd();
// draw first ear
glBegin(GL_LINE_LOOP);
glVertex2i(175,85);
glVertex2i(175,100);
glVertex2i(150,100);
glVertex2i(150,85);
glEnd();
//draw second ear
glBegin(GL_LINE_LOOP);
glVertex2i(175,70);
glVertex2i(175,100);
glVertex2i(150,100);
glVertex2i(150,70);
glEnd();
// draw the head
glBegin(GL_LINE_LOOP);
glVertex2i(150,100);
glVertex2i(150,110);
glVertex2i(125,110);
glVertex2i(125,100);
glEnd();
// draw first feet
glBegin(GL_LINE_LOOP);
glVertex2i(110,60);
glVertex2i(110,75);
glVertex2i(30,75); //decrease value increase feet
glVertex2i(30,60);
glEnd();
//draw second feet
glBegin(GL_LINE_LOOP);
glVertex2i(50,100);
glVertex2i(50,85);
glVertex2i(30,85); //decrease value increase feet
glVertex2i(30,100);
glEnd();
//* draw eyes
glBegin(GL_LINE_LOOP);
glVertex2i(140,100);
glVertex2i(140,105);
glVertex2i(135,105);
glVertex2i(135,100);
glEnd();
glFlush();
}
int main (int argc, char** argv){
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowPosition(100,150); // set window position
glutInitWindowSize(screenWidth,screenHeight); // set window size
glutCreateWindow("House"); // create & open window
glutDisplayFunc(drawBunny); // register redraw function
myInit();
glutMainLoop(); // loop forever
}
write a display function which consist of 2 bunnies and rotate them :
void display()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
drawBunny();
glPushMatrix();
glRotatef(degreetoreturn,x,y,z); // Adjust parameters according to what you need
drawBunny();
glPopMatrix();
glutSwapBuffers();
}
Delete glClear() function and glFlush() functions from your drawBunny function. And finally in your main function change this line :
glutDisplayFunc(drawBunny); // register redraw function
to
glutDisplayFunc(display); // register redraw function

How to make a draggable object with OpenGL?

I have a code with which I generate a pawn in OpenGL. However, I want to make its parts draggable. My question is more of a general one, but here's the code for my pawn so that you get an idea of what I'm doing:
int main()
{
/* open gl initialization */
while(running())
{
glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3ub(0, 0, 0);
/* the basis of the pawn */
glPushMatrix();
glScalef(1.6, 1.6, 0.8);
glTranslatef(0.0, 0.0, -2.7 - offset);
drawSmoothUnityEllipsoidPatch(0, 2*M_PI, 0, M_PI /2 );
glPopMatrix();
/* upped ellipsoid */
glPushMatrix();
glScalef(0.8, 0.8, 0.15);
glTranslatef(0.0 - offset, 0.0, 6.0);
drawSmoothUnitySphere();
glPopMatrix();
/* lower ellipsoid */
glPushMatrix();
glScalef(1.2, 1.2, 0.15);
glTranslatef(0.0 - offset, 0.0, -10.0);
drawSmoothUnitySphere();
glPopMatrix();
/* the cone */
glPushMatrix();
glScalef(1.0, 1.0, 3.5);
glTranslatef(0.0 + offset, 0.0, -0.5);
drawSmoothUnityCone();
glPopMatrix();
/* the sphere on top of the pawn */
glPushMatrix();
glScalef(0.7, 0.7, 0.7);
glTranslatef(0.0, 0.0, 2.3 + offset);
drawSmoothUnitySphere();
glPopMatrix();
glfwTerminate();
return 0;
}
where offset is irrelevant. The functions drawSmoothUnity(shape) just draw a unity shape in the centre of the coordinate system. I want to te able to drag and drop any of these shapes in the visible area (800x600, my window-size).
You can use gluUnproject to map your cursor position from screen space into world space. By tracking the delta of the 3D world coordinates when the mouse button was first clicked to the current position (after dragging) this gives you the x,y&z values you should use to translate your object.
Also, it should be 'glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);'
This is kind of off the top of my head and is psuedocodish. This doesn't take into account any selection or any of that. So, clicking down and moving the mouse would move the object even if the object wasn't under the mouse cursor when you clicked. You'll obviously need to add mouse handlers.
glm::dvec3 original_position;//position of object when we start moving
glm::dvec3 world_anchor;//world space coordinates of where we left clicked
glm::ivec2 screen_anchor;//screen space coordinates of where we left clicked
Object object;
OnLButtonDown(int x, int y)//x,y = where we clicked
{
original_position = object.GetPosition();
screen_anchor = ivec2(x,y);//screen coords where we clicked
gluUnproject(x,y,0,modelview_matrix,projection_matrix,viewport,&world_anchor.x,
&world_anchor.y,&world_anchor.z);
}
OnMouseMove(int x, int y) //x,y = current mouse cursor position
{
if(left_button_down)
MoveObject(screen_anchor.x-x,screen_anchor.y-y);
}
}
MoveObject(int dx, int dy)//dx,dy = distance from current mouse position to screen_anchor
{
glm::dvec3 world_position;//current mouse position in world coordinates
gluUnProject(dx,dy,0,modelview_matrix,projection_matrix,viewport,&world_position.x,
&world_position.y,&world_position.z);
glm::dev3 world_delta = world_anchor-world_position;
object.SetPosition(original_position+world_delta);
}

Move 2D Object to a Point in OpenGL

How to move a 2D object in the direction of a point (not a GL_POINTS, but coordinates) using OpenGL?
For a better understanding of my code:
I've splited most of my code into different source codes, but this is the one that is actually creating the shapes and setting the scene:
void setupScene(int clearColor[]) {
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
//glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black.
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
// Set up the orthographic projection so that coordinates (0, 0) are in the top left.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
// Back to the modelview so we can draw stuff.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}
void drawScene() {
setupScene((int[]){250, 250, 250, 1});
triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);
glBegin(GL_QUADS);
glColor3f(RGB(80), RGB(80), RGB(80));
glPushMatrix();
glTranslatef(400, 400, 0);
glVertex2d(200, 100);
glVertex2d(100, 100);
glVertex2d(100, 200);
glVertex2d(200, 200);
glPopMatrix();
glEnd();
glutSwapBuffers(); // Send the scene to the screen.
}
void update(int value) {
glutPostRedisplay(); // Tell GLUT that the display has changed.
glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds.
}
You need to translate the modelview matrix. Assuming you're in modelview mode already:
glPushMatrix();
glTranslatef(x, y, z);
// Draw your shape
glPopMatrix();
[Edit]
#paddy: Something like this? I tried this but the square isn't moving.
pastebin.com/2PCsy5kC
Try explicitly selecting the modelview matrix. Your example does not tell us which mode it's currently in:
glSetMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(x, y, z);
// Draw your shape
glPopMatrix();
Normally at the beginning of your render you reset everything... So you enter the GL_PROJECTION mode, call glLoadIdentity() to reset it and set up your camera, then do this for the GL_MODELVIEW matrix as well.
Answer on the behalf of the OP:
Thanks #paddy, I was trying to understand the use of glTranslatef and came with the solution. Here is the working code, it will create a square at 100x100 and will move it until 400x200:
void setupScene(int clearColor[]) {
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
//glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black.
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
// Set up the orthographic projection so that coordinates (0, 0) are in the top left.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
// Back to the modelview so we can draw stuff.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}
int a = 100;
int b = 200;
int x = 100;
int y = 100;
void drawScene() {
setupScene((int[]){250, 250, 250, 1});
triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glColor3f(RGB(80), RGB(80), RGB(80));
glVertex2d(b, a);
glVertex2d(a, a);
glVertex2d(a, b);
glVertex2d(b, b);
glEnd();
glPopMatrix();
glutSwapBuffers(); // Send the scene to the screen.
}
void update(int value) {
if (x != 400 && y != 200) {
x += 4;
y += 2;
}
glutPostRedisplay(); // Tell GLUT that the display has changed.
glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds.
}

bug in opengl c++ code, probably related to cood system

I have put all the necessary files[temp link removed] if you need to have a look.
mavStar.exe is my program.
The function currently I‘m trying to debug is :
void drawOG()
{
int curr,right,back,bottom;
//Does NOT draw the right most,back most,bottom most layer at the moment
//Does NOT draw face between state 1 & 2
for(int z=0;z+1 < occupancyGrid->Nz; z++){
glPushMatrix();
for(int y=0;y+1 < occupancyGrid->Ny; y++){
glPushMatrix();
for(int x=0;x+1 < occupancyGrid->Nx; x++){
curr = occupancyGrid->M[x][y][z];
right = occupancyGrid->M[x+1][y][z];
back = occupancyGrid->M[x][y][z+1];
bottom = occupancyGrid->M[x][y+1][z];
drawCube(RIGHT_FACE,colorBetween(curr,right));
drawCube(BACK_FACE,colorBetween(curr,back));
drawCube(BOTTOM_FACE,colorBetween(curr,bottom));
glTranslatef (HALF_VOXEL_SIZE*2, 0.0, 0.0);
}
glPopMatrix();
glTranslatef (0.0, -HALF_VOXEL_SIZE*2, 0.0);
}
glPopMatrix();
glTranslatef (0.0, 0.0, -HALF_VOXEL_SIZE*2);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//mouse tracking
glRotatef(fYDiff, 1,0,0);
glRotatef(fXDiff, 0,1,0);
glRotatef(fZDiff, 0,0,1);
glScalef(fScale, fScale, fScale);
//draw model
glMatrixMode(GL_MODELVIEW);
drawOG();
printOpenGLError(); // Check for OpenGL errors
glutSwapBuffers();
}
There is a much easier way to draw the faces you want by using:
glPushMatrix();
glBegin(GL_QUADS);
//Draw the 16 vertices with their normals.
glEnd();
glPopMatrix();
For example if you want the front:
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glNormal3f(0.0,0.0,1.0);
glVertex3f( position[0], position[1], position[2]);
glNormal3f(0.0,0.0,1.0);
glVertex3f( position1[0], position1[1], position1[2]);
glNormal3f(0.0,0.0,1.0);
glVertex3f(position2[0], position2[1], position2[2]);
glNormal3f(0.0,0.0,1.0);
glVertex3f(position3[0], position3[1], position3[2]);
glEnd();
glPopMatrix();
Draw the faces on a piece of paper to figure out what values you need for position,position1,position2,position3,etc. Named them so for general purpose, it should be fairly easy to determine their coordinates.
If you want to give it a touch of flexibility you can create a Cube class and render only the faces for witch you have set a flag to be on. By using a class you gain lots of control on how you want your render-able object to be displayed (color,position,scale, etc).