簡體   English   中英

通過讀取文本文件在 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

您的點被透視投影錐體的近遠平面裁剪 所有不在近平面和遠平面之間的幾何體被剪裁:

增加近平面和遠平面之間的距離 ( gluPerspective ) 並更改場景的外觀 ( 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);  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM