gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:26:24 27 4
gpt4 key购买 nike

我试图通过读取文本文件中包含的一系列坐标来绘制 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 的近远平面裁剪。的perspective projection .所有不在近平面和远平面之间的几何图形都被剪裁:

增加近平面和远平面之间的距离( 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);

关于c++ - 通过读取文本文件在 OpenGL 中显示 3D 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64321024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com