我想繪製由文本文件中列[0]的值指定的3D點。例如,列[0]等於2,然後顯示以下三維點(xyz):(1.529,0.25,-2.038),(1.530,0.253,-2.040),(1.530,0.253,-2.044)如何使用Opengl庫從文本文件顯示3D點
的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)都相同points.Can有人能幫我解決這個
你說的那一行之前做的很大。你的意思是哪一行? – VioletnRose