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

  • Home
  • SEARCH
  • 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 8911275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:54:06+00:00 2026-06-15T03:54:06+00:00

I am implementing a basic ray tracer so I am reading up on theory

  • 0

I am implementing a basic ray tracer so I am reading up on theory and other implementations. Here is the code that I am currently referring to

    template<typename T>
void render(const std::vector<Sphere<T> *> &spheres)
{
    int width = 800, height = 800;//define width and height of the screen
    Vector3d<T> *image = new Vector3d<T>[width * height], *pixel = image;
    T invWidth = 1 / T(width), invHeight = 1 / T(height);
    T fov = 90, aspectratio = width / T(height);//defining field of view angle and aspect ratio
    T fovDist = tan(M_PI * 0.5 * fov / T(180));//Calculates half screen width / perpendicular distance to the camer
a position
    // For each ray crossing a pixel on the view screen.
    for (int y = 0; y < height; ++y) //For each scan line
    {
        for (int x = 0; x < width; ++x, ++pixel) //for each pixel on a scan line
        {
            //Calculate the x position using centre of the pixel. 
           /*By simple trig (width/2)/perpendicular distance to camera = tan (fov/2)
             =>width = distance * tan (fov/2)

           */
            T xx = (2 * ((x + 0.5) * invWidth) - 1) * fovDist * aspectratio;
            T yy = (1 - 2 * ((y + 0.5) * invHeight)) * fovDist;//Calculate the y position
            Vector3d<T> raydir(xx, yy, -1);
            raydir.normalize();
            *pixel = trace(Vector3d<T>(0), raydir, spheres, 0);
        }
    }

I am putting in comments of what I understand but I am stuck on the calculation of xx and yy.

I understand that by simple trigonometry width = 2 * (perpendicular distance from camera to the view plane) * tan (fov / 2). But I am not able to figure out the expression for T xx and T yy.

Please can someone help clarify.

Regards,
Moira

  • 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-15T03:54:08+00:00Added an answer on June 15, 2026 at 3:54 am

    Ok, so if I understand this, you’ve got your frame parallel to the XY plane, and sitting at Z=-1, with your camera/eye at the origin. Now you’re trying to find the X and Y location of the center of each pixel. Is that right?

    So x + 0.5 offsets you to the center of the current pixel, that’s good. Dividing by the total number of pixels across gives you the percentage of the way across the frame: good. Multiply by two, then subtract one and you’re in the range -1 to 1, which corresponds to 100% of the way from center to the left, and 100% of the way from the center to the right, respectively (or vice-versa). Now multiply by half the width (fovDist), so it puts you somewhere between one-half width to the left, and one-half width to the right. So that’s all good. Lastly, you multiply by the aspect ratio because fovDist is really half the height, not necc. half the width. So if you have an aspect ratio of 2:1, it should be wider than it is tall, so you want to spread your pixels out horizontally, so you’re all good there, too.

    You’ve got yy set up the same way, except inverted because scanlines usually go the other way. So that looks fine, as well.

    So does it work and you’re just trying to understand it? Or is it not working correctly? If it’s not working correctly, what kind of behavior are you seeing instead?

    If it’s not working correctly, the only thing that jumps out at me is all of the different numeric types. For instance your invWidth and invHeight variables both divide integer 1 by something. I can’t remember how C++ handles this, but it’s possible that you’re getting truncated to 0 here by integer division. Similarly you’ve got a lot of integers in your expressions for xx and yy. Not sure if the compiler will work it out alright, but if it’s not working correctly, I’d start there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am currently implementing a simple ray tracer in c++. I have a class
I am currently implementing a basic raytracer in c++. Works pretty well so far,
I am implementing a basic board game in Java and that I am having
I'm trying to grasp higher-order-polymophism in scala by implementing a very basic interface that
I am currently implementing cache. I have completed basic implementation, like below. What I
I'm making a basic platformer engine in AS3, and I'm currently implementing gravity. The
I'm working on an assembler for a very basic ISA. Currently I'm implementing parser
I'm looking at a tutorial for asp.net mvc here on the asp site: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application
I am currently implementing a custom c# cache provider that I can plug in
I was implementing some basic complex number arithmetic in Clojure, and noticed that it

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.