Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Loading objects from Blender by Qt OpenGL



  • Hello

    I have crash in the construction Scene::Scene() https://github.com/8Observer8/ObjLoader_v1_0_0

    You should run my application -> Click "Load" -> Select the file "Plane.observerFormat"

    I think that I cannot fill std::vector like this:

    @
    #include <iostream>
    #include <vector>

    void fill( std::vector<float> &vec1, std::vector<float> &vec2 );
    void show( const std::vector<float> &vec );

    int main()
    {
    std::vector<float> vec1;
    std::vector<float> vec2;
    fill( vec1, vec2 );

    show( vec1 );
    std::cout << "\n";
    show( vec2 );
    
    return 0;
    

    }

    void fill( std::vector<float> &vec1 , std::vector<float> &vec2 )
    {
    vec1.push_back( -0.5f );
    vec1.push_back( 0.5f );
    vec1.push_back( 1.5f );

    vec2.push_back( 0.5f );
    vec2.push_back( 0.5f );
    vec2.push_back( 1.5f );
    

    }

    void show( const std::vector<float> &vec )
    {
    for( unsigned int i = 0; i < vec.size(); ++i )
    {
    std::cout << vec[i] << "/n";
    }
    }
    @


  • Moderators

    This works fine: http://ideone.com/aXGV7Z

    But your real code is different and has an error:

    void Object::initColors( int numOfVertices ) {
        colors.clear();
        colors.resize( numOfVertices * 3 );
    
        for ( unsigned int i = 0, j = 0; i < numOfVertices; ++i ) {
            colors[j++] = 1.0f;
            colors[j++] = 0.0f;
            colors[j++] = 0.0f;
    
            colors[j++] = 0.0f;
            colors[j++] = 1.0f;
            colors[j++] = 0.0f;
    
            colors[j++] = 0.0f;
            colors[j++] = 0.0f;
            colors[j++] = 1.0f;
        }
    }
    

    Size of the vector is 3*N. i iterates from 0 to N and for each i you increment j 9 times, which means j reaches 9 * N, which is waaaay beyond the end of the vector.



  • Thank you very much. It works fine. Now I wrote like this:

    @for ( unsigned int i = 0, j = 0; i < numOfVertices / 3; ++i )@

    Source: https://github.com/8Observer8/CubeFromBlender_v1.0.1
    Picture: http://i9.pixs.ru/storage/2/1/3/SquareFrom_3854842_15269213.png

    The file "Cube.observerFormat" must be lie in the "currentPath" folder

    You can load objects from Blender like me

    Copy this script: https://yadi.sk/d/hvmi_wpxdXG5Y
    to C:\Program Files (x86)\Blender Foundation\Blender\2.72\scripts\addons

    Open Blender -> Press Ctrl+Alt+U -> Select "Addons" -> Select "Objects" -> Check "Object: SupPupExporter" -> Close the window "Blender User Preferences" -> Save Project (.blend file) -> Select "Object Mode" -> Click "Object" -> Select "SupPupExporter" -> You can find ".observerFormat" file ( it is where ".blend" lie)


  • Moderators

    I would argue that this is a lot easier to reason about:

    void Object::initColors( int numOfVertices ) {
        int size = numOfVertices * 3;
        colors.resize(size); //no need to clear if resizing anyway
    
        for (int i = 0; i < size; ) {
            colors[i++] = 1.0f;
            colors[i++] = 0.0f;
            colors[i++] = 0.0f;
    
            colors[i++] = 0.0f;
            colors[i++] = 1.0f;
            colors[i++] = 0.0f;
    
            colors[i++] = 0.0f;
            colors[i++] = 0.0f;
            colors[i++] = 1.0f;
        }
    }
    

    or if you're on c++11:

    void Object::initColors( int numOfVertices ) {
      auto size = numOfVertices * 3;
      colors.resize(size);
    
      std::vector<float> rgb = {1.0f,0.0f,0.0f, 0.0f,1.0f,0.0f, 0.0f,0.0f,1.0f};
      auto i = 0;
      std::generate(colors.begin(), colors.end(), [&]{return rgb[i++ % rgb.size()];});
    }


  • I was able to get the example running on Windows 8.1 by changing the code a little:

    Cube.cpp

    @//#include <GL/gl.h>
    #include <qopengl.h>@

    The Plane.observerFormat model didn't get drawn (and no error messages) but the Cube and some others already generated by me on Blender do work.

    Nice job!


Log in to reply