1

I implemented an 3D Texture based Volume Rendering using OpenTK. The code is based on this project. But the result showed no 3D effects, just flat 2D image.

The pipeline is simple enough: (1) Load 3D texture; (2) draw a series of quads(rectangles) and specify the corresponding texture coordinates.

The Vertices of each quads were generated using

private void generateVertices(int n)
    {
        vertices = new float[n * 3 * 4];
        int cur;
        for (int i = 0; i < n; i++)
        {
            cur = 3 * 4 * i;

            vertices[cur] = -.5f;
            vertices[cur + 1] = -.5f;
            vertices[cur + 2] = -0.5f + i / n;

            vertices[cur+3] = -.5f;
            vertices[cur + 4] = .5f;
            vertices[cur + 5] = -0.5f +  i / n;

            vertices[cur+6] = .5f;
            vertices[cur + 7] = .5f;
            vertices[cur + 8] = -0.5f +  i / n;

            vertices[cur+9] = .5f;
            vertices[cur + 10] = -.5f;
            vertices[cur + 11] = -0.5f + i / n;
        }
    }

To draw 256 quads, just call generateVertices(256) and opengl routine

GLDrawElements(GL_QUADS,...)

The TexCoordinates were calculated using vertices position in vertex shader like this:

texCoord = aPosition+vec3(0.5f,0.5f,0.5f);

Any ideas are welcome.

1
  • I voted to close the question because it lacks a minimal, complete, and verifiable example, and because it's not at all clear (to me) what OP's problem here is. How should anyone know what they should expect to see by looking at a function that generates some vertices? Also, the codeproject article is relying on the obsolete OpenGL pipeline. Commented Mar 5, 2019 at 7:37

1 Answer 1

2

Integer i at range [0,n-1], i/n will be 0 forever. So you indeed specify n overlaped quads.

The solution is simple, just explicitly cast i to float (float)i/n.

Sign up to request clarification or add additional context in comments.

4 Comments

If n == 0, then it never enters the loop. If n > 0, then it does enter the loop, and i increases up to n (n-1 is the last time the body gets exec'ed). From where do you assert that "i/n will be 0 forever"? This makes no sense.
Got it! I get an 3D volume rendered on the screen by making this changes: in the for-loop: { vertices[cur + 2] = -0.5f +1.0f* i / n; vertices[cur + 8] = -0.5f + 1.0fi / n; vertices[cur + 11] = -0.5f +1.0f i / n; }
@code_dredd integer 1 / integer 2 produce another integer 0. vertices[cur + 2] = -0.5f + i / n equals -0.5f forever. There are n rectangle with the same location added to VBO=> no 3d effets.
@ShannonChow I think adding that bit of info (i.e. the int-based truncation) to the answer and telling OP to cast i and/or n to a float, rather than "just multiply by 1.0f" (which does the same, but less explicitly), would improve the answer. I'm not sure OP would necessarily understand why that was a problem in the first place just from your post...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.