GLfloat vertices from txt file

Started by
14 comments, last by m_power_hax 13 years, 4 months ago
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.
Advertisement
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.
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?
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.
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].
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.

This topic is closed to new replies.

Advertisement