Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGl glTexImage2D data

I am trying to read floatingpoint numbers from a CSV file, that contains a precomputed texture, store it in a 1 dimensional array, and then put that read data into a 2 dimensional texture.
I need to make sure the following code does that, because i have problems accessing the data and I cannot figure out where the error is:

// Allocate memory  
float * image = new float [width * height * 3 ];  
for( int i = 0; i < height; i++)  
    {  
        for( int j = 0; j < width-1; j++)  
            {  
                fscanf( fDataFile, "%f,", &fData );  
                image[ 4 * i * j + 0 ] = fData;  
                image[ 4 * i * j + 1 ] = fData;  
                image[ 4 * i * j + 2 ] = fData;  
            }  
        fscanf( fDataFile, "%f", &fData );  
        image[ 4 * i * width-1 + 0 ] = fData;  
        image[ 4 * i * width-1 + 1 ] = fData;  
        image[ 4 * i * width-1 + 2 ] = fData;  
     }   

There shouldn't be a problem here, but what troubles me is the following:

// create the texture  
glGenTextures(1, &texHandle);  
glBindTexture(GL_TEXTURE_2D, texHandle);   
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, &image[0]); 

Is it okay to just give glTexImage2D the pointer to my onedimensional array?
the size of the array is width*height*3 and the texture's format should be width*height with 3 channels... so the size should be okay i guess?! Still my program won't work as expected and this is one potential source for an error.

like image 274
molostil Avatar asked Mar 05 '13 23:03

molostil


1 Answers

I solved my messed up texture reading. I don't know what got into me but the initilization of my array was pure nonesense. Here is the corrected code, I found out when trying to write a test texture:

// Allocate memory  
float * image = new float [width * height * 3 ];  
for( int i = 0; i < height; i++)  
{  
    for( int j = 0; j < width-1; j++)  
    {  
        fscanf( fDataFile, "%f,", &fData );  
        image[ 3 * (i * width + j) + 0 ] = fData;  
        image[ 3 * (i * width + j) + 1 ] = fData;  
        image[ 3 * (i * width + j) + 2 ] = fData;  
        //image[ 4 * i * j + 2 ] = 1.0f;  
    }  
    fscanf( fDataFile, "%f", &fData );  
    image[ 3 * (i * width + width-1) + 0 ] = fData;  
    image[ 3 * (i * width + width-1) + 1 ] = fData;  
    image[ 3 * (i * width + width-1) + 2 ] = fData;  
    //image[ 4 * i * width-1 + 2 ] = 1;  
}  

Furthermore it would work now independent of the internal format. GL_RGB, GL_RGBA, GL_RGB32F and GL_RGBA32F all work fine without changing the way I read my texture.

like image 159
molostil Avatar answered Oct 23 '22 15:10

molostil