3D mesh viewer More...
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include "ViewerMesh/ViewerMesh.h"
#include "viewer/Arcball.h"
#include "bmp/RgbImage.h"
Go to the source code of this file.
Functions | |
void | read_frame_buffer () |
void | setupObject (void) |
void | setupEye (void) |
void | setupLight () |
void | draw_mesh () |
void | display () |
void | reshape (int w, int h) |
void | help () |
void | keyBoard (unsigned char key, int x, int y) |
void | setupGLstate () |
void | mouseClick (int button, int state, int x, int y) |
void | mouseMove (int x, int y) |
void | initialize_bmp_texture () |
void | normalize_mesh (CVMesh *pMesh) |
void | compute_normal (CVMesh *pMesh) |
int | main (int argc, char *argv[]) |
Variables | |
int | win_width |
int | win_height |
int | gButton |
int | startx |
int | starty |
int | shadeFlag = 0 |
CQrot | ObjRot (0, 0, 1, 0) |
CPoint | ObjTrans (0, 0, 0) |
CVMesh | mesh |
CArcball | arcball |
int | textureFlag = 2 |
GLuint | texName |
RgbImage | image |
3D mesh viewer
3D triangle mesh viewer / /*!
Definition in file viewer.cpp.
void compute_normal | ( | CVMesh * | pMesh | ) |
Compute the face normal and vertex normal
pMesh | the input mesh |
Definition at line 471 of file viewer.cpp.
{
//student: fill the details to compute face normal and vertex normal
};
void display | ( | ) |
display call back function
Definition at line 200 of file viewer.cpp.
{ /* clear frame buffer */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); setupLight(); /* transform from the eye coordinate system to the world system */ setupEye(); glPushMatrix(); /* transform from the world to the ojbect coordinate system */ setupObject(); /* draw the mesh */ draw_mesh(); glPopMatrix(); glutSwapBuffers(); }
void draw_mesh | ( | ) |
draw mesh
Definition at line 179 of file viewer.cpp.
{ glBindTexture(GL_TEXTURE_2D, texName); //students: fill the details here //go through faces of the mesh //go through vertices of each face //specify normal, texture coordinates, vertex color and vertex position for (CVMesh::MeshFaceIterator fiter( &mesh ); !fiter.end(); ++ fiter ) { CViewerFace * pf = *fiter; for( CVMesh::FaceVertexIterator fviter( pf ); !fviter.end(); ++ fviter ) { CViewerVertex * v = *fviter; } } }
void help | ( | ) |
helper function to remind the user about commands, hot keys
Definition at line 242 of file viewer.cpp.
{ printf("w - Wireframe Display\n"); printf("f - Flat Shading \n"); printf("s - Smooth Shading\n"); printf("t - Texture Mapping\n"); printf("o - Save frame buffer to snap_n.bmp\n"); printf("? - Help Information\n"); printf("esc - quit\n"); }
void initialize_bmp_texture | ( | ) |
initialize bitmap image texture
Definition at line 398 of file viewer.cpp.
{ glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &texName); glBindTexture(GL_TEXTURE_2D, texName); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); int ImageWidth = image.GetNumCols(); int ImageHeight = image.GetNumRows(); GLubyte * ptr = (GLubyte * )image.ImageData(); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ImageWidth, ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, ptr); if(textureFlag == 1) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); else if(textureFlag == 2) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D); }
void keyBoard | ( | unsigned char | key, | |
int | x, | |||
int | y | |||
) |
Keyboard call back function
Definition at line 255 of file viewer.cpp.
{ //students: fill the details in the following switch( key ) { case 'f': //Flat Shading //set shadeFlag break; case 's': //Smooth Shading //set shadeFlag break; case 'w': //Wireframe mode break; case 't': textureFlag = (textureFlag +1 )%3; switch( textureFlag ) { case 0: //disable texture mapping break; case 1: //enable texture mapping //replace mode break; case 2: //enable texture mapping //modulate mode break; } break; case '?': help(); break; case 'o': read_frame_buffer(); break; case 27: exit(0); break; } glutPostRedisplay(); }
int main | ( | int | argc, | |
char * | argv[] | |||
) |
main function for viewer
Definition at line 478 of file viewer.cpp.
{ if( argc < 3 ) { printf("Usage: %s mesh_name bmp_name\n"); return -1; } std::string mesh_name( argv[1] ); if( strutil::endsWith( mesh_name, ".obj" ) ) { mesh.read_obj( argv[1] ); } if( strutil::endsWith( mesh_name, ".m" ) ) { mesh.read_m( argv[1] ); } image.LoadBmpFile( argv[2] ); normalize_mesh( &mesh ); compute_normal( &mesh ); /* glut stuff */ glutInit(&argc, argv); /* Initialize GLUT */ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); glutInitWindowSize(800, 800); glutCreateWindow("Mesh Viewer"); /* Create window with given title */ glViewport(0,0,800,800 ); glutDisplayFunc(display); /* Set-up callback functions */ glutReshapeFunc(reshape); glutMouseFunc(mouseClick); glutMotionFunc(mouseMove); glutKeyboardFunc(keyBoard); setupGLstate(); initialize_bmp_texture(); glutMainLoop(); /* Start GLUT event-processing loop */ return 0; }
void mouseClick | ( | int | button, | |
int | state, | |||
int | x, | |||
int | y | |||
) |
mouse click call back function
Definition at line 331 of file viewer.cpp.
{ /* set up an arcball around the Eye's center switch y coordinates to right handed system */ if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { gButton = GLUT_LEFT_BUTTON; //student: fill the details to initialize Arcball } if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { startx = x; starty = y; gButton = GLUT_MIDDLE_BUTTON; } if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { startx = x; starty = y; gButton = GLUT_RIGHT_BUTTON; } return ; }
void mouseMove | ( | int | x, | |
int | y | |||
) |
mouse motion call back function
Definition at line 359 of file viewer.cpp.
{ CPoint trans; CQrot rot; /* rotation, call arcball */ if (gButton == GLUT_LEFT_BUTTON ) { //student: fill the details to update the Arcball //update the ObjRot glutPostRedisplay(); } /*xy translation */ if (gButton == GLUT_MIDDLE_BUTTON) { double scale = 10./win_height; trans = CPoint(scale*(x-startx), scale*(starty-y), 0 ); startx = x; starty = y; ObjTrans = ObjTrans + trans; glutPostRedisplay(); } /* zoom in and out */ if (gButton == GLUT_RIGHT_BUTTON ) { double scale = 10./win_height; trans = CPoint(0,0, scale*(starty-y) ); startx = x; starty = y; ObjTrans = ObjTrans+trans; glutPostRedisplay(); } }
void normalize_mesh | ( | CVMesh * | pMesh | ) |
Normalize mesh
pMesh | the input mesh |
Definition at line 430 of file viewer.cpp.
{ CPoint s(0,0,0); for( CVMesh::MeshVertexIterator viter( pMesh ); !viter.end(); ++ viter ) { CViewerVertex * v = *viter; s = s + v->point(); } s = s / pMesh->numVertices(); for( CVMesh::MeshVertexIterator viter( pMesh ); !viter.end(); ++ viter ) { CViewerVertex * v = *viter; CPoint p = v->point(); p = p-s; v->point() = p; } double d = 0; for( CVMesh::MeshVertexIterator viter( pMesh ); !viter.end(); ++ viter ) { CViewerVertex * v = *viter; CPoint p = v->point(); for( int k = 0; k < 3; k ++ ) { d = ( d > fabs(p[k]) )?d: fabs(p[k]); } } for( CVMesh::MeshVertexIterator viter( pMesh ); !viter.end(); ++ viter ) { CViewerVertex * v = *viter; CPoint p = v->point(); p = p/d; v->point() = p; } };
void read_frame_buffer | ( | ) |
save frame buffer to an image "snap_k.bmp"
Definition at line 123 of file viewer.cpp.
{ static int id = 0; GLfloat * buffer = new GLfloat[win_width * win_height*3]; assert( buffer ); glReadBuffer(GL_FRONT_LEFT); glReadPixels( 0, 0, win_width, win_height, GL_RGB, GL_FLOAT, buffer); RgbImage image(win_height,win_width); for( int i = 0; i < win_height; i ++ ) for( int j = 0; j < win_width;j ++ ) { float r = buffer[(i*win_width + j)*3+0]; float g = buffer[(i*win_width + j)*3+1]; float b = buffer[(i*win_width + j)*3+2]; image.SetRgbPixelf( i,j, r,g,b); } delete []buffer; char name[256]; std::ostringstream os(name); os << "snape_"<< id++ << ".bmp"; image.WriteBmpFile( os.str().c_str() ); }
void reshape | ( | int | w, | |
int | h | |||
) |
Called when a "resize" event is received by the window.
Definition at line 218 of file viewer.cpp.
{ float ar; win_width=w; win_height=h; ar = (float)(w)/h; glViewport(0, 0, w, h); /* Set Viewport */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); // magic imageing commands gluPerspective( 40.0, /* field of view in degrees */ ar, /* aspect ratio */ 1.0, /* Z near */ 100.0 /* Z far */); glMatrixMode(GL_MODELVIEW); glutPostRedisplay(); }
void setupEye | ( | void | ) |
the eye is always fixed at world z = +5
Definition at line 165 of file viewer.cpp.
{ glLoadIdentity(); gluLookAt( 0,0, 5,0,0,0,0,1,0); }
void setupGLstate | ( | ) |
setup GL states
Definition at line 305 of file viewer.cpp.
{ GLfloat lightOneColor[] = {1, 1, 1, 1}; GLfloat globalAmb[] = {.1, .1, .1, 1}; GLfloat lightOnePosition[] = {.0, .0, 1, 0.0}; glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glEnable(GL_DEPTH_TEST); glClearColor(0,0,0,0); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHT1); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmb); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition); }
void setupLight | ( | ) |
setup light
Definition at line 171 of file viewer.cpp.
{ CPoint position(0,0,1); GLfloat lightOnePosition[4]={position[0], position[1], position[2], 0}; glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition); }
void setupObject | ( | void | ) |
Definition at line 112 of file viewer.cpp.
int gButton |
Definition at line 100 of file viewer.cpp.
Definition at line 118 of file viewer.cpp.
Definition at line 109 of file viewer.cpp.
int shadeFlag = 0 |
Definition at line 102 of file viewer.cpp.
int startx |
Definition at line 101 of file viewer.cpp.
int starty |
Definition at line 101 of file viewer.cpp.
GLuint texName |
Definition at line 117 of file viewer.cpp.
int textureFlag = 2 |
Definition at line 114 of file viewer.cpp.
int win_height |
Definition at line 99 of file viewer.cpp.
int win_width |
Definition at line 99 of file viewer.cpp.