0

I have created this code where a plane follow the mouse position.

#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
float objX = 100;
float objY = 100;
float objSize = 50;

void motion(int x, int y) {
  objX = x;
  objY = y;
}

void drawRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}
void drawStaticRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}
int c = 0;
void display() {
  glClearColor(0, 0, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  const double w = glutGet(GLUT_WINDOW_WIDTH);
  const double h = glutGet(GLUT_WINDOW_HEIGHT);
  glOrtho(0, w, h, 0, -1, 1);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  drawRect(objX, objY, objSize);

  glutSwapBuffers();
}
void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      drawStaticRect(objX, objY, objSize);
    }
      glutPostRedisplay();
}
int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  glutInitWindowSize(600, 600);
  glutCreateWindow("GLUT");
  glutDisplayFunc(display);
  glutMouseFunc(mouseClicks);
  glutPassiveMotionFunc(motion);
  glutIdleFunc(display);
  glutMainLoop();
  return 0;
}

When the left mouse button is pressed i want to draw the object in the actual position, and repeat the operation as many times i want.


void drawStaticRect(float x, float y, float size) {
  glPushMatrix();
  glTranslatef(x, y, 0.0f);
  glScalef(size, size, 1.0f);
  glBegin(GL_QUADS);
  glColor3ub(255, 255, 255);
  glVertex2f(-1, -1);
  glVertex2f(1, -1);
  glVertex2f(1, 1);
  glVertex2f(-1, 1);
  glEnd();
  glPopMatrix();
}

void mouseClicks(int button, int state, int x, int y) {
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
      drawStaticRect(objX, objY, objSize);
    }
      glutPostRedisplay();

My idea is when the event trigger i just call a function that generate a quads under the actual mouse position and call glutPostRedisplay() to update the scene.

  • 1
    The scene is completely redrawn in each frame. You must draw the objects in the display callback. Store the click positions in an array and draw the objects in a loop in `display`. – Rabbid76 Mar 12 '22 at 08:37
  • see [Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?](https://stackoverflow.com/a/20924609/2521214) on the basics of GUI (Its not OpenGL but that does not matter the important stuff is how to store and edit your scene) You can rewrite the C++ classes into structs – Spektre Mar 13 '22 at 07:12

0 Answers0