Results 1 to 5 of 5

Thread: Off topic C++ question, But its for use in OpenGL

  1. #1
    Senior Member Regular Contributor
    Join Date
    Sep 2001
    Location
    macedon, ny
    Posts
    288

    Off topic C++ question, But its for use in OpenGL

    Ok here is my issue.
    I am working on a way to load my models from a .txt file into my program to render them. The problem is, I dont know how many models there will be, nor do i know how many verts apiece they will be when they are finished.

    I would like to create a "for" loop to load my models into arrays, to be used by opengl to render with draw elements. Now obviously i can dynamicaly allocate arrays with the
    int *Array = new int[NumberOfVerts]; Where the number of verts is the first thing read from the .txt file. So the not knowing how many verts to put into the array is solved. The problem is creating an unknown number of arrays themselves.

    I would really like a way to dynamicaly create array names for the number of arrays needed. Something like this (obviously this dosnt work, or i would use it instead)....

    for(int x=0; x<NumberOfArrays; x++)
    {
    int *Array(x) = new int[NumVerts];
    }

    so if x=4; i would wind up with
    Array0;
    Array1;
    Array2;
    Array3;

    Anyone have ANY idea how one would so something like this. I have used sprintf before to do something similar with strings, but I dont think it will work with variables.........Thank you so much in advance.

  2. #2
    Senior Member Regular Contributor nickels's Avatar
    Join Date
    Feb 2000
    Location
    Colorado
    Posts
    284

    Re: Off topic C++ question, But its for use in OpenGL

    Code :
     
    #include <vector.h>
     
    typedef struct {
      float x,y,z;
    } vert;
     
    typedef struct {
      ....model crap
      vector<vert*> vertices;
    } model;
     
     
    main()
    {
       vector<model*> models;
       model *amodel;
       vert *avert;
     
       for (while models)
       {
           amodel = new model();       
    .... fill out model info
           models.push(amodel);
           for (verts in this model)
           {
              avert = new vert();
     ... fill out vert info
              amodel.vertices.push(avert);
           }
       }
    }
    STL would work great for this, or create your own linked list.

    [This message has been edited by nickels (edited 07-24-2002).]

  3. #3
    Senior Member Regular Contributor
    Join Date
    Sep 2001
    Location
    macedon, ny
    Posts
    288

    Re: Off topic C++ question, But its for use in OpenGL

    That looks like it would work well, but with my current setup I would REALLY like to stick with everyday arrays. Mainly because im not familiar with STL, and switching over my current code (which is QUITE) long, could turn out to be a very very arduous task. Is there any other way to do this with plan old arrays??

  4. #4
    Senior Member Regular Contributor
    Join Date
    Sep 2001
    Location
    macedon, ny
    Posts
    288

    Re: Off topic C++ question, But its for use in OpenGL

    Never mind; I think i found the answer. I will simply set a Max Number of possible items. Then create an array of pointers for that max number. Like this. int *Pointers[MaxNumber]; and allocate all i need with Pointer[x] = new int[Whatever];
    So thank you for helping anyways.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2001
    Posts
    469

    Re: Off topic C++ question, But its for use in OpenGL

    Originally posted by dabeav:
    That looks like it would work well, but with my current setup I would REALLY like to stick with everyday arrays. Mainly because im not familiar with STL, and switching over my current code (which is QUITE) long, could turn out to be a very very arduous task. Is there any other way to do this with plan old arrays??
    Hi

    well, when you dont try to use STL you won't get familiar You don't need to change all code at once. For example you can use nickels suggestions and fill all your data in a STL vector. When you are ready you just create some plain C arrays with the STL vectors size and fill them with the STL vectors data.

    When I firstly came in contact with STL I thought it would be crazy, but now I couldn't live without it

    Bye
    ScottManDeath
    P.S. I hope I pulled someone to the light side of force ;D

Similar Threads

  1. off topic - Volume Rendering question
    By Kal Mar in forum OpenGL: Advanced Coding
    Replies: 25
    Last Post: 12-04-2011, 02:06 AM
  2. research topic in opengl
    By dlynza in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-19-2003, 02:28 AM
  3. This is NOT off-topic : )
    By raverbach in forum OpenGL: Advanced Coding
    Replies: 26
    Last Post: 04-11-2003, 09:54 AM
  4. Question off topic
    By M/\dm/\n in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 12-12-2002, 12:59 PM
  5. MFC question (off topic a bit)
    By Siwko in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 08-09-2000, 06:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean