Sign in to follow this  
m_power_hax

GLfloat vertices from txt file

Recommended Posts

Hi

Here the code for loading data from a txt file to a normal array. What i'm trying to do now, is to load that data to a GLfloat vertice[].

#include <iostream>
#include <fstream>
using namespace std;

float vertices[2999];

void CreateDataFile()
{
ofstream out("testfile.txt");
for (float x=0;x<10;x++)
for (float y=0;y<10;y++)
for (float z=0;z<10;z++)
{
if ((x == 9) && (y == 9) && (z == 9))
{
out << x*0.123456 << " " << y*0.123456 << " " << z*0.123456;
}
else
out << x*0.123456 << " " << y*0.123456 << " " << z*0.123456 << "\n";
}
}

int main()
{
CreateDataFile();

ifstream loadData("testfile.txt");

if (!loadData)
{
cout<< "Can't open file!\n";
return 1;
}
else
{
int j = 0;
cout.precision(2);
while (loadData >> vertices[j])
{
//cout << vertices[j] << ",";
++j;
}
//cout << j;
cout << vertices[1];
}
loadData.close();
return 0;
}


I tried it for a GLfloat vertices array, but it didnt work. Could someone help find the best way to upload data in a GLfloat vertice[]?
Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by dpadam450
10*10*10 = 10000 floats, why do you have 2999? Is 1/3 of the model showing?


because there are x,y,z, each going from 0 to 9, which make 1000 points, since there are 3 coordinates per point, that make an array of 2999 (0 count for 1). There is only a few vertices showing, i didnt count them but, only one plane is draw, maybe 20 points total.

Share this post


Link to post
Share on other sites
Quote:
Original post by karwosts
Quote:

but it didnt work.

Can you be more specific?


well i think specifying the GLfloat like this : GLfloat vertices[] = {0.5,0.5,0.5 , 0.5,1.0,0.5 , 0.5,0.5,1.0 , ...} is different than what my code is doing when loading the data from the txt file. The GLfloat def know what value are x, y ,z.

So what should i change in the code?

Share this post


Link to post
Share on other sites
What kind of "it doesn't work"? Compiler error? Linker error? Runtime error? Running but not producing the correct output?

Offtopic gripe: Also, why do I keep seeing/hearing people who think the singular of 'vertices' is 'vertice'? It's 'vertex'. Plurals for words ending in -ex end in -ices.

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Quote:
Original post by dpadam450
10*10*10 = 10000 floats, why do you have 2999? Is 1/3 of the model showing?


because there are x,y,z, each going from 0 to 9, which make 1000 points, since there are 3 coordinates per point, that make an array of 2999 (0 count for 1). There is only a few vertices showing, i didnt count them but, only one plane is draw, maybe 20 points total.


the last index in a float foo[2999] array is 2998 so you still only have room for 2999 elements.

using glfloat instead of float shouldn't change anything, for most OpenGL implementations glfloat and float will be the exact same thing.

The code you have fills the array properly but one element is written out of bounds, chainging float vertices[2999] to float vertices[3000] should solve that, but using a dynamic container (such as a vector) would be preferable)

If correcting the array size doesn't work your problem lies in code you havn't posted yet.

Share this post


Link to post
Share on other sites
If you don't know a programming language, then you should learn that first. Learn to use its IO features. Learn all the basics of that language. Then learn about GL. Also, your question isn't about GL.

Share this post


Link to post
Share on other sites
Quote:
Original post by Aken H Bosch
What kind of "it doesn't work"? Compiler error? Linker error? Runtime error? Running but not producing the correct output?

Offtopic gripe: Also, why do I keep seeing/hearing people who think the singular of 'vertices' is 'vertice'? It's 'vertex'. Plurals for words ending in -ex end in -ices.


Running but not producing the correct output. When using the normal GLfloat call in my program, it can draw correctly a number x number x number point sprites. When using the vertices array from the text file, only one plane is draw (number x number) and that plane is not of the right dimension, alot of point sprites are missing in that plane. I hope it's more clear now.

Share this post


Link to post
Share on other sites
Quote:
Original post by dpadam450
Right about the size of the array. But you really need to show us a pic or something of what is going on. What is your drawing code?


That's a good idea, i'll try to take a screenshot so you guys can see what's going on.

Share this post


Link to post
Share on other sites
Quote:
Original post by SimonForsman
Quote:
Original post by m_power_hax
Quote:
Original post by dpadam450
10*10*10 = 10000 floats, why do you have 2999? Is 1/3 of the model showing?


because there are x,y,z, each going from 0 to 9, which make 1000 points, since there are 3 coordinates per point, that make an array of 2999 (0 count for 1). There is only a few vertices showing, i didnt count them but, only one plane is draw, maybe 20 points total.


the last index in a float foo[2999] array is 2998 so you still only have room for 2999 elements.

using glfloat instead of float shouldn't change anything, for most OpenGL implementations glfloat and float will be the exact same thing.

The code you have fills the array properly but one element is written out of bounds, chainging float vertices[2999] to float vertices[3000] should solve that, but using a dynamic container (such as a vector) would be preferable)

If correcting the array size doesn't work your problem lies in code you havn't posted yet.


Changing it to vertices[3000] didnt change anything. I don't understand why you say there is an element in [3000]. When i call a cout << vertices[3000], i don't get the last value, i do get the last value with vertices[2999]. Also, the ifstream << vertices stop at 2999 and not 3000.

Share this post


Link to post
Share on other sites
i'm in a hurry but

if ((x <= 9) && (y <= 9) && (z <= 9))
{
out << x*0.123456 << " " << y*0.123456 << " " << z*0.123456;
}
else
out << x*0.123456 << " " << y*0.123456 << " " << z*0.123456 << "\n";


is the correct way to do this isn't it?

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Quote:
Original post by SimonForsman
Quote:
Original post by m_power_hax
Quote:
Original post by dpadam450
10*10*10 = 10000 floats, why do you have 2999? Is 1/3 of the model showing?


because there are x,y,z, each going from 0 to 9, which make 1000 points, since there are 3 coordinates per point, that make an array of 2999 (0 count for 1). There is only a few vertices showing, i didnt count them but, only one plane is draw, maybe 20 points total.


the last index in a float foo[2999] array is 2998 so you still only have room for 2999 elements.

using glfloat instead of float shouldn't change anything, for most OpenGL implementations glfloat and float will be the exact same thing.

The code you have fills the array properly but one element is written out of bounds, chainging float vertices[2999] to float vertices[3000] should solve that, but using a dynamic container (such as a vector) would be preferable)

If correcting the array size doesn't work your problem lies in code you havn't posted yet.


Changing it to vertices[3000] didnt change anything. I don't understand why you say there is an element in [3000]. When i call a cout << vertices[3000], i don't get the last value, i do get the last value with vertices[2999]. Also, the ifstream << vertices stop at 2999 and not 3000.

float vertices[3000] creates an array of 3000 floats, and there are 3000 indices you can use to access the array, ranging from 0 to 2999 inclusive. float vertices[2999] creates an array of 2999 floats whose index ranges from 0 to 2998 inclusive. Read up on arrays and how you index them.

Share this post


Link to post
Share on other sites
Quote:

Changing it to vertices[3000] didnt change anything.

It shaved off a little undefined behaviour, which is always a good thing. When you wrote to vertices[2999], you were overwriting some piece of memory.

Think about it. An integer array with one element has a declaration: int array[1]; and its only element is at: int x = array[0];. For two elements, the declaration is int array[2]; and the elements are at array[0] and array[1].

Logically adding N to this and generalising the types you can see that for a declaration of Type array[N]; you can index to array[0] to array[N - 1].

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:

Changing it to vertices[3000] didnt change anything.

It shaved off a little undefined behaviour, which is always a good thing. When you wrote to vertices[2999], you were overwriting some piece of memory.

Think about it. An integer array with one element has a declaration: int array[1]; and its only element is at: int x = array[0];. For two elements, the declaration is int array[2]; and the elements are at array[0] and array[1].

Logically adding N to this and generalising the types you can see that for a declaration of Type array[N]; you can index to array[0] to array[N - 1].


Ok that's clear. I'm calling the vertices[3000] now. I found the error, and it was not in the file loading code. It was in the drawing code, which didnt have the right number of points to draw.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this