[SOLVED]GeomVertexReader odd return junk in C++

My problem was that i created a var v_data but there used var vdata which was defined in the .h file. It took me 3 years but i finally realized why self is important.

*** i keep the code here in case some one will find it usefull ***
but change vdata to v_data

Here is my code


////////////////////////////////////////////////////////////////////
//     Function: Pyro::Constructor
//       Access: Published
//  Description: Draws the geometry that is inside this node path into 
//               The pyro object.  This performs a similiar functions as 
//               Rigid body combiner
////////////////////////////////////////////////////////////////////
void Pyro::geometry(NodePath node){
    
    cout << "pyro::geometry\n";
    
    LVector4f color = LVector4f(1,1,1,1);
    // create tmp arrays
    LVector3f v1,v2,v3;
    LVector2f uv1,uv2,uv3;
    
    // process node
    NodePathCollection geom_collection = node.find_all_matches("**/+GeomNode");
    
    for(int i=0; i < geom_collection.get_num_paths(); i++ ){
        NodePath current_node_path = geom_collection.get_path(i);
        GeomNode* geomNode  = (GeomNode*)current_node_path.node();
        // process geom node
        for(int j=0; j<geomNode->get_num_geoms(); j++){
            //cout << "getting geom " << j << " of " << geomNode->get_num_geoms() << "\n";
            CPT(Geom) geom = geomNode->get_geom(j);
            // proess geom
            CPT(GeomVertexData) v_data = geom->get_vertex_data();
            cout << " vdata i got " << v_data << "\n";
            cout << "  rows= " << v_data->get_num_rows() << "\n";
            GeomVertexReader *prim_vertex_reader = new GeomVertexReader(vdata, "vertex");
            GeomVertexReader *prim_uv_reader = new GeomVertexReader(vdata, "texcoord");
                
            cout << "n prim " << geom->get_num_primitives() << "\n";
            for(int k=0; k <geom->get_num_primitives(); k++){
                //cout << "getting prim " << k << " of " << geom->get_num_primitives() << "\n";
                CPT(GeomPrimitive) prim = geom->get_primitive(k)->decompose();
                // process primitive
                v1.set(7,0,0);
                v2.set(2,0,0);
                v3.set(3,0,0);
                
                for(int p=0; p < prim->get_num_primitives();p++){
                    int s = prim->get_primitive_start(p);
                    int e = prim->get_primitive_end(p);
                    int indx_over = 0;
                    // process polygon
                    for(int idx=s; idx<e; idx++){
                        cout << "--" << indx_over << "\n";
                        cout <<"idx" << idx << "\n";
                        int vidx = prim->get_vertex(idx);
                        cout <<"vidx" << vidx << "\n";
                        prim_vertex_reader->set_row(vidx);
                        prim_uv_reader->set_row(vidx);
                        
                        LVecBase3f vb1,vb2,vb3;
                        
                        switch(indx_over){
                            case 0:
                                vb1 = prim_vertex_reader->get_data3f();
                                cout << vb1 << "?\n";
                                uv1 = prim_uv_reader->get_data2f();
                            break;
                            case 1:
                                vb2 = prim_vertex_reader->get_data3f();
                                cout << vb2 << "?\n";
                                uv2 = prim_uv_reader->get_data2f();
                            break;
                            case 2:
                                vb3 = prim_vertex_reader->get_data3f();
                                cout << vb3 << "?\n";
                                uv3 = prim_uv_reader->get_data2f();
                            break;
                        }
//                        // assign vertex into tmp array
//                        //cout << "r[" << prim_vertex_reader.get_data3f() << "]\n";
//                        LVector3f v = 
//                        v[indx_over].set(v.get_x(),v.get_y(),v.get_z()); 
//                        cout << "v[" << v[indx_over] << "]\n";
//                        v[indx_over] = current_node_path.get_relative_vector(node,v[indx_over]);
//                        cout << "v[" << v[indx_over] << "]\n";
//                        uv[indx_over] = LVector2f();
                        indx_over++;
                        
                        if (indx_over > 2) break;                  
                    }
                    cout << "v " << v1 <<"|"<< v2 <<"|"<< v3 << "\n";
                   //cout << "v " << v[0] <<"|"<< v[1] <<"|"<< v[2] << "\n";
                    //cout << "uv " << uv[0] <<"|"<< uv[1] <<"|"<< uv[2] << "\n";
                    //cout << ".";
                    // empty tmp array into the generator
                    tri(v1,color,uv1,
                        v2,color,uv2,
                        v3,color,uv3);
                }
            }
            
            delete prim_vertex_reader;
            delete prim_uv_reader;
        }
    }
    
}

sorry for all the mess. I been banging at it for about a day. My problem is that get_data3f and get_data2f return junk random uninitialized vectors that look almost like the vectors I set on top. So when i set couple of vectors on top i get them or some random close to 0 vector. I been up and down the source code of the reader and its equally confusing

////////////////////////////////////////////////////////////////////
//     Function: GeomVertexReader::get_data3f
//       Access: Published
//  Description: Returns the data associated with the read row,
//               expressed as a 3-component value, and advances the
//               read row.
////////////////////////////////////////////////////////////////////
INLINE const LVecBase3f &GeomVertexReader::
get_data3f() {
  nassertr(has_column(), LVecBase3f::zero());
  return _packer->get_data3f(inc_pointer());
}

_packer is GeomVertexColumn


////////////////////////////////////////////////////////////////////
//     Function: GeomVertexColumn::Packer::get_data3f
//       Access: Public, Virtual
//  Description: 
////////////////////////////////////////////////////////////////////
const LVecBase3f &GeomVertexColumn::Packer::
get_data3f(const unsigned char *pointer) {
  switch (_column->get_num_values()) {
  case 1:
    _v3.set(get_data1f(pointer), 0.0f, 0.0f);
    return _v3;

  case 2:
    {
      const LVecBase2f &v2 = get_data2f(pointer);
      _v3.set(v2[0], v2[1], 0.0f);
    }
    return _v3;

  default:
    switch (_column->get_numeric_type()) {
    case NT_uint8:
      maybe_scale_color(pointer[0], pointer[1], pointer[2]);
      return _v3;
      
    case NT_uint16:
      {
        const PN_uint16 *pi = (const PN_uint16 *)pointer;
        _v3.set(pi[0], pi[1], pi[2]);
      }
      return _v3;
      
    case NT_uint32:
      {
        const PN_uint32 *pi = (const PN_uint32 *)pointer;
        _v3.set(pi[0], pi[1], pi[2]);
      }
      return _v3;
      
    case NT_packed_dcba:
      {
        PN_uint32 dword = *(const PN_uint32 *)pointer;
        maybe_scale_color(GeomVertexData::unpack_abcd_d(dword),
                          GeomVertexData::unpack_abcd_c(dword),
                          GeomVertexData::unpack_abcd_b(dword));
      }
      return _v3;
      
    case NT_packed_dabc:
      {
        PN_uint32 dword = *(const PN_uint32 *)pointer;
        maybe_scale_color(GeomVertexData::unpack_abcd_b(dword),
                          GeomVertexData::unpack_abcd_c(dword),
                          GeomVertexData::unpack_abcd_d(dword));
      }
      return _v3;
      
    case NT_float32:
      {
        const PN_float32 *pi = (const PN_float32 *)pointer;
        _v3.set(pi[0], pi[1], pi[2]);
      }
      return _v3;
    }
  }

  return _v3;
}

I must be hitting some sort of bug in c++ but not in python because the python code that does the same thing works just fine.

mabe the

    LVecBase2f _v2;
    LVecBase3f _v3;
    LVecBase4f _v4;

decorations in GeomVertexColumn get messed up?

I think this is the reason why Panda3D code tends to mark class variables like “_vertex_data” and local variables with just “vertex_data”, to avoid confusion.

hehe python all over again. I will do that :slight_smile: