As a beginner of openGL, I spent several days and finished this mesh viewer using VC++. This mesh viewer can read 3D model from M file and draw the model. It also has the following functions:
1. select file;
2. set projection;
3. draw object by points/wireframe/smooth shading/flat shading;
4. draw xyz axis;
5. draw xz plane;
6. draw boundary box;
7. translate object/camera;
8. rotate object/camera;
9. scale object/camera
Here is the picture
First, let me show you the format of M file. In a M file, vertices and faces are defined:
# Created on 2012/09/12 05:06:24 using:
# filtermesh cap.m -removeinfo
# (Timing on cpu=x86 Family 6 Model 37 Stepping 2, GenuineIntel host=?)
# (_readmesh: 0.00)
# (Filtermesh: 0.00)
Vertex 1 -0.00418561 0.93191 4.22368
Vertex 2 -0.492633 1.82421 3.84173
Vertex 3 -0.884398 2.62797 3.2424
Vertex 4 4.43596 -3.0351 0.881848Face 1 1 2 3
Face 2 2 3 4
Face 3 3 4 1
Face 4 4 1 2
I stored all the information into two vectors: a vector of vertices and a vector of faces.
vector<Vertices> v;
vector<Face> f;
While reading each face, I compute the normal vector and put it into each of the three vertices of the face. At last, I normalize all the normal vectors in each vertex to generate its final normal vector.
Then I creat the main window and another glui window. I didn't creat glui subwindow on the main window, because if I do so, it would be difficult to control when I resize the main window.
Since it's a consule application, I cannot use open-file dialog. So I can only use the fileBrowser provided by glui.
I put two sets of rotation and translation controls, one is for object, the other is for camera.
After building the user interface, some callback functions have to be defined:
glutReshapeFunc function is called the first time the window is generated and every time it's resized.
glutDisplayFunc is the main part of this app. It is called many time in a second to draw objects on the window.
In glutDisplayFunc, first I set projection matrix.
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
if(projection==0){
glOrtho(-maxLength,maxLength,-maxLength,maxLength,-maxLength*2,maxLength*2);
//set camera
gluLookAt(maxLength/8,maxLength/8,maxLength/8,0,0,0,0,1,0);
}
else{
gluPerspective(60,1,maxLength/100,maxLength*4);
//set camera
gluLookAt(maxLength,maxLength,maxLength,0,0,0,0,1,0);
}
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
You have to set the current matrix to modelview after you set it to projection.
Then I compute the current matrix to rotate, translate, and zoom in/out the camera.
glMultMatrixf (cameraRotation);
glScalef(size,size,size);
glTranslatef(tx,ty,tz);
After this, draw xyz axis and xz-plane. This must be done before moving the object.
To rotate, translate, or update the size of the object, you can use the same way as the camera.
Finally, you can draw your object. Since all the object are triangle mesh, you can just draw triangles. It's very simple.
for(int i=1;i<f.size();i++){
glBegin(mode);
glNormal3fv(v[f[i].a].n);
glVertex3f(v[f[i].a].x,v[f[i].a].y,v[f[i].a].z);
glNormal3fv(v[f[i].b].n);
glVertex3f(v[f[i].b].x,v[f[i].b].y,v[f[i].b].z);
glNormal3fv(v[f[i].c].n);
glVertex3f(v[f[i].c].x,v[f[i].c].y,v[f[i].c].z);
glEnd();
}
Run this app, you can load a mesh file and do the operations on the control box.
That's all. I don't have much time to cover the details here. If you have questions, please leave a message:)
Thanks for viewing:)
I am also a OpenGL beginner. I could not find a nice mesh viewer tutorial. Yours looks so great! Could you please give me a chance to learn your tutorial or source code of this project? Thank you very much!
ReplyDeleteMy email address is buptyuanyuan@gmail.com
me too,. I also need help with source code and tutorial. Thanks.
ReplyDelete611haru@gmail.com
DeleteCould you please help me? Thanks.
DeletePlease...
Delete