![]() |
|
Anybody have any .obj file to ALLEGRO_VERTEX code? |
Mark Oates
Member #1,146
March 2001
![]() |
So I wrote a routine to load a blender .obj file into my program. This is just a simple cube: {"name":"608542","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/f\/efc6caeda9d0a6762939e1e1b5eaec9b.png","w":644,"h":509,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/f\/efc6caeda9d0a6762939e1e1b5eaec9b"} That exports like this: # Blender v2.64 (sub 0) OBJ File: '' # www.blender.org o Cube v 300.000000 -300.000000 -299.999969 v 300.000000 -300.000000 300.000000 v -300.000031 -300.000000 299.999939 v -299.999878 -300.000000 -300.000122 v 300.000153 300.000000 -299.999847 v 299.999817 300.000000 300.000183 v -300.000122 300.000000 299.999878 v -299.999969 300.000000 -300.000000 s off f 1 2 3 4 f 5 8 7 6 f 1 5 6 2 f 2 6 7 3 f 3 7 8 4 f 5 1 4 8 And when displayed it looks like this: {"name":"608541","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/c\/ec2f741c5e888f24a33d98427083e1f5.png","w":644,"h":509,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/c\/ec2f741c5e888f24a33d98427083e1f5"} So obviously I'm getting something right. It looks like the coordinates are being imported correctly, but not being drawn correctly. I tried different ALLEGRO_PRIM_TYPEs because it seems like that might be what's causing it to not work, and they changed the way it displayed but never displayed correctly. I tried reversing the order of the vertexes, cause, who knows that might do something. here's the import function:
1class Model
2{
3public:
4 std::vector<std::vector<ALLEGRO_VERTEX>> face;
5 ALLEGRO_PRIM_TYPE prim_type;
6 ALLEGRO_BITMAP *texture;
7 //placement3d place;
8
9 Model();
10
11 void clear();
12 void scale(float x, float y, float z);
13 void move(float x, float y, float z);
14 void move_to(float x, float y, float z);
15
16 void set_texture(ALLEGRO_BITMAP *t);
17
18 void load_obj(std::string filename, ALLEGRO_COLOR color); // see below
19
20 void draw();
21};
22
23
24
25
26void Model::load_obj(std::string filename, ALLEGRO_COLOR color=al_map_rgb_f(1, 1, 1))
27{
28 clear();
29
30
31 std::ifstream in(filename, std::ios::in);
32 if (!in)
33 {
34 std::cerr << "Cannot open " << filename << std::endl;
35 //exit(1);
36 }
37
38 //prim_type = ALLEGRO_PRIM_TYPE::ALLEGRO_PRIM_TRIANGLE_LIST;
39 //prim_type = ALLEGRO_PRIM_TYPE::ALLEGRO_PRIM_TRIANGLE_STRIP;
40 prim_type = ALLEGRO_PRIM_TYPE::ALLEGRO_PRIM_TRIANGLE_FAN;
41 //ALLEGRO_PRIM_TRIANGLE_FAN;
42
43 //vec3d vec;
44 //Face *current_face = face;
45
46 std::string line;
47 std::vector<ALLEGRO_VERTEX> *current_face = NULL;
48 std::vector<ALLEGRO_VERTEX> dummy_face;
49 float x, y, z;
50 while (getline(in, line))
51 {
52 if (line.substr(0,2) == "o ")
53 {
54 face.push_back(dummy_face);
55 current_face = &face.back();
56 }
57 else if (line.substr(0,2) == "v ")
58 {
59 std::istringstream s(line.substr(2));
60 s >> x; s >> y; s >> z;
61 current_face->push_back(build_vertex(x, y, z, color, 0, 0));
62 }
63 else if (line.substr(0,2) == "f ")
64 {
65 /*
66 std::istringstream s(line.substr(2));
67 GLushort a,b,c;
68 s >> a; s >> b; s >> c;
69 a--; b--; c--;
70
71 elements.push_back(a);
72 elements.push_back(b);
73 elements.push_back(c);
74 */
75 }
76 else if (line[0] == '#') { /* ignoring this line */ }
77 else { /* ignoring this line */ }
78 }
79}
(I also have no idea how to get the texture applied/exported/imported.) [edit:] After more poking, I think it has to do with the faces, I have an idea. [edit 2:] -- |
pkrcel
Member #14,001
February 2012
|
Mark, is the code in your post the latest one that loads correctly? I can't see how you handle vertex ordering in the faces. It is unlikely that Google shares your distaste for capitalism. - Derezo |
Mark Oates
Member #1,146
March 2001
![]() |
Now my loader takes in uv textures! {"name":"608545","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/8\/b895d2e258bd8b16efc906985ddd3da6.png","w":852,"h":682,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/8\/b895d2e258bd8b16efc906985ddd3da6"} It's all weird and half the coordinates are flipped and stuff, but who cares!! pkrcel said: Mark, is the code in your post the latest one that loads correctly? I can't see how you handle vertex ordering in the faces.
Here's the code, now. It's nasty don't look at it
1
2class Model
3{
4public:
5
6 std::vector<std::vector<ALLEGRO_VERTEX>> face;
7
8 std::vector<ALLEGRO_VERTEX> vertexes;
9 std::vector<vec2d> uvs;
10 std::vector<std::vector<int>> face_index_lists;
11 std::vector<std::vector<int>> uv_index_lists;
12 //std::vector<ModelFace> face_definitions;
13
14 ALLEGRO_PRIM_TYPE prim_type;
15 ALLEGRO_BITMAP *texture;
16 //placement3d place;
17
18 Model();
19
20 void clear();
21 void scale(float x, float y, float z);
22 void move(float x, float y, float z);
23 void move_to(float x, float y, float z);
24
25 void set_texture(ALLEGRO_BITMAP *t);
26
27 void load_obj(std::string filename, ALLEGRO_COLOR color);
28
29 void draw();
30};
31
32
33
34
35void Model::load_obj(std::string filename, ALLEGRO_COLOR color=al_map_rgb_f(1, 1, 1))
36{
37 clear();
38
39 texture = al_load_bitmap("data/bitmaps/pillar_texture-02.png");
40
41
42 std::ifstream in(filename, std::ios::in);
43 if (!in)
44 {
45 std::cerr << "Cannot open " << filename << std::endl;
46 //exit(1);
47 }
48
49 //prim_type = ALLEGRO_PRIM_TYPE::ALLEGRO_PRIM_TRIANGLE_LIST;
50 //prim_type = ALLEGRO_PRIM_TYPE::ALLEGRO_PRIM_TRIANGLE_STRIP;
51 prim_type = ALLEGRO_PRIM_TYPE::ALLEGRO_PRIM_TRIANGLE_FAN;
52 //ALLEGRO_PRIM_TRIANGLE_FAN;
53
54 //vec3d vec;
55 //Face *current_face = face;
56
57 //std::vector<ALLEGRO_VERTEX> *current_face = NULL;
58 //std::vector<ALLEGRO_VERTEX> dummy_face;
59
60 std::string line;
61 float x, y, z;
62 std::vector<int> dummy_face_index;
63 while (getline(in, line))
64 {
65 if (line.substr(0,2) == "o ")
66 {
67 //face.push_back(dummy_face);
68 //current_face = &face.back();
69 }
70 else if (line.substr(0,2) == "v ")
71 {
72 std::istringstream s(line.substr(2));
73 s >> x; s >> y; s >> z;
74 vertexes.push_back(build_vertex(x, y, z, color::white, 0, 0));
75 }
76 else if (line.substr(0,3) == "vt ")
77 {
78 std::istringstream s(line.substr(3));
79 s >> x; s >> y;
80 uvs.push_back(vec2d(x, y));
81 }
82 else if (line.substr(0,2) == "f ")
83 {
84 std::istringstream s(line.substr(2));
85
86 face_index_lists.push_back(dummy_face_index);
87 uv_index_lists.push_back(dummy_face_index);
88 std::vector<std::string> tokens = php::explode(" ", line.substr(2));
89 for (unsigned i=0; i<tokens.size(); i++)
90 {
91 std::vector<std::string> face_parts = php::explode("/", tokens[i]);
92
93 // get the face vertex
94 if (face_parts.size() >= 1)
95 face_index_lists.back().push_back(atoi(face_parts[0].c_str()) - 1);
96
97 // get the uv vertex
98 if (face_parts.size() >= 2)
99 uv_index_lists.back().push_back(atoi(face_parts[1].c_str()) - 1);
100 }
101 }
102 else if (line[0] == '#') { /* ignoring this line */ }
103 else { /* ignoring this line */ }
104 }
105
106 std::cout << "Model Loaded" << std::endl;
107 std::cout << " uvs.size(): " << uvs.size();
108}
109
110
111
112
113
114void Model::draw()
115{
116 //ALLEGRO_TRANSFORM transform;
117 //al_identity_transform(&transform);
118
119 //place.start_transform();
120
121 if (!face.empty())
122 for (unsigned f=0; f<face.size(); f++)
123 al_draw_prim(&face[f][0], NULL, texture, 0, face[f].size(), prim_type);
124 else if (!face_index_lists.empty() && !vertexes.empty())
125 {
126 for (unsigned i=0; i<face_index_lists.size(); i++)
127 {
128 // reset the uv coordinates
129 for (unsigned j=0; j<face_index_lists[i].size(); j++)
130 {
131 ALLEGRO_VERTEX *this_vertex = &vertexes[face_index_lists[i][j]];
132 //uv_index_lists[i][j];
133 this_vertex->u = uvs[uv_index_lists[i][j]].x * al_get_bitmap_width(texture);
134 this_vertex->v = -uvs[uv_index_lists[i][j]].y * al_get_bitmap_height(texture);
135 //this_vertex->u = random_float(0, 1) * al_get_bitmap_width(texture);
136 //this_vertex->v = random_float(0, 1) * al_get_bitmap_height(texture);
137 }
138 al_draw_indexed_prim(&vertexes[0], NULL, texture, &face_index_lists[i][0], face_index_lists[i].size(), prim_type);
139 }
140 }
141
142
143
144 //place.restore_transform();
145}
-- |
AMCerasoli
Member #11,955
May 2010
![]() |
|
pkrcel
Member #14,001
February 2012
|
Cool, I am not familiar with blender obj files.....has it any info about the texture embedded in it? Nice job anyway Mark.....I still struggle a bit with how you handle vertex indexes but it is really really cool. It is unlikely that Google shares your distaste for capitalism. - Derezo |
|