hello everybody i need some help cuz i’m at start with opengl. i just done an exercise which by dragging with left mouse clik it draws different rectangles! now, i have to do same think but by mouse clicking instead of mouse dragging! i’ll post here the code of the dragging exercize and i hope uyou understand that i’m trying to do for the next step
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <math.h>
#include<stdio.h>
#define N 500
GLsizei hgt = 700;
GLsizei wdt = 700;
//high left vertices coords
GLint vertices[N][2];
//low r
GLint fertices[N][2];
//vettore che contiene il codice dei colori
GLint colorType[N];
GLint my_menu;
GLint indi = 0;
GLint color = 4;
init(){
// imposto la viewport
glViewport(0,0,wdt,hgt);
// modalità proiezione
glMatrixMode(GL_PROJECTION);
// setto la matrice corrente con la matrice identita'
glLoadIdentity();
// imposta lo smooth shading
glShadeModel(GL_SMOOTH);
//setto lo sfondo nero
glClearColor(0.0,0.0,0.0,1.0);
}
void myReshape(GLsizei ww,GLsizei hh){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)hh);
glViewport(0,0,ww,hh);
wdt = ww;
hgt = hh;
}
//funzione che setta il colore a seconda della scelta effettuata dal menù
void chooseColor(int value){
switch(value){
case 1:
color = 1;
break;
case 2:
color = 2;
break;
case 3:
color = 3;
break;
case 4:
color = 4;
break;
}
glutPostRedisplay();
}
//drow rectangles
void drawImage(){
int i = 0;
while( i <= indi ){
if(colorType[i] == 0)
glColor3f(0.0,0.0,0.0);
if(colorType[i] == 1)
glColor3f(1.0, 0.0, 0.0);
if(colorType[i] == 2)
glColor3f(0.0, 0.0, 1.0);
if(colorType[i] == 3)
glColor3f(0.0, 1.0, 0.0);
if(colorType[i] == 4)
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2i(vertices[i][0],vertices[i][1]);
glVertex2i(fertices[i][0],vertices[i][1]);
glVertex2i(fertices[i][0],fertices[i][1]);
glVertex2i(vertices[i][0],fertices[i][1]);
glEnd();
i++;
}
}
//save start coords
void startVertex(x,y){
vertices[indi][0] = fertices[indi][0] = x;
vertices[indi][1] = fertices[indi][1] = hgt - y;
colorType[indi] = color;
}
//save bottom right coords
void painting(x,y){
fertices[indi][0] = x;
fertices[indi][1] = hgt - y;
glutPostRedisplay();
}
void myMouse(int button,int event,int x,int y){
if( button == GLUT_LEFT_BUTTON && event == GLUT_DOWN){
startVertex(x,y);
glutPostRedisplay();
}
if( button == GLUT_LEFT_BUTTON && event == GLUT_UP){
indi++;
}
}
//decrementa contatore e quindi numero di rettangoli e ne modifica i valori
void undo(unsigned char key,int x,int y){
if( key == 26 ){
indi--;
colorType[indi] = 0;
vertices[indi][0] = vertices[indi][1] = fertices[indi][0] = fertices[indi][1] = 0;
glutPostRedisplay();
}
}
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
drawImage();
glFlush();
glutSwapBuffers();
}
int main(int argc,char*argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(wdt,hgt);
glutInitWindowPosition(0,0);
glutCreateWindow("Points");
//richiamo funzione della tastiera
glutKeyboardFunc(undo);
//richiamo la funzione del mouse
glutMouseFunc(myMouse);
//richiamo la funzione dello cursore
glutMotionFunc(painting);
//ridisegno tutta la finestra
glutReshapeFunc(myReshape);
//richiamo la funzione display
glutDisplayFunc(myDisplay);
//creo il menu'
my_menu = glutCreateMenu(chooseColor);
glutAddMenuEntry("Red",1);
glutAddMenuEntry("Blue",2);
glutAddMenuEntry("Green",3);
glutAddMenuEntry("White",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//richiamo la funzione che setta la finestra
init();
//richiamo il loop per il disegno continuo
glutMainLoop();
return 0;
}
btw, functions that i didnt transalte in english it doesnt matter, there are functions that i dont need now , the most important think now is do a work code that draw rectangle by clickng top left vertice and bottom right vertice instead of dragging it! thank you