PDA

View Full Version : Hollow texture



stenio
10-27-2011, 06:46 AM
Hello,

I'm going crazy trying to achive an hollow texture. I want to simulate a 3D sphere using a circular shaded image. The problem is that the QUAD corners are not transparent. To better illustrate the problem I've made a 8x8 green square with a 4x4 hole in the center. I've tried everything but the hole is always not transparent and its color is always the last used color, as you can see from the image attached. Here is the code:


/* ########
* ########
* ##OOOO##
* ##OOOO##
* ##OOOO##
* ##OOOO##
* ########
* ########
*/
byte[] textureBgra = new byte[] { /* All pixel are green */
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF
};


Texture creation:


uint textureId;
Gl.glGenTextures(1, out textureId);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureId);

Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, 8, 8, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, textureBgra);


Rendering loop:


Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

const float axisSize = 25.0f;
// draw a line along the z-axis
Gl.glColor4f(0.0f, 0.0f, 1.0f, 1f);
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(0.0f, 0.0f, -axisSize); Gl.glVertex3f(0.0f, 0.0f, axisSize);
Gl.glEnd();
// draw a line along the y-axis
Gl.glColor4f(0.0f, 1.0f, 0.0f, 1f);
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(0.0f, -axisSize, 0.0f);
Gl.glVertex3f(0.0f, axisSize, 0.0f);
Gl.glEnd();
// draw a line along the x-axis
Gl.glColor4f(1.0f, 0.0f, 0.0f, 1f);
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(-axisSize, 0.0f, 0.0f);
Gl.glVertex3f(axisSize, 0.0f, 0.0f);
Gl.glEnd();

Gl.glEnable(Gl.GL_BLEND);
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);

Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureId);
Gl.glBegin(Gl.GL_QUADS);
Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-8f, -8f, 1.0f);
Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(8f, -8f, 1.0f);
Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(8f, 8f, 1.0f);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-8f, 8f, 1.0f);
Gl.glEnd();
Gl.glDisable(Gl.GL_TEXTURE_2D);

Gl.glDisable(Gl.GL_BLEND);

Gl.glFlush();


Maybe the method I'm using is not suitable to achive the result that I want?

Thanks for your help,
Stenio

stenio
10-28-2011, 07:39 AM
I've attached the source png file and the result. The red lines should intersect the circles.

mbentrup
10-28-2011, 08:44 AM
Hello,

I'm going crazy trying to achive an hollow texture. I want to simulate a 3D sphere using a circular shaded image. The problem is that the QUAD corners are not transparent. To better illustrate the problem I've made a 8x8 green square with a 4x4 hole in the center. I've tried everything but the hole is always not transparent and its color is always the last used color, as you can see from the image attached. Here is the code:


/* ########
* ########
* ##OOOO##
* ##OOOO##
* ##OOOO##
* ##OOOO##
* ########
* ########
*/
byte[] textureBgra = new byte[] { /* All pixel are green */
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF
};


Texture creation:


uint textureId;
Gl.glGenTextures(1, out textureId);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureId);

Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, 8, 8, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, textureBgra);


Rendering loop:


Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

const float axisSize = 25.0f;
// draw a line along the z-axis
Gl.glColor4f(0.0f, 0.0f, 1.0f, 1f);
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(0.0f, 0.0f, -axisSize); Gl.glVertex3f(0.0f, 0.0f, axisSize);
Gl.glEnd();
// draw a line along the y-axis
Gl.glColor4f(0.0f, 1.0f, 0.0f, 1f);
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(0.0f, -axisSize, 0.0f);
Gl.glVertex3f(0.0f, axisSize, 0.0f);
Gl.glEnd();
// draw a line along the x-axis
Gl.glColor4f(1.0f, 0.0f, 0.0f, 1f);
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex3f(-axisSize, 0.0f, 0.0f);
Gl.glVertex3f(axisSize, 0.0f, 0.0f);
Gl.glEnd();

Gl.glEnable(Gl.GL_BLEND);
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);

Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureId);
Gl.glBegin(Gl.GL_QUADS);
Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-8f, -8f, 1.0f);
Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(8f, -8f, 1.0f);
Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(8f, 8f, 1.0f);
Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-8f, 8f, 1.0f);
Gl.glEnd();
Gl.glDisable(Gl.GL_TEXTURE_2D);

Gl.glDisable(Gl.GL_BLEND);

Gl.glFlush();


Maybe the method I'm using is not suitable to achive the result that I want?

Thanks for your help,
Stenio

Your problem is the texture env: GL_DECAL alpha-blends the texture over the "previous stage", which is just the glColor for the first texture stage, so the hole is filled with the current glColor before it uses the blendFunc to draw to the composite image framebuffer.

You should use GL_REPLACE to pass the unmodified texture RGBA values to the BlendFunc.

stenio
10-28-2011, 09:04 AM
You should use GL_REPLACE to pass the unmodified texture RGBA values to the BlendFunc.


Thank you, thank you, thank you! Now it works!

Best regards,
Stenio