#include "ply.h"
void plyloaddraw::load(char* filename)
{
printf("Loading %s...\n",filename);
char* pch = strstr(filename,".ply");
if (pch != NULL) //检查该文件是否是.ply文件
{
FILE* file = fopen(filename,"r"); //以只读方式打开file
if (!file) //如果file不存在则执行if内部语句
{
printf("load PLY file %s failed\n",filename);
}
fseek(file,0,SEEK_END); //把文件内部指针移到末尾,offset为0,故指针停在文件末尾处
long fileSize = ftell(file); //记录文件长度,file指针相对于文件首的偏移量
fseek(file,0,SEEK_SET); //指针移到文件开头位置
if (file)
{
char buffer[1000];
fgets(buffer,300,file); //读取ply中数据,每次读一行到buffer中.
while ( strncmp( "element vertex", buffer,strlen("element vertex")) != 0 )//读取的这一行如果没有element vertex字符串,则继续读下一行,若有则跳出循环进行处理
{
fgets(buffer,300,file);
}
strcpy(buffer, buffer+strlen("element vertex")); //把是vertex的那一行的后面的点的总数记录下来
sscanf(buffer,"%d", &totalver); //从buffer中读数据,以十进制的形式存到vertexlist中
//sscanf(buffer,"%d", &totalnor);
fseek(file,0,SEEK_SET);
while ( strncmp( "element face", buffer,strlen("element face")) != 0 )
{
fgets(buffer,300,file);
}
strcpy(buffer, buffer+strlen("element face"));
sscanf(buffer,"%d", &totaltri);
while ( strncmp( "end_header", buffer,strlen("end_header")) != 0 )
{
fgets(buffer,300,file);
}
for (int iterator = 0; iterator < totalver; iterator++)
{
fgets(buffer,300,file);
vertexnum temp;
sscanf(buffer,"%f %f %f ", &temp.x, & temp.y, & temp.z);
//cout<<temp.x<<" "<<temp.y<<" "<<temp.z<<"\n";
vertexlist.push_back(temp);
}
for (int iterator = 0; iterator < totaltri; iterator++)
{
fgets(buffer,300,file);
if (buffer[0] == '3')
{
trinum tempt;
int num;
sscanf(buffer,"%d %d\t%d\t%d",&num,& tempt.x,&tempt.y,&tempt.z );
trilist.push_back(tempt);
}
}
float a[3]={0,0,0},b[3]={0,0,0},n[3]={0,0,0};
for (int nor = 0; nor < totaltri; nor++)
{
// glVertex3f(vertexlist[trilist[nor].x].x,vertexlist[trilist[nor].x].y,vertexlist[trilist[nor].x].z);
// glVertex3f(vertexlist[trilist[nor].y].x,vertexlist[trilist[nor].y].y,vertexlist[trilist[nor].y].z);
// glVertex3f(vertexlist[trilist[nor].z].x,vertexlist[trilist[nor].z].y,vertexlist[trilist[nor].z].z);
nornum norm;
/* a[3]=0;
b[3]=0;
n[3]=0;*/
a[0]=vertexlist[trilist[nor].y].x-vertexlist[trilist[nor].x].x;
a[1]=vertexlist[trilist[nor].y].y-vertexlist[trilist[nor].x].y;
a[2]=vertexlist[trilist[nor].y].z-vertexlist[trilist[nor].x].z;
b[0]=vertexlist[trilist[nor].z].x-vertexlist[trilist[nor].x].x;
b[1]=vertexlist[trilist[nor].z].y-vertexlist[trilist[nor].x].y;
b[2]=vertexlist[trilist[nor].z].z-vertexlist[trilist[nor].x].z;
n[0]=a[1]*b[2]-a[2]*b[1];
n[1]=a[2]*b[0]-a[0]*b[2];
n[2]=a[0]*b[1]-a[1]*b[0];
//cout<<a[0]<<"\n";
float d=sqrt(pow(n[0],2)+pow(n[1],2)+pow(n[2],2));
norm.x=n[0]/d;
//cout<<norm.x<<"\n";
norm.y=n[1]/d;
norm.z=n[2]/d;
norlist.push_back(norm);
}
fclose(file);
printf("%s Loaded!\n",filename);
}
else
{
printf("File can't be opened\n");
}
}
else
{
printf("File does not have a .PLY extension. ");
}
}
//vassist
void plyloaddraw::draw()
{
glColor3f(0,1,0);
//glPointSize(1.8);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_TRIANGLES);
int i;
for (i=0;i<totaltri;i++)
{
//glNormal3f(norlist[i].x,norlist[i].y,norlist[i].z);
glVertex3f(vertexlist[trilist[i].x].x,vertexlist[trilist[i].x].y,vertexlist[trilist[i].x].z);
glVertex3f(vertexlist[trilist[i].y].x,vertexlist[trilist[i].y].y,vertexlist[trilist[i].y].z);
glVertex3f(vertexlist[trilist[i].z].x,vertexlist[trilist[i].z].y,vertexlist[trilist[i].z].z);
}
glEnd();
}
Ply文件读取OpenGL显示
4星 · 超过85%的资源 需积分: 45 169 浏览量
2016-04-26
10:57:04
上传
评论 4
收藏 22.88MB RAR 举报
cl596332193
- 粉丝: 0
- 资源: 5
最新资源
- 第12章spring-mvc自定义类型转换器
- 基于PHP图书管理系统实验报告.docx
- Python爬取淘宝热卖商品并可视化分析
- 5152单片机proteus仿真和源码将按键次数写入AT24C02再读出并用1602LCD显示
- SE-SSD复现过程(Det3D的安装教程)
- 基于Python的在线学习与推荐系统设计与实现(论文+源码)-kaic
- 串口通过 YMODEM 协议进行文件传输
- 蓝桥杯2024年第十五届省赛真题-前缀总分
- com.qihoo.appstore_300101305-1.apk
- tensorflow-gpu-2.7.1-cp37-cp37m-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页