简体   繁体   中英

Displaying 3D points in OpenGL by reading in textfile

I am trying to plot 3D points by reading in a series of coordinates contained in a text file but I don't seem to get any output.

I personally think that either I am doing something wrong while reading my text file or in void reshape

Here is my code so far:

#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;

}

And here is the text file:

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

Your points are clipped by the near an far plane of the Viewing frustum of the perspective projection . Alle the geometry which is not in between the near and far plane is clipped:

Increase the distance between the near and far plane ( gluPerspective ) and change the look at ( gluLookAt ) the scene:

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);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM