Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - colori degli oggetti OpenGL
Forum - C/C++ - colori degli oggetti OpenGL

Avatar
Bonny (Member)
Expert


Messaggi: 437
Iscritto: 24/04/2009

Segnala al moderatore
Postato alle 12:04
Martedė, 23/10/2012
Salve ragazzi sto facendo degli esercizi con OpenGL. Non sono un esperto.
L'esercizio consiste nel disegnare dei rettangoli tenendo premuto il pulsante sinistro del mouse tipo Paint di Windows.
Fin qui tutto ok.
Mediante un menu l'utente puō scegliere un colore per il rettangolo.
Forse non ho capito bene come gestire i colori dei vari oggetti in una scena.
Posto il sorgente se qualcuno di voi cortesemente mi aiuta a a capire dove sbaglio mi sarebbe di grande aiuto.
Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #ifdef __APPLE__
  5. #include <GLUT/glut.h>
  6. #else
  7. #include <GL/glut.h>
  8. #endif
  9. /* colori possibili a scelta dal menu */
  10. GLfloat colors [] = {
  11.    0.1f, 0.0f, 0.0f, /*red*/
  12.    0.0f, 0.1f, 0.0f, /*green*/
  13.    0.0f, 0.0f, 0.1f, /*blue*/
  14.    0.255f, 0.255f, 0.0f, /*yellow*/
  15.    0.160f, 0.160f, 0.160f, /*gray*/
  16.    0.255f, 0.192f, 0.192f/*pink*/
  17. };
  18. /* indice display list*/
  19. GLuint listIndex;
  20. GLsizei wh = 500, ww = 500;
  21. int i;
  22. /* coordinate (x, y) angolo in alto a sinistra del rettangolo */
  23. int first_x, first_y;
  24. /* coordinate (x', y') angolo in basso a destra del rettangolo */
  25. int last_x, last_y;
  26. /* variazioni di (x', y') rispetto a (x, y) */
  27. int dx, dy;
  28. /* id selezionato dal menu */
  29. int idColor;
  30.  
  31. void init() {
  32.  
  33.    idColor = 0;
  34.    dx = dy = first_x = first_y = last_x = last_y = 0;
  35.    glViewport(0, 0, ww, wh);
  36.    glClearColor(0.0, 0.0, 0.0, 1.0);
  37.    //glColor3f(1.0, 0.0, 0.0);
  38.    glMatrixMode(GL_PROJECTION);
  39.    glLoadIdentity();
  40.    gluOrtho2D(0.0, (GLdouble) ww, 0.0, (GLdouble) wh);
  41.    glMatrixMode(GL_MODELVIEW);
  42.  
  43. }
  44.  
  45. /*
  46.  * disegna il rettangolo in fase di motion del mouse
  47.  */
  48. void drawSquare(int x0, int y0, int x1, int y1) {
  49.  
  50.    y0 = wh - y0;
  51.    y1 = wh - y1;
  52.    //calcolo variazione infinitesima rispetto alle
  53.    //coortėdinate di partenza
  54.    dx = x1 - x0;
  55.    dy = y1 - y0;
  56.  
  57.    glColor3fv(&colors[idColor]);
  58.    glBegin(GL_QUADS);
  59.    glVertex2f(x0, y0);
  60.    glVertex2f(x0 + dx, y0);
  61.    glVertex2f(x1, y1);
  62.    glVertex2f(x0, y0 + dy);
  63.    glEnd();
  64.    glFlush();
  65. }
  66.  
  67. /*
  68.  * premendo il tasto 'q' dalla tastiera calcella le display list
  69.  * e termina il programma
  70.  */
  71. void keyboard(unsigned char key, int x, int y) {
  72.    if (key == 'q') {
  73.       glDeleteLists(1, listIndex);
  74.       exit(0);
  75.    }
  76. }
  77.  
  78. /*
  79.  * repaint del rettangolo fino a quando non viene
  80.  * rilasciato il tasto sinistro del mouse
  81.  */
  82. void motion(int x, int y) {
  83.    glutPostRedisplay();
  84.    drawSquare(first_x, first_y, x, y);
  85. }
  86.  
  87. /*
  88.  *
  89.  */
  90. void mouse(int button, int state, int x, int y) {
  91.    /*
  92.     * se viene premito il pulsante sinistro del mouse
  93.     * memorizzo le coordinate ovvero le coordinate del vertice
  94.     * il alto a sinistra del rettangolo
  95.     */
  96.    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  97.       //printf("FIRST (%d, %d)\n", x, y);
  98.       first_x = x;
  99.       first_y = y;
  100.    }
  101.    /*
  102.     * .. dopo aver trascinato il mouse avendo tenuto premuto il pulsante
  103.     * sinistro e avendo rilasciato tale pulsante ..
  104.     */
  105.    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  106.  
  107.       //printf("LAST (%d, %d)\n", x, y);
  108.       last_x = x;
  109.       last_y = y;
  110.  
  111.       /*
  112.        * ..salvo la serie di comandi per disegnare il rettangolo il una
  113.        * display list, evitando cosi evitando il trasferimento dei dati (immediate mode)
  114.        */
  115.       glShadeModel(GL_SMOOTH);
  116.  
  117.       listIndex = glGenLists(1);
  118.       glNewList(listIndex, GL_COMPILE_AND_EXECUTE);
  119.  
  120.       first_y = wh - first_y;
  121.       last_y = wh - last_y;
  122.  
  123.       dx = last_x - first_x;
  124.       dy = last_y - first_y;
  125.  
  126.       glColor3fv(&colors[idColor]);
  127.       glBegin(GL_QUADS);
  128.       glVertex2f(first_x, first_y);
  129.       glVertex2f(first_x + dx, first_y);
  130.       glVertex2f(last_x, last_y);
  131.       glVertex2f(first_x, first_y + dy);
  132.       glEnd();
  133.  
  134.       glEndList();
  135.  
  136.    }
  137.  
  138. }
  139.  
  140. void reshape(GLsizei w, GLsizei h) {
  141.  
  142.    glMatrixMode(GL_PROJECTION);
  143.    glLoadIdentity();
  144.    gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
  145.    glMatrixMode(GL_MODELVIEW);
  146.    glLoadIdentity();
  147.  
  148.    glViewport(0, 0, w, h);
  149.  
  150.    ww = w;
  151.    wh = h;
  152. }
  153.  
  154. /*
  155.  * tiene traccia del colore scelto dal menu
  156.  */
  157. void menu(int id) {
  158.    idColor = id - 1;
  159. }
  160.  
  161. void display() {
  162.  
  163.    glClearColor(1.0, 1.0, 1.0, 0.0);
  164.    glClear(GL_COLOR_BUFFER_BIT);
  165.  
  166.    for (i = 1; i <= listIndex; i++) {
  167.       glCallList(i);
  168.    }
  169. }
  170.  
  171. int main(int argc, const char * argv[]) {
  172.    glutInit(&argc, argv);
  173.    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  174.    glutInitWindowSize(ww, wh);
  175.    glutInitWindowPosition(0, 0);
  176.    glutCreateWindow("paint");
  177.  
  178.    glutMotionFunc(motion);
  179.    glutMouseFunc(mouse);
  180.    glutReshapeFunc(reshape);
  181.    glutKeyboardFunc(keyboard);
  182.  
  183.    glutCreateMenu(menu);
  184.    glutAddMenuEntry("red", 1);
  185.    glutAddMenuEntry("green", 2);
  186.    glutAddMenuEntry("blue", 3);
  187.    glutAddMenuEntry("yellow", 4);
  188.    glutAddMenuEntry("gray", 5);
  189.    glutAddMenuEntry("pink", 6);
  190.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  191.  
  192.    init();
  193.  
  194.    glutDisplayFunc(display);
  195.    glutMainLoop();
  196.  
  197.    return 0;
  198. }


Ultima modifica effettuata da Bonny il 23/10/2012 alle 12:06
PM Quote