PDA

View Full Version : Load point from file



chicken
01-13-2011, 05:40 AM
I have a text file : (data.txt)
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4

I want draw 5 Points from this file into screen (http://nehe.gamedev.net/data/lessons/vc/lesson26.zip =>I want to draws 5 points in the floor, and move ball across that 5 points)
My code:
#include ...
using namespace std;
char* filename="data.txt";
int** a;
int rows,cols;
void readfile()
{
int a[5][3];
char* filename="data.txt";
FILE* f;
f=fopen(filename,"r");
for(int j=0;j<2;j++)
for(int k=0;k<4;k++)
fscanf(f,"%d",&amp;a[j][k]);

}

=> readfile function is ok! , but when i call funntion :

void drawpoints()
{ glclear(..)
glPointsize(..)
readfile(); //call readfile funntion :
glBegin(GL_POINTS);
glColor3f(2.0f, 1.0f, 0.0f);
glVertex3f(a[0][0],a[0][1],a[0][2]);
glVertex3f(a[1][0],a[1][1],a[1][2]);
...
glVertex3f(a[4][0],a[4][1],a[4][2]);

glEnd();




}



Result :

http://img513.imageshack.us/i/80952197.jpg
or :
http://img513.imageshack.us/i/80952197.jpg

mhagain
01-13-2011, 05:56 AM
a[j][k] -> j goes from 0 to 1 (2 elements) and k goes from 0 to 3 (4 elements), yet the array is declared as [5][3], so you've got your array indexes the wrong way around, you're overflowing one of them and you're not reading in enough overall.

Even fixing this won't work as you have the variable 'a' declared twice; once in global scope and once in local scope, and you're never assigning anything to the one in global scope. You're also doing the same with the variable 'filename'.

My advice - without wanting to be rude, I really think you need to learn some C/C++ before trying to code OpenGL.

chicken
01-13-2011, 06:24 AM
ok, this is readfile() function:

void ReadFile()
{
FILE* f;
f=fopen(filename,"r");
fscanf(f,"%d",&amp;rows);
fscanf(f,"%d",&amp;cols);
a = new int*[rows];
for(int i=0;i<rows;i++)
a[i]=new int[cols];
for(int j=0;j<rows;j++)
for(int k=0;k<cols;k++)
fscanf(f,"%d",&amp;a[j][k]);

}
ok?
But OPenGL project don't work...