2017-09-18 2 views
1

Je suis en train de charger un fichier .obj de quelque chose que j'ai dessiné dans blender et le montrer sur mon interface graphique, mais je suis un peu perdu sur la façon de continuer. Certaines des documentations fournissent des informations très brèves sur certaines fonctions.openGL - chargement .obj et le dessin sur l'interface graphique échoue

C'est le tutorial que j'ai suivi en l'écrivant.

Je n'ai simplement aucune idée maintenant comment dessiner sur mon OpenGLWidget.

Ceci est mon code qui devrait -I montrer quelque chose En pensant mon IUG, mais il ne fait pas:

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    load_obj("/home/yalishanda/Desktop/car.obj", suzanne_vertices, suzanne_normals, suzanne_elements); 

    //enable all vertices attributes 
    for(int i=0;i<suzanne_vertices;i++) 
    { 
     glEnableVertexAttribArray(i); 
    } 

    GLuint buffer; 
    glBindBuffer(GL_ARRAY_BUFFER, buffer); 
    glVertexAttribPointer(
     0, 
     4, 
     GL_FLOAT, 
     GL_FALSE, 
     0, 
     0 
    ); 

    // glEnableVertexAttribArray(0); 
     GLuint buffer2; 
     glBindBuffer(GL_ARRAY_BUFFER, buffer2); 
     glVertexAttribPointer(
     0, 
     3, 
     GL_FLOAT,   // the type of each element 
     GL_FALSE,   // take our values as-is 
     0,     // no extra data between each position 
     0     // offset of first element 
    ); 

     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
     int size; 
     glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); 
     glDrawElements(GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0); 

     paintGL(); 

} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 


void MainWindow::load_obj(const char* filename, std::vector<QVector4D> &vertices, std::vector<QVector3D> &normals, std::vector<GLushort> &elements) 
{ 
    std::ifstream in(filename, std::ios::in); 
    if (!in) 
    { 
     std::cerr << "Cannot open " << filename << std::endl; exit(1); 
    } 

    std::string line; 
    while (std::getline(in, line)) 
    { 
     if (line.substr(0,2) == "v ") 
     { 
      std::istringstream s(line.substr(2)); 
      QVector4D v; 
      //s >> v.x; 
      //s >> v.y; 
      //s >> v.z; 
      //v.w = 1.0f; 
      float helpValue; 
      s>>helpValue; 
      v.setX(helpValue); 
      v.setY(helpValue); 
      v.setZ(helpValue); 
      v.setW(1.0f); 
      vertices.push_back(v); 
     } 
     else if (line.substr(0,2) == "f ") 
     { 
      std::istringstream s(line.substr(2)); 
      GLushort a,b,c; 
      s >> a; s >> b; s >> c; 
      a--; b--; c--; 
      elements.push_back(a); elements.push_back(b); elements.push_back(c); 
     } 
     else if (line[0] == '#') 
     { 
      /* ignoring this line */ 
     } 
     else 
     { 
      /* ignoring this line */ 
     } 
    } 

    normals.resize(vertices.size(), QVector3D(0.0, 0.0, 0.0)); 
    for (int i = 0; i < elements.size(); i+=3) 
    { 
     GLushort ia = elements[i]; 
     GLushort ib = elements[i+1]; 
     GLushort ic = elements[i+2]; 

     QVector3D difference = QVector3D::crossProduct(
     QVector3D(vertices[ib]) - QVector3D(vertices[ia]), 
     QVector3D(vertices[ic]) - QVector3D(vertices[ia])); 

     QVector3D normal = difference.normalized(); 
     normals[ia] = normals[ib] = normals[ic] = normal; 
    } 
} 

void MainWindow::initializeGL() 
{ 
    glClearColor(0,0,0,1); 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_LIGHTING); 
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); 
    glEnable(GL_COLOR_MATERIAL); 
} 

void MainWindow::paintGL() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    //draw all the vertices 
    // I guess that here I should select my openGL widget somehow and draw the vertices *on* it 
    glBegin(GL_VERTEX4_BIT_PGI); 
     for(int i=0; i<suzanne_vertices.size();i++) 
     { 
      glVertex3f(suzanne_vertices.at(i).x, suzanne_vertices.at(i).y, suzanne_vertices.at(i).z); 
     } 

    glEnd(); 
} 

Répondre

0

Je pense tout d'abord vous ne spécifiez pas l'appareil photo?

J'ai pris cet extrait du tutoriel,

glm::mat4 view = glm::lookAt(
    glm::vec3(0.0, 2.0, 4.0), // eye 
    glm::vec3(0.0, 0.0, 0.0), // direction 
    glm::vec3(0.0, 1.0, 0.0)); // up 
    glm::mat4 projection = glm::perspective(45.0f, 1.0f*screen_width/screen_height, 0.1f, 100.0f); 

OpenGL est une bibliothèque très douloureuse au début, il faut la canalisation d'être tout à fait correct pour rendre la bonne chose, autre part, il ne vous dira pas où est faux. Vous pouvez également désactiver le matériau et la lumière, et définir la couleur de rendu (par exemple vert) pour voir quelque chose au début, si votre matériau n'est pas configuré correctement, la lumière sera probablement noire tout aussi bien.

+0

en effet. Comment puis-je maintenant spécifier comment le dessiner sur mon widget? Cela me donne aussi une erreur "glm n'a pas été déclaré dans cette portée" – traducerad

+1

@traducerad Je dirais 1. mettre en place le port de vue, 2. dessiner quelque chose là (comme un simple triangle ou une ligne), 3. Importer le maillage, 4. faites le matériel. Prenez pas à pas, je ne pense pas que je peux élaborer trop en raison de la limite ici –