通过读取文本文件在 OpenGL 中显示 3D 点
Displaying 3D points in OpenGL by reading in textfile
我试图通过读取文本文件中包含的一系列坐标来绘制 3D 点,但我似乎没有得到任何输出。
我个人认为要么是我在读取文本文件时做错了,要么是在 void reshape 中做错了
到目前为止,这是我的代码:
#include <glut.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
struct POINTS
{
int n;
float x;
float y;
float z;
};
POINTS point[1371];
void initGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-200, 0.0, 0.0, 200, 0.0, 300);
}
void display()
{
ifstream f;
f.open("points.txt");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2);
glBegin(GL_POINTS);
for (int i = 0; i < 1371; ++i)
{
f >> point[i].n >> point[i].x >> point[i].y >> point[i].z;
cout << point[i].n << " " << point[i].x << " " << point[i].y << " " << point[i].z << endl;
glVertex3f(point[i].x, point[i].y, point[i].z);
}
glEnd();
glFlush();
f.close();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-117.564, 36.7301, -5.0, -117.564, 36.7301, 151.769, 0, 1, 0);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA
| GLUT_SINGLE | GLUT_MULTISAMPLE); // initialize GLUT
glutInitWindowSize(640, 480); // set the window size
glutInitWindowPosition(100, 100); // set display-window width and height to 100
glutCreateWindow("Plotting a series of 3D Points");
glutReshapeFunc(reshape);
glutDisplayFunc(display); // send graphics to display window
initGL();
glutMainLoop();
return 0;
}
这是文本文件:
0 -117.6027 161.5286 70.5128
1 -82.9727 87.7585 107.0592
2 -117.6027 72.2113 106.0432
3 -92.2141 80.0949 116.0134
4 -86.2138 96.987 122.6796
5 -102.6702 75.0957 108.7022
6 -58.8401 129.0492 72.169
7 -75.3688 91.5178 93.905
8 -97.0844 22.4057 115.8543
9 -101.1874 18.6077 127.3053
10 -111.0116 13.8925 122.3735
您的点被 Viewing frustum of the perspective projection 的近远平面裁剪。所有不在近平面和远平面之间的几何体都被剪裁:
增加近远平面(gluPerspective
) and change the look at (gluLookAt
)场景的距离:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-117.564, 36.7301, -1.0, -117.564, 36.7301, 0.0, 0, 1, 0);
我试图通过读取文本文件中包含的一系列坐标来绘制 3D 点,但我似乎没有得到任何输出。
我个人认为要么是我在读取文本文件时做错了,要么是在 void reshape 中做错了
到目前为止,这是我的代码:
#include <glut.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
struct POINTS
{
int n;
float x;
float y;
float z;
};
POINTS point[1371];
void initGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-200, 0.0, 0.0, 200, 0.0, 300);
}
void display()
{
ifstream f;
f.open("points.txt");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2);
glBegin(GL_POINTS);
for (int i = 0; i < 1371; ++i)
{
f >> point[i].n >> point[i].x >> point[i].y >> point[i].z;
cout << point[i].n << " " << point[i].x << " " << point[i].y << " " << point[i].z << endl;
glVertex3f(point[i].x, point[i].y, point[i].z);
}
glEnd();
glFlush();
f.close();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-117.564, 36.7301, -5.0, -117.564, 36.7301, 151.769, 0, 1, 0);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA
| GLUT_SINGLE | GLUT_MULTISAMPLE); // initialize GLUT
glutInitWindowSize(640, 480); // set the window size
glutInitWindowPosition(100, 100); // set display-window width and height to 100
glutCreateWindow("Plotting a series of 3D Points");
glutReshapeFunc(reshape);
glutDisplayFunc(display); // send graphics to display window
initGL();
glutMainLoop();
return 0;
}
这是文本文件:
0 -117.6027 161.5286 70.5128
1 -82.9727 87.7585 107.0592
2 -117.6027 72.2113 106.0432
3 -92.2141 80.0949 116.0134
4 -86.2138 96.987 122.6796
5 -102.6702 75.0957 108.7022
6 -58.8401 129.0492 72.169
7 -75.3688 91.5178 93.905
8 -97.0844 22.4057 115.8543
9 -101.1874 18.6077 127.3053
10 -111.0116 13.8925 122.3735
您的点被 Viewing frustum of the perspective projection 的近远平面裁剪。所有不在近平面和远平面之间的几何体都被剪裁:
增加近远平面(gluPerspective
) and change the look at (gluLookAt
)场景的距离:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-117.564, 36.7301, -1.0, -117.564, 36.7301, 0.0, 0, 1, 0);