读取32M的文件需要10秒左右,使用c语言会快很多。代码有些繁琐。
绘制
dragonShow.cpp
#include "freeglut.h"
#include "ReadTxtFIle.h"
#include "Point3f.h"
#include "GlutWin.h"
#pragma comment(lib,"freeglut.lib")
vector<Point3f> pointListt;
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;
static double theta =
0;
theta +=
0.05;
glColor3d(
1,
0.5,
1);
glRotated(theta,
0,
1,
0);
glBegin(GL_TRIANGLES);
for (
vector<Point3f>::iterator it = pointListt.begin(); it != pointListt.end(); it++)
{
glVertex3f(it->GetParamX(),it->GetParamY(),it->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();
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 <iostream>
#include <time.h>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#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<Point3f> Get3DPoint(
const char *fileName);
vector<Point3f> Get3DPoint();
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;
int m_vertNum;
int m_triNum;
ifstream *m_fin;
char m_line[BUFFERLINE];
vector<Point3f> 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<Point3f> 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);
closeFile();
return m_pointList;
}
vector<Point3f> 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);
closeFile();
return m_pointList;
}
int ReadTxtFile::getVertexNum()
const
{
return m_vertNum;
}
int ReadTxtFile::getEdgeNum()
const
{
return m_triNum;
}
time_t ReadTxtFile::getLoadTime()
const
{
return (m_t2 - m_t1);
}
绘制窗口
GlutWin.h
#pragma once
#include <windows.h>
#include "freeglut.h"
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