如何使用 Opengl 库从文本文件中显示 3D 点
Posted
技术标签:
【中文标题】如何使用 Opengl 库从文本文件中显示 3D 点【英文标题】:How to display the 3D point from text file by using Opengl library 【发布时间】:2017-03-18 12:28:41 【问题描述】:我想在文本文件中绘制由 column[0] 中的值指定的 3D 点。例如,column[0] 等于 2,然后显示 3D 点 (xyz) 如下: (1.529, 0.25,-2.038 ), (1.530,0.253,-2.040), (1.530,0.253,-2.044)
test.txt
1 1.529 0.253 -2.038
1 1.529 0.253 -2.038
2 1.529 0.253 -2.038
2 1.530 0.253 -2.040
2 1.530 0.253 -2.044
3 1.532 0.254 -2.038
3 1.533 0.255 -2.036
3 1.533 0.255 -2.036
3 1.534 0.255 -2.036
3 1.534 0.255 -2.036
这是我的代码
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
char title[] = "Point";
void initGL()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
void display()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
vector<vector<double>> tempdouble;
ifstream in("test.txt");
string line;
double Dstage1x = 0.0;
double Dstage1y = 0.0;
double Dstage1z = 0.0;
double Dstage2x = 0.0;
double Dstage2y = 0.0;
double Dstage2z = 0.0;
double Dstage3x = 0.0;
double Dstage3y = 0.0;
double Dstage3z = 0.0;
int ii = 0;
int stage1 = 0;
int stage2 = 0;
int stage3 = 0;
while (std::getline(in, line))
tempdouble.push_back(vector<double>());
stringstream ss(line);
double num;
while (ss >> num)
tempdouble.back().push_back(num);
for (int i = 0; i < tempdouble.size(); i++)
for (int j = 0; j < tempdouble[i].size(); j++)
int st1_x=0; int st1_y=0; int st1_z=0;
int st2_x=0; int st2_y=0; int st2_z=0;
int st3_x=0; int st3_y=0; int st3_z=0;
if(tempdouble[i][0]==1 )
glPointSize(5);glColor3f(1.0f, 0.5f, 0.0f);
glBegin(GL_POINTS);
st1_x += Dstage1x*0; st1_y += Dstage1y*0; st1_z += Dstage1z*0; //set value
Dstage1x = st1_x + tempdouble[i][1];
Dstage1y = st1_y + tempdouble[i][2];
Dstage1z = st1_z + tempdouble[i][3];
stage1++;
//cout <<"Check tempdouble[i][0] state1 : " << tempdouble[i][0] <<"\t"<<endl;
cout << "state1(xyz) : " << "( " << Dstage1x << " ," << Dstage1y << "," << Dstage1z << ")"<< endl;
glVertex3f(Dstage1x, Dstage1y, Dstage1z);
//glVertex3f(tempdouble[i][1], tempdouble[i][2], tempdouble[i][3]);
//glVertex3f(tempdouble[i][j], tempdouble[i][j], tempdouble[i][j]);
glEnd();
break;
if (tempdouble[i][0] == 2)
glPointSize(5);glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POINTS);
st2_x += Dstage2x * 0; st2_y += Dstage2y*0; st2_z += Dstage2z*0; //set value
Dstage2x = st2_x + tempdouble[i][1];
Dstage2y = st2_y + tempdouble[i][2];
Dstage2z = st2_z + tempdouble[i][3];
stage2++;
cout << "state2(xyz) : " << "( " << Dstage2x << " ," << Dstage2y << "," << Dstage2z << ")"<< endl;
glVertex3f(Dstage2x,Dstage2y,Dstage2z);
//glVertex3f(tempdouble[i][1], tempdouble[i][2], tempdouble[i][3]);
//glVertex3f(tempdouble[i][j], tempdouble[i][j], tempdouble[i][j]);
glEnd();
break;
if (tempdouble[i][0] == 3)
glPointSize(5); glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
st3_x += Dstage3x*0; st3_y += Dstage3y*0; st3_z += Dstage3z*0; //set value
Dstage3x = st3_x + tempdouble[i][1];
Dstage3y = st3_y + tempdouble[i][2];
Dstage3z = st3_z + tempdouble[i][3];
stage3++;
cout << "state3(xyz) : " << "( " << Dstage3x << " ," << Dstage3y << "," << Dstage3z << ")"<< endl;
glVertex3f(Dstage3x,Dstage3y,Dstage3z);
//glVertex3f(tempdouble[i][0],tempdouble[i][1],tempdouble[i][2]);
//glVertex3f(tempdouble[i][j], tempdouble[i][j], tempdouble[i][j]);
glEnd();
break;
cout << "stage 1: "<< stage1 << endl;
cout << "stage 2: "<<stage2 << endl;
cout << "stage 3: "<<stage3 << endl;
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
void reshape(GLsizei width, GLsizei height) // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
void changeViewPort(int w, int h)
glViewport(0, 0, w, h);
void render()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
int main(int argc, char* argv[])
void display();
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL(); // Our own OpenGL initialization
glutMainLoop();
return 0;
我的问题是点显示在错误的位置。列 [0](等于 1,2,3)不可能有所有相同的点。有人可以帮我解决这个问题
【问题讨论】:
【参考方案1】:setlocale( LC_NUMERIC, "C");
.
【讨论】:
如果您能改进您的答案以便 OP 可以对它做任何事情,那就太好了。【参考方案2】:https://rocketgit.com/user/bowler17/gl/source/tree/branch/wrench/blob/t.c
大号do while
之前的一行
【讨论】:
你在大做之前说过那句话。你指的是哪一行?以上是关于如何使用 Opengl 库从文本文件中显示 3D 点的主要内容,如果未能解决你的问题,请参考以下文章
OpenGL - 在 3D 世界的屏幕上固定位置打印 2D 文本
如何使用用于 iPhone 开发的 OpenGL ES 渲染 3D 对象(顶点和法线存储在头文件中)?
如何使用 Horde3d 和 OpenGL 绘制 Qt 小部件?