6

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
molostil
  • 119
  • 1
  • 1
  • 5
  • 1
    In fact, specifying the texture as a one-dimensinal array as you do is one of the most common ways of passing a texture into OpenGL. You're also correct in specifying the width and height combined with the pixel format (your statement of "3 channels"). All of that looks correct with what you provide here. Is it the case that your texture doesn't show up on the geometry? Have you specified a minification filter using `glTexParamter`? If not, try calling that with `GL_NEAREST` to see if your texture shows up. You can refine from there. – radical7 Mar 06 '13 at 05:36
  • Hey! Thanks for the quick and encouraging answer! Ineed i found one mistake, when reading the file. before i had an alpha channel as well so i wrote `image[ 4 * i * width-1 + 1 ] = fData;` instead of `image[ 3 * i * width-1 + 1 ] = fData;` and i guess the error is at another line of code. but i feel more confident to find it now! thank you! – molostil Mar 06 '13 at 12:41

2 Answers2

5

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
molostil
  • 119
  • 1
  • 1
  • 5
2
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, &image[0]);

You should be using a floating-point internal format. For example, GL_RGB32F. That should be the third parameter.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    That really depends; OpenGL will convert pixel types, particularly in this generic _RGB_ case. As the OP doesn't specify the version he's working with, `GL_RGB32F` may not be available. Likewise, depending on the version, while unlikely, it could non-power-of-two textures. However, the most likely issue is the choice of minificatiion filter, which the OP doesn't specify. – radical7 Mar 06 '13 at 05:33
  • first of all thank you for your helpful responses.the thing is – molostil Mar 06 '13 at 12:16
  • thank you two for your great input. it's always very very helpfull, even if using magnification filter and minifictaion filter didn't do the trick right now, i would have had problems with it later on i guess. ^^ `GL_RGB32F` didn't help either but at least i am more confident that my texture input is quite possibly flawless now and the error is somewhere else. thanks, i will respond if i found it. – molostil Mar 06 '13 at 12:47