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++ - Problema con texture mapping di OpenGL
Forum - C/C++ - Problema con texture mapping di OpenGL

Avatar
Bonny (Member)
Expert


Messaggi: 437
Iscritto: 24/04/2009

Segnala al moderatore
Postato alle 20:42
Venerdì, 14/12/2012
Salve ragazzi ho un problema con questo sorgente.. si tratta di un esercizio per imparare a gestire le texture di opengl. Voglio disegnare una sfera e con con una texture mapping della terra farla ruotare.. per poi aggiungere gli altri pianeti e simulare il sistema solare:
Questo è il codice
Codice sorgente - presumibilmente C++

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #ifdef __APPLE__
  5. #include <GLUT/glut.h>
  6. #else
  7. //#include <windows.h>
  8. #include <GL/glut.h>
  9. #endif
  10.  
  11. typedef struct {
  12.    unsigned long sizeX;
  13.    unsigned long sizeY;
  14.    unsigned char *data;
  15. } Image;
  16.  
  17. int ImageLoad(char *, Image *);
  18.  
  19. GLuint texName;
  20. Image tx;
  21. GLfloat angle;
  22.  
  23. void display(void) {
  24.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  25.    glColor3f(1.0, 1.0, 1.0);
  26.  
  27.    glLoadIdentity();
  28.    gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
  29.  
  30.    glPushMatrix();
  31.    
  32.    glRotated(angle, 0.0, 1.0, 0.0);
  33.    glEnable(GL_TEXTURE_GEN_S);
  34.    glEnable(GL_TEXTURE_GEN_T);
  35.    glBindTexture(GL_TEXTURE_2D, texName);
  36.    glutSolidSphere(0.5, 25, 25);
  37.    glDisable(GL_TEXTURE_GEN_T);
  38.    glDisable(GL_TEXTURE_GEN_S);
  39.  
  40.    glPopMatrix();
  41.  
  42.    glutSwapBuffers();
  43. }
  44.  
  45. void resize(int w, int h) {
  46.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  47.    glMatrixMode(GL_PROJECTION);
  48.    glLoadIdentity();
  49.    gluPerspective(60.0, 1.0, 0.1, 200.0);
  50.    glMatrixMode(GL_MODELVIEW);
  51.    glLoadIdentity();
  52. }
  53.  
  54. void idle() {
  55.    angle += 0.1;
  56.    glutPostRedisplay();
  57. }
  58.  
  59. void init(void) {
  60.  
  61.    glClearColor(0.0, 0.0, 0.0, 0.0);
  62.    glEnable(GL_DEPTH_TEST);
  63.    glEnable(GL_TEXTURE_2D);
  64.    ImageLoad("earthmap.bmp", &tx);
  65.    glGenTextures(1, &texName);
  66.    glBindTexture(GL_TEXTURE_2D, texName);
  67.  
  68.    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,tx.sizeX,tx.sizeY,
  69.            0,GL_BGR,GL_UNSIGNED_BYTE,tx.data);
  70.  
  71.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  72.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  73.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  74.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  75.  
  76.    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  77.    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  78.  
  79. }
  80.  
  81. int main(int argc, char ** argv) {
  82.  
  83.    glutInit(&argc, argv);
  84.    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  85.    glutInitWindowSize(500, 500);
  86.    glutInitWindowPosition(100, 150);
  87.    glutCreateWindow("Sistema Solare");
  88.    init();
  89.    glutDisplayFunc(display);
  90.    glutReshapeFunc(resize);
  91.    glutIdleFunc(idle);
  92.    glutMainLoop();
  93. }
  94.  
  95. int ImageLoad(char *filename, Image *image) {
  96.    FILE *file;
  97.    unsigned long size; // size of the image in bytes.
  98.    unsigned long i; // standard counter.
  99.    unsigned short int planes; // number of planes in image (must be 1)
  100.    unsigned short int bpp; // number of bits per pixel (must be 24)
  101.    unsigned char temp; // used to convert bgr to rgb color.
  102.  
  103.    // Make sure the file exists
  104.    if ((file = fopen(filename, "rb")) == NULL) {
  105.       printf("File Not Found : %s\n", filename);
  106.       return 0;
  107.    }
  108.    // Skip to bmp header
  109.    fseek(file, 18, SEEK_CUR);
  110.  
  111.    // read width
  112.    if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
  113.       printf("Error reading width from %s.\n", filename);
  114.       fclose(file);
  115.       return 0;
  116.    }
  117.    printf("Width of %s: %lu\n", filename, image->sizeX);
  118.    //read the height
  119.    if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
  120.       printf("Error reading height from %s.\n", filename);
  121.       fclose(file);
  122.       return 0;
  123.    }
  124.    printf("Height of %s: %lu\n", filename, image->sizeY);
  125.    // calculate the size (assuming 24 bpp)
  126.    size = image->sizeX * image->sizeY * 3;
  127.    // read the planes
  128.    if ((fread(&planes, 2, 1, file)) != 1) {
  129.       printf("Error reading planes from %s. \n", filename);
  130.       fclose(file);
  131.       return 0;
  132.    }
  133.    if (planes != 1) {
  134.       printf("Planes from %s is not 1: %u\n", filename, planes);
  135.       fclose(file);
  136.       return 0;
  137.    }
  138.    // read the bpp
  139.    if ((i = fread(&bpp, 2, 1, file)) != 1) {
  140.       printf("Error reading bpp from %s. \n", filename);
  141.       fclose(file);
  142.       return 0;
  143.    }
  144.    if (bpp != 24) {
  145.       printf("Bpp from %s is not 24: %u\n", filename, bpp);
  146.       fclose(file);
  147.       return 0;
  148.    }
  149.    // seek past the rest of the bitmap header
  150.    fseek(file, 24, SEEK_CUR);
  151.    // Read the data
  152.    image->data = (unsigned char *) malloc(size);
  153.    if (image->data == NULL) {
  154.       printf("Error allocating memory for colour-corrected image data");
  155.       fclose(file);
  156.       return 0;
  157.    }
  158.    if ((i = fread(image->data, size, 1, file)) != 1) {
  159.       printf("Error reading image data from %s.\n", filename);
  160.       fclose(file);
  161.       return 0;
  162.    }
  163.    fclose(file);
  164.  
  165.    return 1;
  166. }



Non capisco dove sbaglio ... perchè la sfera ruota e la texture non resta attaccata, se qualcuno di voi conosce l'argomento vorrei che mi indicasse dove sbaglio per favore.

Ultima modifica effettuata da Bonny il 14/12/2012 alle 20:42
PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 21:01
Venerdì, 14/12/2012
Non usare glRotated se stai usando dei GLfloat... usa glRotatef.

A parte questo, sei sicuro che la sfera stia effettivamente ruotando? Come fai a saperlo (e' una sfera...)?



Il mio blog: https://piero.dev
PM Quote
Avatar
Bonny (Member)
Expert


Messaggi: 437
Iscritto: 24/04/2009

Segnala al moderatore
Postato alle 21:37
Venerdì, 14/12/2012
Se provi a commentare le righe 33 34 35 e 37 38 cioè togli la texture dalla sfera su vede che ruota..

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 16:35
Sabato, 15/12/2012
Non sono un esperto di OpenGL, ma prova a sostituire con questo:

Codice sorgente - presumibilmente Plain Text

  1. glRotatef(angle, 0.0, 1.0, 0.0);
  2.   gl.glBindTexture(GL_TEXTURE_2D, texName);
  3.   gl.glEnable(GL_TEXTURE_2D);
  4.   gl.glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  5.   gl.glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  6.   gl.glEnable(GL_TEXTURE_GEN_S);
  7.   gl.glEnable(GL_TEXTURE_GEN_T);
  8.   glutSolidSphere(0.5, 25, 25);
  9.   glDisable(GL_TEXTURE_GEN_T);
  10.   glDisable(GL_TEXTURE_GEN_S);
  11.   gl.glDisable(GL_TEXTURE_2D);



Il mio blog: https://piero.dev
PM Quote
Avatar
Bonny (Member)
Expert


Messaggi: 437
Iscritto: 24/04/2009

Segnala al moderatore
Postato alle 0:35
Lunedì, 17/12/2012
Grazie per le risposte, alla fine ho risolto in questo modo:
Codice sorgente - presumibilmente C/C++

  1. GLUquadricObj *sfera;
  2.  
  3. //..... ....... ........ ........
  4.  
  5. void init(){
  6.  
  7.    glClearColor(0.0, 0.0, 0.0, 0.0);
  8.    glEnable(GL_DEPTH_TEST);
  9.    glEnable(GL_TEXTURE_2D);
  10.  
  11.    //reader delle bitmap e creazione delle texture per ogni pianeta
  12.  
  13.    Image img;
  14.  
  15.    ImageLoad(TERRA_BMP, &img);
  16.    glGenTextures(1, &idTerra);
  17.    glBindTexture(GL_TEXTURE_2D, idTerra);
  18.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  19.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  20.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  21.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  22.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  23.    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.sizeX, img.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data);
  24.  
  25.    //configura glut
  26.    sfera = gluNewQuadric();
  27.    gluQuadricTexture(sfera, GL_TRUE); //abilita UV map
  28.    gluQuadricDrawStyle(sfera, GLU_FILL); //disegna "solido"
  29.    gluQuadricNormals(sfera, GLU_SMOOTH); // normali per vertice (luci)
  30.    
  31. }
  32.  
  33. // .......  ....... ............
  34.  
  35. void display(){
  36.  
  37.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  38.    glColor3f(1.0, 1.0, 1.0);
  39.  
  40.    glLoadIdentity();
  41.    gluLookAt(0, 0, 3.5, 0, 0, 0, 0, 1, 0);
  42.  
  43.    glPushMatrix();
  44.    glRotatef(TerraAngle, 0.0, 1.0, 0.0);
  45.    glTranslatef(0.2, 0.0, 0.0);
  46.    glBindTexture(GL_TEXTURE_2D, idterra);
  47.    gluSphere(sfera, 0.05f, 32, 16);
  48.    glPopMatrix();
  49.  
  50.    glutSwapBuffers();
  51. }



Generando la sfera con GLUquadricObj  funziona.. a presto:)

PM Quote