Odd results using glTexImage2D

I tried to figure out how it works glTexImage2Dand see some odd results from pretty clear code. My code simply draws a rough circle into an unsigned array of length 256 * 256 and then sends this data to become a texture. However, the texture is displayed as variations of red and orange, regardless of which combinations I choose inside the image creation cycle:

unsigned* data = new unsigned[256*256];
for (int y = 0; y < 256; ++y)
    for (int x = 0; x < 256; ++x)
        if ((x - 100)*(x - 100) + (y - 156)*(y - 156) < 75*75)
            data[256*y + x] = ((156 << 24) | (256 << 16) | (156 << 8) | (200 << 0));
        else
            data[256*y + x] = 0;  // I'd expect this to be transparent and the above to be slightly transparent and green, but it red somehow.

glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)data);

OpenGL Options:

    glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE);
//glEnable(GL_BLEND);
//glDisable(GL_CULL_FACE);

glGenTextures(1, &leaf[0]);
    createLeaf(leaf[0]);  // createLeaf(GLuint& texid) is posted entirely above

The rest of the code does nothing but display the texture on one quadrant in the window. (x64 win7)

Edit: I decided to try Rickard for sure, and I still get a purple circle.

+5
source share
2 answers
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)data);

. (GL_RGBA8, GL_RGBA). ; . (GL_RGBA8) (GL_RGBA). .

. OpenGL, . ; . , data, data. , OpenGL?

. :

 ((156 << 24) | (256 << 16) | (156 << 8) | (200 << 0))

-, 256 . 256 - 0x100, , .

, :

 0x9D009CC8

RGBA , - 0x9D, - 0x00, - 0x9C, - 0xC8.

, , , , 4 , :

 0xC89C009D

OpenGL, , ( ), - . , OpenGL , 0xC8, . .

OpenGL, : 8- 32- . :

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, (GLvoid*)data);

GL_UNSIGNED_INT_8_8_8_8 , OpenGL 32- ( ). 8 32- , - , - , - .

, , :

GLuint* data = new GLuint[256*256]; //Use OpenGL types
for (int y = 0; y < 256; ++y)
    for (int x = 0; x < 256; ++x)
        if ((x - 100)*(x - 100) + (y - 156)*(y - 156) < 75*75)
            data[256*y + x] = ((0x9C << 24) | (0xFF << 16) | (0x9C << 8) | (0xC8 << 0));
        else
            data[256*y + x] = 0;  // I'd expect this to be transparent and the above to be slightly transparent and green, but it red somehow.

glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);  //Always set the base and max mipmap levels of a texture.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, (GLvoid*)data);

// , , , - .

; , . , , , . .

+13

, , unsigned int 4 .

unsigned char* data = new unsigned char[256*256*4];
for (int y = 0; y < 256; ++y)
    for (int x = 0; x < 256; ++x)
        if ((x - 100)*(x - 100) + (y - 156)*(y - 156) < 75*75){
            data[(256*y + x)*4+0] = 156;
            data[(256*y + x)*4+1] = 256;
            data[(256*y + x)*4+2] = 156;
            data[(256*y + x)*4+3] = 200;
        }else{
            data[(256*y + x)*4+0] = 0;
            data[(256*y + x)*4+1] = 0;
            data[(256*y + x)*4+2] = 0;
            data[(256*y + x)*4+3] = 0;
        }

glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)data);

, , -. , GL_RGBA8 GL_RGBA. . glBindTexture GLuint refrense (& texid), texid GLuint (GLuint * texid;), , . (Edit: Just , glGenTexture, glBindTexture)

+2

All Articles