读取32M的文件需要10秒左右,使用c语言会快很多。代码有些繁琐。
dragonShow.cpp
#include "freeglut.h"
#include "ReadTxtFIle.h"
#include "Point3f.h"
#include "GlutWin.h"
#pragma comment(lib,"freeglut.lib")
vector pointListt;
//double gVertexNum;
//double gEdgeNum;
int gVertexNum;
int gEdgeNum;
GlutWin *win;
using namespace std;
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
cout << "正在绘制。。" << endl;
cout << pointListt.at(0).GetParamX()<< endl;
cout << pointListt.at(1).GetParamY()<< endl;
cout << pointListt.at(2).GetParamZ() << endl;
/*glBegin(GL_TRIANGLES);
glVertex3f(0.032027, 0.056575, -0.050364);
glVertex3f(0.031798, 0.056575, -0.050338);
glVertex3f(0.032377, 0.056564, -0.050338);
glEnd();*/
static double theta = 0;
theta += 0.05;
glColor3d(1, 0.5, 1);
glRotated(theta, 0, 1, 0);
glBegin(GL_TRIANGLES);
//for (int i = 0; i < gVertexNum - 2; i++)
//{
// glVertex3f(pointListt.at(i+0).GetParamX(), pointListt.at(i+0).GetParamY(), pointListt.at(i+0).GetParamZ());
// glVertex3f(pointListt.at(i+1).GetParamX(), pointListt.at(i+1).GetParamY(), pointListt.at(i+1).GetParamZ());
// glVertex3f(pointListt.at(i+2).GetParamX(), pointListt.at(i+2).GetParamY(), pointListt.at(i+2).GetParamZ());
//}
for (vector ::iterator it = pointListt.begin(); it != pointListt.end(); it++)
{
glVertex3f(it->GetParamX(),it->GetParamY(),it->GetParamZ());
/*glVertex3f( (it+1)->GetParamX(), (it+1)->GetParamY(), (it+1)->GetParamZ() ) ;
glVertex3f( (it+2)->GetParamX(), (it+2)->GetParamY(), (it+2)->GetParamZ() );*/
}
glEnd();
glFlush();
glutSwapBuffers();
}
void myReshape(GLsizei width, GLsizei height)
{
const float ar = (float)width / (GLfloat)height;
if (height == 0)
{
height = 1;
}
//投影->视口变换
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#if 0
glOrtho(-1.0, 1.0, -1.0 / ar, 1.0 / ar, -1.0, 1.0);
#else
gluPerspective(45.0, ar, 0.1, 100.0);
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
#endif // 透视投影、平行投影
glScalef(10.0, 10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void myidle()
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
ReadTxtFile DPoint;
cout << "加载数据中..." << endl;
pointListt = DPoint.Get3DPoint(); //
if (pointListt.empty())
{
cerr << "加载数据为空" << endl;
}
gVertexNum = DPoint.getVertexNum();
//gVertexNum = DPoint.getVertexNum();
//gEdgeNum = DPoint.getEdgeNum();
//cout << "顶点个数:" << gVertexNum << endl;
//cout << "面片个数:" << gEdgeNum << endl;
cout << "加载已运行" << DPoint.getLoadTime() << "s" << endl;
win = new GlutWin(600, 800,100, 100, GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH, FILENAME);
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutIdleFunc(myidle);
glutMainLoop();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); //如果输入的深度值小于参考值,则通过
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL); //开启颜色追踪
delete win;
return 0;
}
Point3f.h
#pragma once
#include "freeglut.h"
//-----------------------------顶点结构体----------------------------------
struct Vertex //顶点结构
{
Vertex() {}
Vertex(float x, float y, float z)
{
_x = x; _y = y; _z = z;
}
float _x, _y, _z;
};
class Point3f
{
public:
Point3f();
Point3f(float _x, float _y, float _z);
int SetParam(float _x, float _y, float _z);
GLfloat GetParamX();
GLfloat GetParamY();
GLfloat GetParamZ();
inline Vertex IVertex()
{
return Vertex(x, y, z);
}
private:
float x, y, z;
};
Point3f.cpp
#pragma once
#include"Point3f.h"
Point3f::Point3f() :x(0), y(0), z(0)
{
}
Point3f::Point3f(float _x, float _y, float _z) : x(_x), y(_y), z(_z)
{
}
int Point3f::SetParam(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
return 0;
}
GLfloat Point3f::GetParamX()
{
return x;
}
GLfloat Point3f::GetParamY()
{
return y;
}
GLfloat Point3f::GetParamZ()
{
return z;
}
#pragma once
#include
#include
#include
#include
#include
#include
#include "Point3f.h"
#if 0
#define FILENAME "bunny.txt"
#else
#define FILENAME "dragon.txt"
#endif
#define BUFFERLINE 50 //读取一行大小
using namespace std;
class ReadTxtFile
{
public:
ReadTxtFile();
~ReadTxtFile();
vector Get3DPoint(const char *fileName);
vector Get3DPoint();
/*double getVertexNum();
double getEdgeNum();
double formatConvert(char *m_line);
*/
int getVertexNum() const;
int getEdgeNum() const;
time_t getLoadTime() const;
inline void closeFile()
{
m_fin->clear();
m_fin->close();
}
private:
string m_x,m_y,m_z;
time_t m_t1;
time_t m_t2;
//const char * m_VertexNumchar;
//const char * m_EdgeNumchar;
//const char * m_Numchar; //用于格式转换
//double m_Num;
//double m_VertexNum; //顶点数
//double m_EdgeNum; //面片数
//
int m_vertNum;
int m_triNum;
ifstream *m_fin;
char m_line[BUFFERLINE]; //读一行所用数组
vector m_pointList;
//vector m_pointList;
};
ReadTxtFIle.cpp
#pragma once
#include "ReadTxtFIle.h"
ReadTxtFile::ReadTxtFile() :m_x(""), m_y(""), m_z(""), m_vertNum(NULL), m_triNum(NULL)
{
*m_line = { 0 };
m_fin = new ifstream(FILENAME, ios::in);
}
ReadTxtFile::~ReadTxtFile()
{
}
vector ReadTxtFile::Get3DPoint(const char *fileName)
{
int i = 0;
int vertNum;
int triNum;
float x, y, z;
*m_fin >> vertNum ;
*m_fin >> triNum;
time(&m_t1);
for (int i = 0; i < vertNum; i++)
{
*m_fin >> x >> y >> z;
m_pointList.push_back(Point3f(x, y, z));
}
time(&m_t2);
//读取文件
//time(&m_t1);
//while (m_fin->getline(m_line, sizeof(m_line)))
//{
// if (i < 1)
// {
// m_VertexNum = formatConvert(m_line);
// i += 1;
// continue;
// }
// if (i == 1)
// {
// m_EdgeNum = formatConvert(m_line);
// i += 1;
// continue;
// }
// stringstream word(m_line);
// word >> m_x;
// word >> m_y;
// word >> m_z;
// const char * xChar = m_x.c_str();
// const char * yChar = m_y.c_str();
// const char * zChar = m_z.c_str();
// m_pointList.push_back(Point3f(atof(xChar), atof(yChar), atof(zChar)));
// //pointList.at(i - 2).SetParam(atof(xChar), atof(xChar), atof(xChar));
// i++;
//}
//time(&m_t2);
closeFile();
return m_pointList;
}
vector ReadTxtFile::Get3DPoint()
{
int i = 0;
float x, y, z;
*m_fin >> m_vertNum;
*m_fin >> m_triNum;
time(&m_t1);
for (int i = 0; i < m_vertNum; i++)
{
*m_fin >> x >> y >> z;
m_pointList.push_back(Point3f(x, y, z));
}
time(&m_t2);
////读取文件
//time(&m_t1);
//while (m_fin->getline(m_line, sizeof(m_line)))
//{
// if (i < 1)
// {
// m_VertexNum = formatConvert(m_line);
// i += 1;
// continue;
// }
// if (i == 1)
// {
// m_EdgeNum = formatConvert(m_line);
// i += 1;
// continue;
// }
// stringstream word(m_line);
// word >> m_x;
// word >> m_y;
// word >> m_z;
// const char * xChar = m_x.c_str();
// const char * yChar = m_y.c_str();
// const char * zChar = m_z.c_str();
// m_pointList.push_back(Point3f(atof(xChar), atof(yChar), atof(zChar)));
// //pointList.at(i - 2).SetParam(atof(xChar), atof(xChar), atof(xChar));
// i++;
//}
//time(&m_t2);
closeFile();
return m_pointList;
}
//double ReadTxtFile::getVertexNum()
//{
// return m_VertexNum;
//}
//
//double ReadTxtFile::getEdgeNum()
//{
// return m_EdgeNum;
//}
int ReadTxtFile::getVertexNum() const
{
return m_vertNum;
}
int ReadTxtFile::getEdgeNum() const
{
return m_triNum;
}
time_t ReadTxtFile::getLoadTime() const
{
return (m_t2 - m_t1);
}
//double ReadTxtFile::formatConvert(char *m_line)
//{
// string s(&m_line[0], &m_line[strlen(m_line)]); //char *[]转string转char*
// m_Numchar = s.c_str();
// m_Num = atof(m_Numchar);
//
// return m_Num;
//}
GlutWin.h
#pragma once
#include
#include "freeglut.h"
//Glut初始化
class GlutWin
{
public:
GlutWin(int windowHeight, int windowWidth,int windowPosX, int windowPosY,
unsigned int displayMode,const char * windowTitle);
~GlutWin() {};
private:
const char * windowTitle;
int windowHeight, windowWidth;
int windowPosX, windowPosY;
int windowID;
unsigned int displayMode;
bool fullScreen;
};
GlutWin.cpp
#include "GlutWin.h"
GlutWin::GlutWin(int windowHeight, int windowWidth,int windowPosX, int windowPosY,
unsigned int displayMode,const char * windowTitle)
{
windowTitle = windowTitle;
windowHeight = windowHeight;
windowWidth = windowWidth;
windowPosX = windowPosX;
windowPosY = windowPosY;
displayMode = displayMode;
fullScreen = false;
char cmd_line[8];
char * argv[1];
argv[0] = cmd_line;
int argc = 1;
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(windowPosX, windowPosY);
glutInitDisplayMode(displayMode);
windowID = glutCreateWindow(windowTitle);
const float ar = (float)windowWidth / (GLfloat)windowHeight;
#if 0
glOrtho(-1.0, 1.0, -1.0 / ar, 1.0 / ar, -1.0, 1.0);
#else
gluPerspective(45.0, ar, 0.1, 100.0);
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
#endif // 透视投影、平行投影
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glScalef(10.0, 10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
三维点:http://download.csdn.net/detail/tingzhushaohua/9814466