glut 显示 obj 模型
来源:互联网 发布:敏捷地产 知乎 编辑:程序博客网 时间:2024/05/01 04:04
图形学作业的最后一部分……
添加了光照, 但是没有纹理。
#ifndef SHOW_BY_GLUT_H_#define SHOW_BY_GLUT_H_#include <gl/glut.h>#define max_point 1000000#define max_face 1000000float p[max_point][3];int f[max_face][3];int f_count;int p_count;// gl parameters// Rotation amounts GLfloat xRot = 0.0f; GLfloat yRot = 0.0f; // mouseint oldmy=-1,oldmx=-1;// functionsvoid initializeMesh(float (*point)[3], int point_count, int (*face)[3], int face_count){f_count = face_count;p_count = point_count;for (int i = 0; i< p_count; i++){for (int j = 0; j< 3; j++){p[i][j] = point[i][j];}}for (int i = 0; i< f_count; i++){for (int j = 0; j< 3; j++){f[i][j] = face[i][j];}}}void calclater_normilize(float *a, float *b, float *c, float re[3]){float vec1[3], vec2[3];for (int i = 0; i< 3; i++){vec1[i] =b[i]-a[i];}for (int i = 0; i< 3; i++){vec2[i] =c[i]-a[i];}re[0] = vec1[1]*vec2[2] - vec2[1]*vec1[2];re[1] = vec1[2]*vec2[0] - vec2[2]*vec1[0];re[2] = vec1[0]*vec2[1] - vec2[0]*vec1[1];float all = sqrt(re[0]*re[0]+re[1]*re[1]+re[2]*re[2]);for (int i = 0; i< 3; i++){re[i] = re[i]/all;}}void drawTriangles(){for (int i = 0; i< f_count; i++){glBegin(GL_TRIANGLES);float nor[3];calclater_normilize(p[f[i][0]], p[f[i][1]], p[f[i][2]], nor);glNormal3f(nor[0], nor[1], nor[2]); for (int j = 0; j< 3; j++){int pindex = f[i][j];glVertex3f(p[pindex][0], p[pindex][1], p[pindex][2]);}glEnd(); }}// glut functions// Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_NORMALIZE);// Save the matrix state and do the rotations glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); GLfloat mat_amb_diff1[]={1,1,0.45,1.0};glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,mat_amb_diff1);//模型 drawTriangles();// Restore the matrix state glPopMatrix(); // Display the results glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // colorglColor3f(1.0f, 1.0f, 0.45f);// Depth detectglEnable(GL_DEPTH_TEST);glEnable(GL_DEPTH_TEST);// 环境光 GLfloat model_ambient[]={0.05f,0.05f,0.05f,1.0f};//GLfloat model_ambient[]={0.8f,0.8f,0.8f,1.0f};GLfloat mat_ambient[]={0.1,0.1,0.1,1};// 镜面光GLfloat mat_specular[]={0.8,1.0,1.0,1.0};// GLfloat mat_specular[]={0.08,0.10,0.10,1.0};GLfloat mat_shininess[]={5.0};// GLfloat mat_shininess[]={1.0};// 白光// 漫反射光// GLfloat white_light[]={1.0,1.0,1.0,1.0};GLfloat white_light[]={0.5,0.5,0.51,1.0};// 位置GLfloat light_position0[]={40,19.9,-60,1.0};// 背面GLfloat light_position6[]={-40,19.9,80,1.0};// 颜色glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT0,GL_POSITION,light_position0);/*glLightfv(GL_LIGHT0,GL_AMBIENT,mat_ambient);glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);*/glLightfv(GL_LIGHT0,GL_AMBIENT,model_ambient);glLightfv(GL_LIGHT0,GL_DIFFUSE,mat_specular);glLightfv(GL_LIGHT0,GL_SPECULAR,mat_shininess);glLightfv(GL_LIGHT6,GL_POSITION,light_position6);glLightfv(GL_LIGHT6,GL_AMBIENT,mat_ambient);glLightfv(GL_LIGHT6,GL_DIFFUSE,white_light);glLightfv(GL_LIGHT6,GL_SPECULAR,white_light);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0); glEnable(GL_LIGHT6); glEnable(GL_COLOR_MATERIAL); // 平滑着色glShadeModel(GL_SMOOTH); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); //指定材料着色的面glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //指定材料对镜面光的反射// 抗锯齿glEnable(GL_POINT_SMOOTH); glEnable(GL_LINE_SMOOTH); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } ///////////////////////////////////////////////////// // Handle arrow keys void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) { xRot-= 5.0f; } if(key == GLUT_KEY_DOWN) { xRot += 5.0f; } if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(key > 356.0f) xRot = 0.0f; if(key < -1.0f) xRot = 355.0f; if(key > 356.0f) yRot = 0.0f; if(key < -1.0f) yRot = 355.0f; // Refresh the Window glutPostRedisplay(); } void Mouse(int button, int state, int x, int y) //处理鼠标点击 { if(state==GLUT_DOWN) //第一次鼠标按下时,记录鼠标在窗口中的初始坐标 oldmx=x,oldmy=y; } void onMouseMove(int x,int y) //处理鼠标拖动 { //printf("%d\n",du); yRot +=(x-oldmx)/3.0; //鼠标在窗口x轴方向上的增量加到视点绕y轴的角度上,这样就左右转了 xRot +=(y-oldmy)/3.0; //鼠标在窗口y轴方向上的改变加到视点的y坐标上,就上下转了 oldmx=x,oldmy=y; //把此时的鼠标坐标作为旧值,为下一次计算增量做准备 glutPostRedisplay(); } // Reset projection void ChangeSize(int w, int h) { // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLfloat fAspect = (GLfloat) w / (GLfloat) h; gluPerspective(45.0f, fAspect, 1.0f, 225.0f); gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);//glOrtho(-0.5, 0.5, -0.5, 0.5, -10, 10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void show_by_glut(int argc, char *argv[], float (*point)[3], int point_count, int (*face)[3], int face_count){if (point_count > max_point||face_count > max_face){return;}initializeMesh(point, point_count, face, face_count);glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(800,600); glutCreateWindow("deform");glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); glutMouseFunc(Mouse); glutMotionFunc(onMouseMove);SetupRC(); glutMainLoop(); }#endif
顺便, 把main也放上
#include "vrml.h"#include "halfedge_vector.h"#include "loop.h"#include "show_by_glut.h"#define max_point 10000#define max_face 10000int main(int argc, char *argv[]){float points[max_point][3];int faces[max_face][3];int face_count;int point_count;// read vrml to objvrml2obj("test3.wrl", points, faces, point_count, face_count);// show_by_glut(argc, argv, points, point_count, faces, face_count);// obj to halfedgevector<halfegde>e;vector<halfedge_face>f;vector<halfegde_point>p;obj2halfedge(points, point_count, faces, face_count, p, f, e);// loopint n = 0;vector<vector<halfegde>>ee;vector<vector<halfedge_face>>ff;vector<vector<halfegde_point>>pp;ee.reserve(n+1);ff.reserve(n+1);pp.reserve(n+1);ee.push_back(e);ff.push_back(f);pp.push_back(p);for (int i = 0; i< n; i++){// halfedge loop subdivisionvector<halfegde>e2;vector<halfedge_face>f2;vector<halfegde_point>p2;getloop(pp[i], ff[i], ee[i], p2, f2, e2);ee.push_back(e2);ff.push_back(f2);pp.push_back(p2);}// halfedge to obj// will change facefloat (*aa)[3];int (*bb)[3];aa = new float[pp[n].size()][3];bb = new int[ff[n].size()][3];halfedge2obj(pp[n], ff[n], ee[n], aa, bb);obj2vrml("re2.wrl", aa, bb, pp[n].size(), ff[n].size());show_by_glut(argc, argv, aa, pp[n].size(), bb, ff[n].size());system("pause");return 0;}
这是结果:loop
butterfly
程序项目和一些vrml模型我放在自己的上传文档了, 有兴趣的可以去下载。
0 0
- glut 显示 obj 模型
- obj模型显示示例
- obj模型
- GLUT模型绘制函数
- 3D模型(obj文件)加载及纹理显示
- opengl加载obj模型
- OpenGL---加载obj模型
- U3D导出obj模型
- OpenGL -- OBJ 模型加载
- OpenGL: Glut显示立体图形
- obj.h------obj模型格式解析
- java3d显示obj文件
- 显示obj信息
- OpenGL读取Obj模型文件
- OpenGL读取Obj模型文件
- Obj模型导入器实现
- three.js加载obj模型
- opengl读取OBJ模型文件
- 【Unity Shaders】Unity里的雾效模拟
- BZOJ 1132 POI2008 Tro 计算几何
- 第14周项目2-带姓名的成绩单
- Deprecated: Function ereg_replace() is deprecated的解决方法
- 利用C++编写一个“猜数字”游戏
- glut 显示 obj 模型
- Android手机上使用MATLAB
- mfc 绘制文字轮廓
- httpwebrequest/httpwebresponse实际项目使用
- 二分图问题各类变形详解
- 第三章 栈和队列 思维导图
- c中没有string字符串类型
- "弹出DVD驱动器错误"解决方法
- hdoj杨辉三角