-
December 17th, 2010, 01:34 PM
#1
Text file to array
I'm trying to make my code work with pointers, but can't find what is wrong. The first one is working correctly, with global variable declared with the size of the array. The second one are pointers, and can't find what is wrong, because i get no errors, the drawing is just not done.
Code:
//global
#define sizeVertices = 6291456;
GLfloat g_vertices[sizeVertices];
GLfloat g_colors[sizeVertices];
bool Init()
{
...
ifstream loadData("blob3.txt");;
if (!loadData)
{...}
else
{
while(getline(loadData, t, '\n'))
{
++lineCount;
}
loadData.clear();
loadData.seekg(0, ios::beg);
while (j<(lineCount*4))
{
for(i=0; i<3;i++)
{
loadData >> g_vertices[m];
m++;
}
loadData >> g_colors[k];
g_colors[k + 1] = g_colors[k];
g_colors[k + 2] = g_colors[k];
k=k+3;
j=j+4;
}
}
loadData.close();
void InitApp()
{
...
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId); // Bind The Buffer
// Load The Data
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(g_vertices)+sizeof(g_colors), 0, GL_STATIC_DRAW_ARB);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(g_vertices), g_vertices); // copy vertices starting from 0 offest
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(g_vertices), sizeof(g_colors), g_colors); // copy colours after normals
...
}
Code:
//global
GLfloat *g_vertices=NULL;
GLfloat *g_colors=NULL;
bool Init()
{
...
ifstream loadData("blob3.txt");;
if (!loadData)
{...}
else
{
while(getline(loadData, t, '\n'))
{
++lineCount;
}
loadData.clear();
loadData.seekg(0, ios::beg);
const int arraySize = lineCount*3;
g_vertices = new GLfloat [arraySize];
g_colors = new GLfloat [arraySize];
while (j<(lineCount*4))
{
for(i=0; i<3;i++)
{
loadData >> g_vertices[m];
m++;
}
loadData >> g_colors[k];
g_colors[k + 1] = g_colors[k];
g_colors[k + 2] = g_colors[k];
k=k+3;
j=j+4;
}
}
loadData.close();
void InitApp()
{
...
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId); // Bind The Buffer
// Load The Data
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(g_vertices)+sizeof(g_colors), 0, GL_STATIC_DRAW_ARB);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(g_vertices), g_vertices); // copy vertices starting from 0 offest
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(g_vertices), sizeof(g_colors), g_colors); // copy colours after normals
...
}
-
December 17th, 2010, 05:54 PM
#2
Re: Text file to array
Maybe I am reading the code incorrectly, but it seems to me that
g_vertices should be dimensioned lineCount*4*3
Edit: sorry, I missed the "j = j + 4"
Last edited by Philip Nicoletti; December 17th, 2010 at 05:59 PM.
-
December 17th, 2010, 08:15 PM
#3
Re: Text file to array
Can you post a very small sample input file ? I am having
problems seeing why you are dimensioning the arrays
at lineCount*3
-
December 18th, 2010, 01:42 AM
#4
Re: Text file to array
make sure the array allocation size enough...
-
December 18th, 2010, 03:34 AM
#5
Re: Text file to array
Why don't you switch to std::vector which will handle all memory automatically?
If performance is an issue, you can pre-allocate the vector with a given capacity. But even then, if you by accident put more elements in it, it will grow automatically for you.
-
December 20th, 2010, 08:36 AM
#6
Re: Text file to array
Originally Posted by Philip Nicoletti
Can you post a very small sample input file ? I am having
problems seeing why you are dimensioning the arrays
at lineCount*3
Sorry about that, here a little sample:
blob.txt (x,y,z, amplitue(color))
1.0 1.0 1.0 0.0
1.0 2.0 1.0 0.2
1.0 3.0 1.0 0.0
1.0 4.0 1.0 0.0
1.0 5.0 1.0 0.0
1.0 6.0 1.0 0.5
-
December 20th, 2010, 08:37 AM
#7
Re: Text file to array
Originally Posted by Marc G
Why don't you switch to std::vector which will handle all memory automatically?
If performance is an issue, you can pre-allocate the vector with a given capacity. But even then, if you by accident put more elements in it, it will grow automatically for you.
I didnt switch to vector since i don't know how they will work with Opengl. If it doesn't work with normal array, i'll probably have to switch. How could i handle all memory at once from the text file?
Also, i think the problem is coming from sizeof(g_vertices) in the bindbuffer function
Last edited by m_power_hax; December 20th, 2010 at 08:46 AM.
-
December 20th, 2010, 08:58 AM
#8
Re: Text file to array
The problem is that sizeof(g_vertices) is only pointer size if g_vertices is a pointer.
When using pointers for arrays you need an additional variable for the size and cannot use sizeof.
Edit: Sorry, didn't read the last comment of m_power_hax.
-
December 20th, 2010, 09:33 AM
#9
Re: Text file to array
Originally Posted by itsmeandnobodyelse
The problem is that sizeof(g_vertices) is only pointer size if g_vertices is a pointer.
When using pointers for arrays you need an additional variable for the size and cannot use sizeof.
Edit: Sorry, didn't read the last comment of m_power_hax.
I tried sizeVertices*sizeof(GLfloat) and it worked (well it looks like it).
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeVertices*sizeof(GLfloat)*2, 0, GL_STATIC_DRAW_ARB);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeVertices*sizeof(GLfloat), g_vertices); // copy vertices starting from 0 offest
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeVertices*sizeof(GLfloat), sizeVertices*sizeof(GLfloat), g_colors); // copy colours after normals
Last edited by m_power_hax; December 20th, 2010 at 09:35 AM.
-
December 20th, 2010, 11:50 AM
#10
Re: Text file to array
Originally Posted by m_power_hax
I tried sizeVertices*sizeof(GLfloat) and it worked (well it looks like it).
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeVertices*sizeof(GLfloat)*2, 0, GL_STATIC_DRAW_ARB);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeVertices*sizeof(GLfloat), g_vertices); // copy vertices starting from 0 offest
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeVertices*sizeof(GLfloat), sizeVertices*sizeof(GLfloat), g_colors); // copy colours after normals
Yes, sizeVertices*sizeof(GLfloat) is exactly the size of the memory allocated with new statement.
didnt switch to vector since i don't know how they will work with Opengl.
std::vector has an internal array that could be passed to OpenGL:
Code:
// GLfloat *g_vertices=NULL;
std::vector<GLfloat> g_vertices;
...
// g_vertices = new GLfloat [arraySize];
g_vertices.resize(arraySize, GLfloat(0.0)); // resize with initialisation
...
// glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeVertices*sizeof(GLfloat), g_vertices);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, g_vertices.size()*sizeof(GLfloat), &g_vertices[0]);
...
-
December 20th, 2010, 11:53 AM
#11
Re: Text file to array
While the above is true, note that you need to special-case if g_vertices.empty(), since in that case accessing g_vertices[0] may throw an exception on some compilers under some configurations. Alternatively you could always resize the vector 1+ what you actually need so that it is never empty.
-
December 20th, 2010, 11:56 AM
#12
Re: Text file to array
Originally Posted by Lindley
While the above is true, note that you need to special-case if g_vertices.empty(), since in that case accessing g_vertices[0] may throw an exception on some compilers under some configurations. Alternatively you could always resize the vector 1+ what you actually need so that it is never empty.
Yes. Note, that for the pointer solution you would need a similar check on pointer != NULL.
-
December 20th, 2010, 12:04 PM
#13
Re: Text file to array
Originally Posted by itsmeandnobodyelse
Yes. Note, that for the pointer solution you would need a similar check on pointer != NULL.
Not necessarily; since the third parameter of glBufferSubDataARB() would be 0 in that case, thus ensuring nothing is done with the pointer anyway.
-
December 20th, 2010, 02:01 PM
#14
Re: Text file to array
Originally Posted by Lindley
Not necessarily; since the third parameter of glBufferSubDataARB() would be 0 in that case, thus ensuring nothing is done with the pointer anyway.
Oh, I already have seen crashes with NULL pointers and size==0 argument e. g. if there is code like
Code:
int f(int * buf, int siz)
{
int nItems = siz/sizeof(buf[0]);
....
}
which doesn't assume an empty buffer.
-
December 20th, 2010, 04:03 PM
#15
Re: Text file to array
In that particular case I would not expect a problem since the sizeof would be evaluated at compile time anyway.
I would argue any implementation of a function taking a pointer and a size which did *anything* with the pointer when a size of 0 is specified would be wrong. The issue in the case of a vector is not the function call, but getting the pointer in the first place.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|