Advertisement

Problem with Loading Texture and Vertex Coords from Arrays

Started by December 21, 2004 03:27 PM
4 comments, last by FireViper 19 years, 11 months ago
I'm using Anim8or , a 3d modeling program. When I export my code in to c, it look something like this

static float Box_coords[] = {
    -2.5, 0.0450001, -2.5,
    2.5, 0.0450001, -2.5,
    2.5, 5.045, -2.5,
    -2.5, 5.045, -2.5,
    -2.5, 0.0450001, 2.5,
    -2.5, 5.045, 2.5,
    2.5, 5.045, 2.5,
    2.5, 0.0450001, 2.5,
    -2.5, 0.0450001, -2.5,
    -2.5, 5.045, -2.5,
    -2.5, 5.045, 2.5,
    -2.5, 0.0450001, 2.5,
    2.5, 0.0450001, -2.5,
    2.5, 0.0450001, 2.5,
    2.5, 5.045, 2.5,
    2.5, 5.045, -2.5,
    -2.5, 5.045, -2.5,
    2.5, 5.045, -2.5,
    2.5, 5.045, 2.5,
    -2.5, 5.045, 2.5,
    -2.5, 0.0450001, -2.5,
    -2.5, 0.0450001, 2.5,
    2.5, 0.0450001, 2.5,
    2.5, 0.0450001, -2.5,
};

static float Box_texcoords[] = {
    0, 0,
    1, 0,
    1, 1,
    0, 1,
    0, 0,
    0, 1,
    1, 1,
    1, 0,
    0, 0,
    0, 1,
    0, 1,
    0, 0,
    1, 0,
    1, 0,
    1, 1,
    1, 1,
    0, 1,
    1, 1,
    1, 1,
    0, 1,
    0, 0,
    0, 0,
    1, 0,
    1, 0,
};



so I use the following code to load it

glBegin(GL_QUADS);
    for (int load_box_coords = 0; load_box_coords < 72; load_box_coords++)
    {
       glVertex3f( Box_coords[load_box_coords], Box_coords[load_box_coords+1], Box_coords[load_box_coords+2] );
        load_box_coords += 2;
    }
glEnd();

But I don't know how to load the texture coords
There should be an array called "Box_indices" that points to each vertex.

Here is what I got when I exported a cube:

//verices, normals and texture coordsstatic int Box_indices[] = {    0, 2, 1,    0, 3, 2,    4, 6, 5,    4, 7, 6,    8, 10, 9,    8, 11, 10,    12, 14, 13,    12, 15, 14,    16, 18, 17,    16, 19, 18,    20, 22, 21,    20, 23, 22,};


When Anim8or exports objects it converts everything to triangles so that the arrays can easily be constructed.

The first triangle would be:

Box_coords[0] & Box_texcoords[0],
Box_coords[2] & Box_texcoords[2] and
Box_coords[1] & Box_texcoords[1]

Its been awhile since I used OGL but I think this might work:

glBegin(GL_TRIANGLES);    for (int i = 0; i < sizeof(Box_indices)/sizeof(int); i++)    {       glTexCoord2f( Box_texcoords[Box_indices*2], Box_texcoords[Box_indices*2]+1 );       glVertex3f(   Box_coords[Box_indices*3], Box_coords[Box_indices*3]+1, Box_coords[Box_indices*3]+2 );    }glEnd();


Like I said that code is probably way off, if it works then it's a miracle.

EDIT:

>.< I can't believe I made such a n00b mistake, adding the values to the vertex positions instead of the array index.

glBegin(GL_TRIANGLES);    for (int i = 0; i < sizeof(Box_indices)/sizeof(int); i++)    {       glTexCoord2f( Box_texcoords[Box_indices*2], Box_texcoords[(Box_indices*2)+1] );       glVertex3f(Box_coords[Box_indices*3], Box_coords[(Box_indices*3)+1], Box_coords[(Box_indices*3)+2] );    }glEnd();


I can get this to compile but I still can't display anything.

The point of my post though was to show you how Anim8or exported your mesh and that you need to use the provided indices rather than just looping through each vertex.

[Edited by - Scet on December 21, 2004 10:52:14 PM]
Advertisement
It dosen't work, too bad [sad]
I guess noone knows how to load arrays, does anyone know how to load models from an8? or a site that has a tutorial on it?
Quote: Original post by FireViper
I guess noone knows how to load arrays, does anyone know how to load models from an8? or a site that has a tutorial on it?


You might want to check out a8viewer and there is also some more info including the file format for .an8's that can be found on the Resources page.
After a few hours of experimenting and editing prewritten codes I finally created a loader. Here is the code for any one else who with the same problem.
Model.h
#ifndef __OBJECT_H__#define __OBJECT_H__#define MAX_VERTICES 8000#define MAX_POLYGONS 8000typedef struct{    float x,y,z;}vertex_type;typedef struct{    int a,b,c;}polygon_type;typedef struct{    float u,v;}mapcoord_type;typedef struct  {	char name[20];    int id_texture;    int vertex_number;    int polygon_number;    vertex_type vertex[MAX_VERTICES];    mapcoord_type mapcoord[MAX_VERTICES];    polygon_type polygon[MAX_POLYGONS];} object_type, *obj_type_ptr;#endif


Cube.c
object_type cube ={ "Cube01", //Name 0,        //Texture ID 72,       //Vertex Number 36,       //Polygon Number  {        //Vertices    -2.5, -2.5, -2.5,     2.5, -2.5, -2.5,     2.5, 2.5, -2.5,    -2.5, 2.5, -2.5,    -2.5, -2.5, 2.5,    -2.5, 2.5, 2.5,     2.5, 2.5, 2.5,     2.5, -2.5, 2.5,    -2.5, -2.5, -2.5,    -2.5, 2.5, -2.5,    -2.5, 2.5, 2.5,    -2.5, -2.5, 2.5,     2.5, -2.5, -2.5,     2.5, -2.5, 2.5,     2.5, 2.5, 2.5,     2.5, 2.5, -2.5,    -2.5, 2.5, -2.5,     2.5, 2.5, -2.5,     2.5, 2.5, 2.5,    -2.5, 2.5, 2.5,    -2.5, -2.5, -2.5,    -2.5, -2.5, 2.5,     2.5, -2.5, 2.5,     2.5, -2.5, -2.5,  },  {//Texture Coords    0, 0,    1, 0,    1, 1,    0, 1,    0, 0,    0, 1,    1, 1,    1, 0,    0, 0,    0, 1,    0, 1,    0, 0,    1, 0,    1, 0,    1, 1,    1, 1,    0, 1,    1, 1,    1, 1,    0, 1,    0, 0,    0, 0,    1, 0,    1, 0,  },  {//Polygons    0, 2, 1,    0, 3, 2,    4, 6, 5,    4, 7, 6,    8, 10, 9,    8, 11, 10,    12, 14, 13,    12, 15, 14,    16, 18, 17,    16, 19, 18,    20, 22, 21,    20, 23, 22,  },};


void DrawFigure(obj_type_ptr object){glBindTexture(GL_TEXTURE_2D, texture[object->id_texture]);     glBegin(GL_TRIANGLES);     for (int index=0;index<object->polygon_number;index++)    {        glTexCoord2f( object->mapcoord[ object->polygon[index].a ].u,                      object->mapcoord[ object->polygon[index].a ].v);        glVertex3f( object->vertex[ object->polygon[index].a ].x,                    object->vertex[ object->polygon[index].a ].y,                    object->vertex[ object->polygon[index].a ].z);         glTexCoord2f( object->mapcoord[ object->polygon[index].b ].u,                      object->mapcoord[ object->polygon[index].b ].v);        glVertex3f( object->vertex[ object->polygon[index].b ].x,                    object->vertex[ object->polygon[index].b ].y,                    object->vertex[ object->polygon[index].b ].z);                glTexCoord2f( object->mapcoord[ object->polygon[index].c ].u,                      object->mapcoord[ object->polygon[index].c ].v);        glVertex3f( object->vertex[ object->polygon[index].c ].x,                    object->vertex[ object->polygon[index].c ].y,                    object->vertex[ object->polygon[index].c ].z);    }    glEnd();}

You can call the function like this
DrawFigure(&cube);

[Edited by - FireViper on December 27, 2004 7:02:33 AM]

This topic is closed to new replies.

Advertisement