Advertisement

opengl model loading

Started by January 07, 2003 12:09 PM
6 comments, last by frankie 21 years, 10 months ago
I''m using blender to export a model (I''m using a model of the decopter project)

import Blender

file=fopen("file","w")

for object in Blender.Object.GetSelected();
  mesh=object.data

 for v in mesh.verts:
  file.write("%f %f %f\n" % (v.co.x , v.co.y , v.co.z))

file.close();

 
It exports fine, I get a list of coordinates in my file... I load the file using my opengl program, but that doesn''t went fine ( see http://www.anemaet.nl/snapshot2.png ) This is the code I''m using to load it:

     while (!feof(file)){ 
      fscanf(file,"%f %f %f",&this->v1_1,&this->v1_2,&this->v1_3);  
      fscanf(file,"%f %f %f",&this->v2_1,&this->v2_2,&this->v2_3);
      fscanf(file,"%f %f %f",&this->v3_1,&this->v3_2,&this->v3_3); 
	
      printf("\n %f %f %f",this->v1_1,this->v1_2,this->v1_3);
      printf("\n %f %f %f",this->v2_1,this->v2_2,this->v2_3);
      printf("\n %f %f %f",this->v3_1,this->v3_2,this->v3_3);
      i++;
     }

 </pre> 

And here''s the code I''m using to draw it:

<pre>
 glBegin(GL_TRIANGLES);
  for (i=0; i&lt;this->nrobjs; i++){
   //glTexCoord2f(this->tv1_1,this->tv1_2); 
   glVertex3f(this->v1_1,this->v1_2,this->v1_3);
   glVertex3f(this->v2_1,this->v2_2,this->v2_3);
   glVertex3f(this->v3_1,this->v3_2,this->v3_3);
  } 
 glEnd();
 </pre> 

Thanks in advice,
Frank  </i>   
Let there be light,and there was code
This should really go in the OpenGL forum, but have you tried checking the file? making sure that the co-ordinates are what you want them to be? then you can isolate which part is going wrong. Exporter or importer.

500 x 1
Advertisement
the co-oordinates are correct.
I printed them on the screen, they are exactly the same as in the file.

I could write an exporter...
but I think I forgot to save something.
Let there be light,and there was code
can you post a screenshot where the model is displayed fine?

500 x 1
http://www.anemaet.nl/snapshot3.png
Let there be light,and there was code
You are exporting just vertices if I right see it, you need to export faces too. It's not correct to render GL_TRIANGLES just from just the vertices you read.

Edit: If you need a OBJ exporter script, here's one good:
http://www.ualberta.ca/~cwant/blender/temp/OBJIO_wings_fix.py

RacingTREME updated.


[edited by - stefu on January 7, 2003 4:24:28 PM]
Advertisement
> You are exporting just vertices if I right see it, you need to >export faces too. It's not correct to render GL_TRIANGLES just >from just the vertices you read.

ok, but what is a face, what's it for?
and how do I save them in the file?


When I loaded an md2 the whole object was made out of triangles.

[edited by - frankie on January 8, 2003 12:17:10 PM]
Let there be light,and there was code
In blender faces can be either _indexed_ triangles or quads.

This means that face holds index values that say which vertices to use.

For example face with indices 0,3,4 is triangle that has vertex[0], vertex[3] and vertex[4] in the corners.

This way one vertex can be shared with many triangles.

Here's how to export faces (triangles) in blender:

      for face in mesh.faces:  face_id = []  for vertex in face:    face_id.append(vertex.index)  if len(face_id)>=3:    file.write("%d %d %d\n" %       (face_id[0] , face_id[1] , face_id[2]))  if len(face_id)>=4:    file.write("%d %d %d\n" %       (face_id[0] , face_id[2] , face_id[3]))    To render indexed triangles, int indices[index_count]; // this holds the face indices exported from blenderstruct vec3 { float x,y,z; };vec3 coords[vertex_count]; // this holds the vertex coordinates exported from blenderglBegin(GL_TRIANGLES);for (i=0; i<index_count; i++){    glVertex3fv( (GLfloat *)&coords[ indices[i] ]);}glEnd();       




RacingTREME updated.




[edited by - stefu on January 8, 2003 3:29:26 PM]

This topic is closed to new replies.

Advertisement