Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:49:01+00:00 2026-06-07T18:49:01+00:00

I am trying to draw the Mandelbox using my own ray marching on the

  • 0

enter image description here

I am trying to draw the Mandelbox using my own ray marching on the CPU.

I have a width*height bitmap to render to.

For each pixel, I want to march towards the cube:

static float eye = 0.0f; eye = glm::clamp(eye+0.005f,0.0f,1.0f); // animate
const glm::mat4 projection = glm::perspective(35.0f, (float)width/height, 0.1f, 10.0f),
        modelview = glm::lookAt(glm::vec3(cos(eye),sin(eye),-1),glm::vec3(0,0,0),glm::vec3(0,0,1));     
const float epsilon = sqrt(1.0f/std::max(width,height))/2.0f;
for(int y=0; y<height; y++) {
        for(int x=0; x<width; x++) {
                glm::vec3 p = glm::unProject(glm::vec3(x,y,0),modelview,projection,glm::vec4(0,0,width,height)),
                        dir = glm::unProject(glm::vec3(x,y,1),modelview,projection,glm::vec4(0,0,width,height))-p,
                        P0 = p;
                //std::cout << x << "," << y << " " << p.x << "," << p.y << "," << p.z << " " << dir.x << "," << dir.y << "," << dir.z << std::endl;
                float D = 0;
                for(int i=0; i<MAX_ITER; i++) {
                        const float d = DE(p);
                        D += d;
                        if(d<epsilon) {
                                depth_bmp[y*width+x] = 255.0f/i;
                                break;
                        }
                        p = dir*D + P0;
                }
        }
}

My distance estimator function is a very literal translation and looks like this:

float DE(glm::vec3 p) {
    const float Scale = -1.77f, fixedRadius2 = 1.0f, minRadius2 = (0.5f*0.5f);
    const glm::vec3 p0 = p;
    float dr = 1.0f;
    for(int n = 0; n < 13; n++) {
        // Reflect
        p = (glm::clamp(p,-1.0f,1.0f) * 2.0f) - p;
        // Sphere Inversion
        const float r2 = glm::dot(p,p);
        if(r2<minRadius2) {
            const float t = (fixedRadius2/minRadius2);
            p *= t;
            dr *= t;
        } else if(r2<fixedRadius2) {
            const float t = (fixedRadius2/r2);
            p *= t;
            dr *= t;
        }
        // Scale & Translate
                p = p * Scale + p0;
                dr = dr * abs(Scale) + 1.0f;
    }
    return glm::length(p)/abs(dr);
}

And the output looks completely unbox-like:

How do I set the eye transform up so I see the cube properly?

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:49:03+00:00Added an answer on June 7, 2026 at 6:49 pm

    The issue is that the length of the ray must be normalised:

    glm::vec3 p = glm::unProject(glm::vec3(x,y,0),modelview,projection,glm::vec4(0,0,width,height)),
        dir = glm::unProject(glm::vec3(x,y,1),modelview,projection,glm::vec4(0,0,width,height))-p;
    const float len = glm::length(dir);
    dir = glm::normalise(dir);
    float D = 0;
    for(int i=0; i<MAX_ITER; i++) {
        const float d = DE(p + dir*D);
        D += d;
        if(D > len) break;
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to draw a Google Line Chart, where each line may have
I'm trying to draw a line using ctx.lineTo in loop. I have small function
I am trying to draw a series of rectangles using OpenGL but some of
I'm trying to draw over the whole screen by using the desktop canvas and
I am trying to draw a textured cube using just 8 vertices and one
I am trying to draw a circle using Windows Form at runtime inside a
I'm trying to draw Arabic text onto a Bitmap for display: Bitmap img =
I am trying to draw an animation. To do so I have extended View
I'm trying to draw shapes that will move around the screen using the LunarLander
I am trying to draw a line using the Graphics 2D but then the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.