Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Dynamically Allocated Vertex Arrays

  1. #1
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Dynamically Allocated Vertex Arrays

    Alright, well, i'm halfway through writing my .ase -> .dans file converter =P... Right now my program reads in Verticies, Faces, Face Normals, and Vert-Normals from an .ase file. Now what i want to be able to do is write a .dans (my own file format) file that has what i want... (the .cpp structure to draw the model). In this new file format i define a model... model has verts, faces, normals... And i use
    #define float vect[3];
    model->verts = new vect3 [numvertices]
    now the problem is, normally when i write code, if i make a vertex array, i use
    static float verticies[20][3] = {{x,y,z}, {x,y,z}....} for the 20 or so verticies i have. But the problem here is, this is being put into a class (model). And this model is being dynamically defined at the beginning of the file by a "new" command. How can i define a whole array at one time?
    Is it something like:
    model->verts[][] = {{x,y,z}, {x,y,z},...}
    ???
    just leave those brackets blank?
    Thanks a bunch for all the help up to here on my program, and Thanks in advance just for reading to here! =P
    Thanks!
    Dan

  2. #2
    Member Contributor
    Join Date
    Nov 2002
    Posts
    96

    Re: Dynamically Allocated Vertex Arrays

    That's a good one.... I don't think you can fill a dynamic array in one go. You can only give it a value at declaration. Note, my book doesn't tell explicitly this isn't possible, but it only mentions this CAN be done at declaration. (no mention of such a definition)

    Still, I don't think this should be an issue, since you are using it in a fileloader anyway, so I guess you fill the array in a loop.

    [This message has been edited by Structural (edited 12-12-2002).]

  3. #3
    Guest

    Re: Dynamically Allocated Vertex Arrays

    i dont know if this helps but..

    int sqrs[][2] =
    {
    1,1
    2,4
    3,9
    4,16
    };

    is an example in my book.

  4. #4
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Re: Dynamically Allocated Vertex Arrays

    But the problem is, i want it to write a code that i just need to link to later... so my loader to drop the whole c++ code to draw it.. make arrays from itself... (kinda)
    the problem is, i have to use a "new" command so, its declared first, THEN i want to set the whole array, in one go..
    anyone?
    beuler? beuler? beuler?

  5. #5
    Senior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Belgium
    Posts
    183

    Re: Dynamically Allocated Vertex Arrays

    You can only use brackets to initialize on stack arrays, example:

    int array[] = { 1, 2, 3, 4 };

    When using dynamic arrays (or on stack arrays using non-constant values) you have to use something else such as loops.

    But note that if you want to convert a model directly into C++ code then you can use the on-stack system. But this isn't such a good idea with large models.

    Why do you want to make such a converter anyway? Wouldn't it be easier to have your program loading the .ase files at runtime?

  6. #6
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Re: Dynamically Allocated Vertex Arrays

    im loading in HUGE models... or well, not huge, just really complex... (ex: a soccer ball, it has 72201 verts, and like 140,000 faces... plus the normals) when i run it to bring in all that, it takes like 2-2.5 min to load just the verts... if it converts, i can then just link to a .cpp doc to draw it...
    or am i way off base on this one?

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Belgium
    Posts
    183

    Re: Dynamically Allocated Vertex Arrays

    I think you should try to improve your loader instead of trying to convert your model into cpp files. The later solution isn't really flexible.

    You should try to make those 2-2.5 mins taken by your loader more like 2-2.5 seconds.

  8. #8
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Re: Dynamically Allocated Vertex Arrays

    If i post my code up here, is it possible for u to look over it? mabey tell me where my laggyness comes in?

  9. #9
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Re: Dynamically Allocated Vertex Arrays

    ***(HEADER FILE)***(ase.cpp)
    #include <math.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    #define NUM_VERTEX "*MESH_NUMVERTEX"
    #define NUM_FACES "*MESH_NUMFACES"
    #define VERTEX "*MESH_VERTEX"
    #define MESHFACE "*MESH_FACE"
    #define VERTNORM "*MESH_VERTEXNORMAL"
    #define FACENORM "*MESH_FACENORMAL"

    typedef float vect1;

    typedef vect1 vect3[3];

    typedef struct
    {
    int vertIndex[3]; // pt a,pt b,pt c,
    int edgeflag[3]; //edge a-b,edge b-c,edge c-a
    int coordIndex[3]; // indicies for the tex coords to texture this face
    vect3 normal[4]; // normal for each vertex and face(in vector form).
    } triangle_t; // normal[1] is face, 2-pt a, 3-pt b, 4-pt c

    typedef struct
    {
    int numVertex; // number of verts in the model
    int numFaces; // number of faces in the model
    int numTexFaces; // number of textured faces in model
    int numTexVertex; // number of texture coords
    int normals; // do we have normal information?
    vect3 *verts; // model verts
    vect3 *texVerts; // texture coords
    vect3 *vertNorms; // vertex normals
    triangle_t *faces; // faces of model
    //texture_t texture; // texture information
    } model;


    ***(RUN FILE)***
    #include <math.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //#include <glut.h>
    #include "ase.cpp"

    model incmodel;

    bool aseGetInfo(FILE *s, model *p){ //Get # of verticies, and
    rewind (s); //define array sizes accordingly
    char info[255];
    while (!feof (s)){
    fscanf(s, "%s", &info);
    if (!strcmp (info, NUM_VERTEX)){
    fscanf(s, "%i", &p->numVertex);
    cout << NUM_VERTEX << " " << p->numVertex << endl;
    p->verts = new vect3 [p->numVertex];
    }
    else if (!strcmp (info, NUM_FACES)){
    fscanf(s, "%i", &p->numFaces);
    cout << NUM_FACES << " " << p->numFaces << endl;
    p->faces = new triangle_t [p->numFaces];
    }
    }
    return 1;
    }

    void aseGetVertex(FILE *s, model *p){ //Get each individual vertex
    rewind (s); //from the file.
    char info[255];
    int index;
    while (!feof (s)){
    fscanf(s, "%s", &info);
    if (!strcmp (info, VERTEX)){
    fscanf(s, "%i", &index);
    fscanf(s, "%f", &p->verts[index][0]);
    fscanf(s, "%f", &p->verts[index][2]);
    fscanf(s, "%f", &p->verts[index][1]);
    cout << "Vertex: " << index << " (" << p->verts[index][0] << "," << p->verts[index][1] << "," << p->verts[index][2] << ")" << endl;
    // ABOVE LINE PROVES IT PULLS IN RIGHT!!!
    }
    }
    }

    void aseGetFaces(FILE *s, model *p){ //Get each face from the
    rewind(s); //file.
    char info[255];
    int index;
    while (!feof (s)){
    fscanf(s, "%s", &info);
    if (!strcmp (info, MESHFACE)){
    fscanf (s, "%d:", &index);
    fscanf (s, "\tA:\t%d B:\t%d C:\t%d AB:\t%d BC:\t%d CA:\t%d",
    &p->faces[index].vertIndex[0],
    &p->faces[index].vertIndex[1],
    &p->faces[index].vertIndex[2],
    &p->faces[index].edgeflag[0],
    &p->faces[index].edgeflag[1],
    &p->faces[index].edgeflag[2]);
    /*cout << "Face " << index << " made up of verticies " << p->faces[index].vertIndex[0] << ", " << p->faces[index].vertIndex[1] << ", and " << p->faces[index].vertIndex[2] << "." << endl;
    cout << "Side AB is ";
    if (p->faces[index].vertIndex[3] == 1){
    cout << "drawn. ";
    }
    else{
    cout << "not drawn. ";
    }
    cout << "Side BC is ";
    if (p->faces[index].vertIndex[4] == 1){
    cout << "drawn. ";
    }
    else{
    cout << "not drawn. ";
    }
    cout << "Side CA is ";
    if (p->faces[index].vertIndex[5] == 1){
    cout << "drawn. ";
    }
    else{
    cout << "not drawn. ";
    }
    cout << endl;*/ //ALL THIS PROVES IT PULLS IN RIGHT!!!
    }
    }
    }

    void aseGetNormals(FILE *s, model *p){
    rewind(s);
    char info[255];
    int index1, index2;
    while (!feof (s)){
    fscanf(s, "%s", &info);
    if (!strcmp (info, FACENORM)){
    fscanf(s, "%d", &index1);
    //cout << index1 << endl;

    fscanf(s, "%f %f %f",
    &p->faces[index1].normal[0][0],
    &p->faces[index1].normal[0][2],
    &p->faces[index1].normal[0][1]);
    cout << "The Normal vect. for face " << index1 << " is (" << p->faces[index1].normal[0][0] << "," << p->faces[index1].normal[0][1] << "," << p->faces[index1].normal[0][2] << ")" << endl;

    fscanf(s, "%s", &info);
    fscanf(s, "%d", &index2);
    fscanf(s, "%f %f %f",
    &p->faces[index1].normal[1][0],
    &p->faces[index1].normal[1][2],
    &p->faces[index1].normal[1][1]);
    cout << "Face " << index1 << " vertex " << index2 << " is, (" << p->faces[index1].normal[1][0] << "," << p->faces[index1].normal[1][1] << "," << p->faces[index1].normal[1][2] << ")" << endl;

    fscanf(s, "%s", &info);
    fscanf(s, "%d", &index2);
    fscanf(s, "%f %f %f",
    &p->faces[index1].normal[2][0],
    &p->faces[index1].normal[2][2],
    &p->faces[index1].normal[2][1]);
    cout << "Face " << index1 << " vertex " << index2 << " is, (" << p->faces[index1].normal[2][0] << "," << p->faces[index1].normal[2][1] << "," << p->faces[index1].normal[2][2] << ")" << endl;

    fscanf(s, "%s", &info);
    fscanf(s, "%d", &index2);
    fscanf(s, "%f %f %f",
    &p->faces[index1].normal[3][0],
    &p->faces[index1].normal[3][2],
    &p->faces[index1].normal[3][1]);
    cout << "Face " << index1 << " vertex " << index2 << " is, (" << p->faces[index1].normal[3][0] << "," << p->faces[index1].normal[3][1] << "," << p->faces[index1].normal[3][2] << ")" << endl;

    }


    }


    }

    void aseWriteVerticies(FILE *s, model *p, char *mn){
    fprintf(s, "#ifndef vect1\ntypedef float vect1;\n#endif\n\n#ifndef vect3\ntypedef vect1 vect3[3];\n#endif\n\n#ifndef triangle\ntypedef vect1 triangle[3];\n#endif\n\n");
    fprintf(s, "#ifndef %smodel\ntypedef struct\n{\n", mn);
    fprintf(s, "\tvect3 verticies[%i] = {\n\t", p->numVertex);
    for (int a=0; a<(p->numVertex-1); a++){
    fprintf(s, "{%f,%f,%f}, ", p->verts[a][0], p->verts[a][1], p->verts[a][2]);
    if (a%2 == 0){
    fprintf(s, "\n\t");
    }
    }
    fprintf(s, "{%f,%f,%f}};\n", p->verts[p->numVertex-1][0], p->verts[p->numVertex-1][1], p->verts[p->numVertex-1][2]);

    fprintf(s, "\ttriangle faces[%i] = {\n\t", p->numFaces);
    for (a=0; a<(p->numFaces-1); a++){
    fprintf(s, "{%i,%i,%i}, ", p->faces[a].vertIndex[0], p->faces[a].vertIndex[1], p->faces[a].vertIndex[2]);
    if (a%2 == 0){
    fprintf(s, "\n\t");
    }
    }
    fprintf(s, "{%i,%i,%i}};\n", p->faces[p->numFaces-1].vertIndex[0], p->faces[p->numFaces-1].vertIndex[1], p->faces[p->numFaces-1].vertIndex[2]);

    fprintf(s, "\tvect3 facenormal[%i] = {\n\t", p->numFaces);
    for (a=0; a<(p->numFaces-1); a++){
    fprintf(s, "{%f,%f,%f}, ", p->faces[a].normal[0][0], p->faces[a].normal[0][1], p->faces[a].normal[0][2]);
    if (a%2 == 0){
    fprintf(s, "\n\t");
    }
    }
    fprintf(s, "{%f,%f,%f}};\n", p->faces[p->numFaces-1].normal[0][0], p->faces[p->numFaces-1].normal[0][1], p->faces[p->numFaces-1].normal[0][2]);



    // float facenormal;\n\tfloat normal1;\n\tfloat normal2;\n\tfloat normal3;\n\tfloat edgeflags;\n} %smodel;\n#endif\n", mn, mn);
    fprintf(s, "%smodel (%s);\n", mn, mn);
    /*fprintf(s, "%s->verticies = new vect3 [%i];\n", mn, p->numVertex);
    fprintf(s, "%s->faces = new triangle [%i];\n", mn, p->numFaces);
    fprintf(s, "%s->edgeflags = new triangle [%i];\n", mn, p->numFaces);
    fprintf(s, "%s->facenormal = new vect3 [%i];\n", mn, p->numFaces);
    fprintf(s, "%s->normal1 = new vect3 [%i];\n", mn, p->numFaces);
    fprintf(s, "%s->normal2 = new vect3 [%i];\n", mn, p->numFaces);
    fprintf(s, "%s->normal3 = new vect3 [%i];\n", mn, p->numFaces);*/

    }

    void aseLoad(char *name, model *p){
    FILE *s;
    if (!(s = fopen (name, "r")))
    {
    printf ("Error: '%s' not found!\n", name);
    }
    else{
    aseGetInfo(s, &incmodel);
    aseGetVertex(s, &incmodel);
    aseGetFaces(s, &incmodel);
    aseGetNormals(s, &incmodel);
    fclose(s);
    }
    }

    void aseWrite(model *p){
    char newfn[255], modelname[255];
    printf("What file do you want the information converted to? (.dan): ");
    cin >> newfn;
    printf("What do you want this model called? ");
    cin >> modelname;
    FILE *s;
    if (!(s = fopen (newfn, "w")))
    printf("Error: '%s' could not be opened for writing.\n", newfn);
    else{
    printf("'%s' Created for writing, Writing '%s' now...\n", newfn, modelname);
    aseWriteVerticies(s, &incmodel, modelname);
    }
    }

    void main(){

    char fn[255];
    model incmodel;
    cout << "FileName? (.ase): ";
    cin >> fn;
    aseLoad(fn, &incmodel);
    aseWrite(&incmodel);
    }

  10. #10
    Senior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Belgium
    Posts
    183

    Re: Dynamically Allocated Vertex Arrays

    WOW!!!
    Just an idea, but, next time just put a link to your file instead of posting it all.


    Anyway, even if I don't know ASE file format by heart I can see several weaknesses.

    - You're doing multiple passes (readings) on the file. At the beginning of every functions retrieving data you rewind the file.
    AFAIK you don't have to, you can parse it only one time and get all the data you need.
    File reading is slow, so avoid doing useless reading.

    - You're reading and comparing a lot of strings when you don't have to. You can actually quickly know when you can discard a line or a complete block of data delimited by brackets. Discarding such blocks can be done via something like this:
    while (File.get(c) && (c != '\n')); // skip a line
    while (File.get(c) && (c != '}')); // skip a block of data
    (with "c" being a "char" and File being an "istream" such as "cin")

    - You're using "cout" for debug purpose, but cout slows down a lot your program. (it's probably what's slowing down the most your loader!)
    You should only use such tricks to debug your program when you're actually debugging it.
    Either use the "#ifdef DEBUG" method (Visual C++ defines _DEBUG when you're in Debug mode, and of course doesn't define it in Release mode. Also don't forget to use the Release mode to turn on the optimizations of the compiler).
    Or use a global constant that you can manually switch (e.g. at global scope: "const bool DebugVerbose = true;" and in your functions: "if (DebugVerbose) cout << "..."; ")

    - Last thing I'd say would be to use C++ standard library to handle I/O instead of fscanf and so.
    std::string, std::getline(istream, string, delimiter), operator >>, etc... are more than welcome when writing a text file parser.
    If you don't know the C++ standard library, then I would say you have now a reason to learn it.


    [This message has been edited by GPSnoopy (edited 12-15-2002).]

Page 1 of 2 12 LastLast

Similar Threads

  1. Dynamically allocated display lists
    By joao flavio in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-16-2012, 07:37 PM
  2. gluNurbsSurfaces & dynamically allocated arrays
    By adamnation in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-02-2004, 11:23 PM
  3. Dynamically Allocated Arrays
    By ImpactDNI in forum OpenGL: Basic Coding
    Replies: 19
    Last Post: 11-28-2002, 09:52 AM
  4. Dynamically allocated vertex arrays
    By GarlicGL in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-19-2002, 07:09 PM
  5. Dynamically allocated vertex arrays
    By GarlicGL in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 02-19-2002, 10:11 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