[记录]使用openGL显示点云的一个程序

时间:2022-07-12 15:32:46
 #include <GL/glut.h>
#include <stdio.h>
#include <iostream>
using namespace std;
void myDisplay(void);
//char * filename=(char *)"d:/aaacpp/点云数据转换/data/人体点云数据_out.asc";
char * filename=NULL; static GLfloat xRot=0.0f;
static GLfloat yRot=0.0f;
int n; float xTmp[];
float yTmp[];
float zTmp[]; void getPoint(char * filename,float * pX,float * pY,float * pZ,int & n)
{
FILE * fp=NULL;
n=;
if (fp = fopen(filename, "r"), fp == NULL)
{
cout << "文件打开失败,文件路径:" << filename << endl;
exit();
}
while(!feof(fp))
{
fscanf(fp,"%f %f %f\n",&pX[n],&pY[n],&pZ[n]);
++n;
//glVertex3f(x/2,y/2,z/2);
}
} void printPoint(float * pX,float * pY,float * pZ,int n)
{
int i=;
float x,y,z;
while(i<n)
{
x=pX[i];
y=pY[i];
z=pZ[i];
glVertex4f(x,y,z,2.0);
++i;
}
} void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot,1.0,0.0,0.0);
glRotatef(yRot,0.0,1.0,0.0);
glBegin(GL_POINTS);
glColor3f(1.0,1.0,1.0);//白色
//glVertex2f(0.0f,0.0f);
//glVertex2f(0.5f,0.8f); printPoint(xTmp,yTmp,zTmp,n);
glEnd();
glBegin(GL_LINES);//glBegin和glEnd保证指定的这些点有效
glColor3f(1.0,0.0,0.0);//红色
glVertex3f(0.0,0.0,0.0);
glVertex3f(1.0,0.0,0.0);
glColor3f(0.0,1.0,0.0);//绿色
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);//蓝色
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,0.0,1.0);
glEnd();
glFlush();//保证前面的命令立即执行
glPopMatrix();
glutSwapBuffers();
} void rDisplay()
{
static int i=;
++i;
glPushMatrix();
glRotatef(0.1*i,1.0f,0.0f,0.0f);
//draw();
glPopMatrix();
glFlush();
//glutSwapBuffers();
} void SpecialKeys(int key,int x,int y)
{
float span=5.0;
switch(key)
{
case GLUT_KEY_UP:
xRot-=span;
break;
case GLUT_KEY_DOWN:
xRot+=span;
break;
case GLUT_KEY_LEFT:
yRot-=span;
break;
case GLUT_KEY_RIGHT:
yRot+=span;
break;
default:
break;
}
xRot=(xRot>360.0)?(xRot-360.0):xRot;
xRot=(xRot<0.0)?(xRot+360.0):xRot;
yRot=(yRot>360.0)?(yRot-360.0):yRot;
yRot=(yRot<0.0)?(yRot+360.0):yRot;
glutPostRedisplay();
} int main(int argc,char* argv[])
{
glutInit(&argc,argv);
filename=(argc==)?argv[]:NULL;
getPoint(filename,xTmp,yTmp,zTmp,n);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("Three Window");//直到glutMainLoop才显示窗口
glPointSize(1.0);
glLineWidth(1.0);
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
glutSwapBuffers();
glViewport(, , , );
glMatrixMode(GL_MODELVIEW);//设置当前操作的矩阵为模型视图矩阵
//glLoadIdentity();//使当前绘图坐标系与世界坐标系重合
//glTranslatef(0.5,0.5,0.5);//使绘图坐标系相对世界坐标系沿xyz移动
glutDisplayFunc(&Display);
glutSpecialFunc(&SpecialKeys);
//glutIdleFunc(&rDisplay);
glutMainLoop();
return ;
} void myDisplay(void)
{
static int i=;
++i;
glEnable(GL_DEPTH_TEST);
glColor3f(1.0f,,);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(75,1,1,40000000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(0.01*i,1.0f,0.0f,0.0f);
//gluLookAt(0,-20000000,-20000000,0,0,0,0,0,1);
glPolygonMode(GL_FRONT, GL_FILL); // 设置正面为填充模式
glPolygonMode(GL_BACK, GL_LINE); // 设置反面为线形模式
glFrontFace(GL_CCW); // 设置逆时针方向为正面
glBegin(GL_POLYGON); // 按逆时针绘制一个正方形,在左下方
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, -0.5f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.5f, 0.0f);
glEnd();
glBegin(GL_POLYGON); // 按顺时针绘制一个正方形,在右上方
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(0.5f, 0.0f);
glEnd(); glFlush();
glutSwapBuffers();
}