通过VC++和OpenGL显示STL文件
来源:互联网 发布:扎克伯格起诉社交网络 编辑:程序博客网 时间:2024/06/03 21:08
老师给留了作业,通过OpenGL将STL文件显示出来。自己初步学习了OpenGL的知识,又学了一些VC++知识,参考了一些资料,最后写出来了。
#include<iostream>#include<vector>#include<fstream>#include<string>#include<sstream>#include<cmath>#include<stdio.h>#include<GLTools.h>#include<GL/glut.h>using namespace std;int nums = 0;float* facets;//use the float onlyfloat* vectors;int angle = 0;int xyz[3] = { 0,0,0 };//lightGLfloat lightAmb[] = { 0.f,0.f,0.f,1.f };GLfloat lightDiff[] = { 1.f,1.f,1.f,1.f };GLfloat lightSpec[] = { 1.f,1.f,1.f,1.f };GLfloat lightPos[] = { 1.f,1.f,1.f,0.f };//materialGLfloat goldAmb[] = { 0.24725f,0.1995f,0.0745f,1.f };GLfloat goldDiff[] = { 0.75164f,0.60648f,0.22648f,1.f };GLfloat goldSpec[] = { 0.628281f,0.555802f,0.366065f,1.f };GLfloat goldShin = 51.2f;void dataGet() { int amountFacet = 0, amountVectors = 0; long size = 0; int nlines = 0; FILE* file; errno_t err; err = fopen_s(&file, "D:\\downloads\\Stanford_Bunny\\Stanford_Bunny\\bunny.stl", "r"); fseek(file, 0L, SEEK_END); size = ftell(file); fclose(file); err = fopen_s(&file, "D:\\downloads\\Stanford_Bunny\\Stanford_Bunny\\bunny.stl", "r"); //读取Standford Bunny的STL文件 for (int i = 0; i < size; i++) {//get the amount of lines if (getc(file) == '\n') { nlines++; } } nums = nlines / 7;//get the amount of triangles amountFacet = 3 * nums; amountVectors = 9 * nums; facets = new float[3 * nums]; vectors = new float[9 * nums]; rewind(file); while (getc(file) != '\n'); for (int i = 0, j = 0; i < nlines; i++, j++) { //获取各个三角面的顶点值和法向量值 double x = 0.0, y = 0.0, z = 0.0; int re = 0; re = fscanf_s(file, "%*s %*s %lf %lf %lf\n", &x, &y, &z); if (re == 3) { facets[i] = x; i++; facets[i] = y; i++; facets[i] = z; }//the facet's data fscanf_s(file, "%*s %*s"); re = fscanf_s(file, "%*s %lf %lf %lf", &x, &y, &z); if (re == 3) { vectors[j] = x; j++; vectors[j] = y; j++; vectors[j] = z; j++; }//the first vector re = fscanf_s(file, "%*s %lf %lf %lf", &x, &y, &z); if (re == 3) { vectors[j] = x; j++; vectors[j] = y; j++; vectors[j] = z; j++; }//the second vector re = fscanf_s(file, "%*s %lf %lf %lf", &x, &y, &z); if (re == 3) { vectors[j] = x; j++; vectors[j] = y; j++; vectors[j] = z; }//the third vector fscanf_s(file, "%*s"); fscanf_s(file, "%*s"); }}void drawTriangle() {//绘制三角形 for (int x = 1; x <= nums; x++) { int i = 3 * (x - 1); int j = 9 * (x - 1); glBegin(GL_TRIANGLES); glVertex3d(vectors[j], vectors[j + 1], vectors[j + 2]); j = j + 3; glVertex3d(vectors[j], vectors[j + 1], vectors[j + 2]); j = j + 3; glVertex3d(vectors[j], vectors[j + 1], vectors[j + 2]); glEnd(); }}void reshape(int w, int h) { glViewport(0.0, 0.0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(350000000000000000000000000.0, (double)w / h, 5.0, 2000000000000000000.0);//显示视角不知道如何控制比价好,一点点调出这个值 glMatrixMode(GL_MODELVIEW);}void idle() { glutPostRedisplay();}void myDisplay(){ static int angle = 0; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(1.0, 1.0, 1.0, 0.0);//background color glLoadIdentity(); gluLookAt(0.0, -160.0, 0.0, 10.0, 50.0, 0.0, 0.0, 0.0, -1.0); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glPushMatrix(); glRotatef(float(angle), 0.f, 0.f, 1.f); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, goldAmb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, goldDiff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, goldSpec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, goldShin); drawTriangle(); glPopMatrix(); glutSwapBuffers(); if (++angle >= 360) { angle = 0; }//图像进行旋转}void init() { glClearColor(1.f, 1.f, 1.f, 1.f); glClearDepth(1.f); glEnable(GL_DEPTH_TEST); glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff); glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING);}void main(int argc, char* argv[]) { dataGet(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); //glutInitDisplayMode(GLUT_RGBA ); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("test"); init(); glutDisplayFunc(myDisplay); glutReshapeFunc(reshape); glutIdleFunc(idle); glutMainLoop(); system("pause");}
参考文章链接:
读取STL模型,并用opengl显示出来
基于 VC++和 OpenGL 的 STL 文件读取显示
0 0
- 通过VC++和OpenGL显示STL文件
- lib3ds加载3ds文件通过opengl显示
- OpenGL BMP文件显示
- 通过Web Services显示和下载文件
- 3DS文件在OpenGL的读入和显示
- 位图文件显示(VC++)
- 基于OpenGL 的STL 文件可视化研究
- 通过opengl来实现yuv的显示
- VC下载文件 + 显示进度条
- OPENGL和VC编程的基础1
- 通过VC++ STL源码了解template特化与偏特化
- OpenGL加载、显示、截图、保存位图文件
- OpenGL加载与显示Targa位图文件
- 【OPENGL】读取并显示BMP文件
- 基于lib3ds和OpenGL的3ds文件的读取与显示
- OpenGL(十一) BMP真彩文件的显示和复制操作
- OpenGL通过顶点绘制点和直线
- vc和bcb中的stl/boost版本
- Nginx服务器+PHP修改文件上传的大小限制
- Python csv模块的使用
- E
- 同步与异步的概念
- 容器操作
- 通过VC++和OpenGL显示STL文件
- Less 5&6
- Mac+Sublime3+Python
- 【IMWeb训练营作业】Stock-trade
- MySQL输入密码后闪退(jbdc连接异常)
- shell学习——基础知识
- 字符编码笔记:ASCII,Unicode和UTF-8
- java8中map的新方法--replace
- 语义化版本