CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2010
    Posts
    81

    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
                    ...
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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

  4. #4
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Text file to array

    make sure the array allocation size enough...

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Oct 2010
    Posts
    81

    Re: Text file to array

    Quote Originally Posted by Philip Nicoletti View Post
    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

  7. #7
    Join Date
    Oct 2010
    Posts
    81

    Re: Text file to array

    Quote Originally Posted by Marc G View Post
    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.

  8. #8
    Join Date
    Oct 2009
    Posts
    577

    Smile 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.

  9. #9
    Join Date
    Oct 2010
    Posts
    81

    Re: Text file to array

    Quote Originally Posted by itsmeandnobodyelse View Post
    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.

  10. #10
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Text file to array

    Quote Originally Posted by m_power_hax View Post
    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]);
    ...

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  12. #12
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Text file to array

    Quote Originally Posted by Lindley View Post
    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.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Text file to array

    Quote Originally Posted by itsmeandnobodyelse View Post
    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.

  14. #14
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Text file to array

    Quote Originally Posted by Lindley View Post
    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.

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured