0

I have a torus rendered by OpenGL and can map a texture; there is no problem as long as the texture is opaque. But it doesn't work when I make the color selectively transparent in fragment shader. Or rather: it works but only in some areas depending on the order of triangles in vertex buffer; see the difference along the outer equator. black and white torus with the issue The torus should be evenly covered by spots.

The source image will be png, however for now I work with bmp as it is easier to load (the texture loading function is part of a tutorial). The image has white background and spots of different colors on top of it; it is not transparent. The desired result is nearly transparent torus with spots. Both spots in the front and the back side must be visible. The rendering will be done offline, so I don't require speed; I just need to generate image of torus from an image of its surface.

So far my code looks like this (it is a merge of two examples): https://gist.github.com/juriad/ba66f7184d12c5a29e84

Fragment shader is:

#version 330 core

// Interpolated values from the vertex shaders
in vec2 UV;

// Ouput data
out vec4 color;

// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;

void main(){

    // Output color = color of the texture at the specified UV
    color.rgb = texture2D( myTextureSampler, UV ).rgb;
    if (color.r == 1 && color.g == 1 && color.b == 1) {
        color.a = 0.2;
    } else {
        color.a = 1;
    }
}

I know that the issue is related to order. What I could do (but don't know what will work):

  1. Add transparency to the input image (and find a code which loads such image).
  2. Do something in vertex shader (see Fully transparent OpenGL model).
  3. Sorting would solve my problem, but if I get it correctly, I have to implement it myself. I would have to find a center of each triangle (easy), project it with my matrix and compare z values.
  4. Change somehow blending and depth handling:

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glDepthFunc(GL_LEQUAL);
    glDepthRange(0.0f, 1.0f);
    

I need an advice, how to continue.


Update, this nearly fixes the issue:

glDisable(GL_DEPTH_TEST);
//glDepthMask(GL_TRUE);
glDepthFunc(GL_NEVER);
//glDepthRange(0.0f, 1.0f);

I wanted to write that it doesn't distinguish sports in front and the back, but then I realized they are nearly white and blending with white doesn't make difference.

The new image of torus with colorized texture is: colorful

The remaining problems are:

  1. red spots are blue - it is related to the function loading BMP (doesn't matter)
  2. as in the input images all the spots are of the same size, the bigger spots should be on the top and therefore saturated and not blended with white body of the torus. It seems that the order is opposite than it should be. If you compare it to the previous image, there the big spots by appearance were drawn correctly and the small ones were hidden (the back side of the torus).

How to fix the latter one?

Community
  • 1
  • 1
juriad
  • 44
  • 1
  • 6
  • Have you tried to disable depth test? – Adrian Maire Oct 17 '14 at 10:26
  • 1
    Since you are playing with alpha, Order Independent Transparency may help you. – Raki Oct 17 '14 at 11:30
  • Yes, it would help me, but doing a quick search I think that I would have to implement it from scratch as it is not part of OpenGL. Am I correct? Are there any libraries which are easy to use and easy to merge into my existing code? In the end, manual sorting might be the best idea as I don't require real time processing. This is a one-time project, I do a different kind of things, but now I need to visualize the results. – juriad Oct 17 '14 at 12:10
  • Yes you are. I didn't find any lib, but [link](http://blog.icare3d.org/2010/06/fast-and-accurate-single-pass-buffer.html) may help you, if u wish to build from scratch. – Raki Oct 17 '14 at 12:49
  • I saw that link; it would certainly use more than 50 lines of code. Thanks anyway. – juriad Oct 17 '14 at 13:45

1 Answers1

0

First problem was solved by disabling depth-test (see update in the question).

Second problem was solved by manual sorting of array of all triangles. It works well even in real-time for 20000 triangles, which is more than sufficient for my purpose

The resulting source code: https://gist.github.com/juriad/80b522c856dbd00d529c

It is based on and uses includes from OpenGL tutorial: http://www.opengl-tutorial.org/download/.

juriad
  • 44
  • 1
  • 6