通过读取文本文件在OpenGL中显示3D点

问题描述

我试图通过读取文本文件中包含的一系列坐标来绘制3D点,但是我似乎没有任何输出

我个人认为阅读文本文件时做错了什么,或者整形无效

到目前为止,这是我的代码

#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);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(-200,200,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,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(65.0,(GLfloat)w / (GLfloat)h,20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(-117.564,36.7301,-5.0,-117.564,151.769,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 frustumperspective projection的近平面所裁剪。剪掉不在近平面和远平面之间的所有几何图形:

增大近距平面(gluPerspective)之间的距离,并更改(gluLookAt)场景的外观:

const [value,setValue] = useState(props.selectedNote && props.selectedNote.content);