<p>It looks like the parameters to your camera class are not correct</p>
<pre><code> const int img_ratio = 2;
 ...
 Camera&lt;float&gt; cam1(20.0f, img_ratio, Point3&lt;float&gt;(0.0f, 0.0f, 0.0f), Point3&lt;float&gt;(0.0f, 0.0f, -1.0f), Vec3&lt;float&gt;(0.0f, 1.0f, 0.0f));
</code></pre>
<p>In your code you set img_ratio to 2, I would expect it should be something like img_width/img_height (or the reverse img_height/img_width). img_ratio should also be also be a floating point to match the  type the camera class is being instantiated with.</p>

CC BY-SA 4.0
2021-02-11T15:46:52.697Z
Thank you so much! Someone already answered on the main stackoverflow site but that was basically the error.
Tippitytopkek
CC BY-SA 4.0
2021-02-11T10:00:04.210Z
2021-02-11T10:00:04.210Z
PaulHK
<p>I followed along with <em>Ray Tracing in One Weekend</em> and managed to get the final output but I wanted to understand more about creating a camera and &quot;painting&quot; the screen since he didn't go over it much.</p>
<p>When I tried using a different way of creating a camera by spheres actually get elongated, making them look like more like ellipses. I've tried modifying the x and y assignments in <code>screenCords</code> but I've only managed to make more errors (such as objects wrapping around to the opposite side)</p>
<p>Camera.h:</p>
<pre><code>#pragma once

#include &quot;../Matrix.h&quot;
#include &quot;../Defs.h&quot;
#include &quot;Defs.h&quot;

template&lt;typename O&gt;
using Point3 = Vec3&lt;O&gt;;

template&lt;typename O&gt;
using Color = Vec3&lt;O&gt;;

template &lt;typename O&gt;
class Camera{
  O Height;
  O Width;
  Vec3&lt;O&gt; Forward, Right, Up;
  Point3&lt;O&gt; Origin;

public:
  Camera(O fov, O aspect_ratio, Point3&lt;O&gt; origin, Point3&lt;O&gt; target, Vec3&lt;O&gt; upguide) {
    Height = atan(degrees_to_radians(fov));
    Width = Height * aspect_ratio;
    
    Origin = origin;

    Forward = target - origin;
    Forward.normalize();
    Right = Forward.cross(upguide);
    Right.normalize();
    Up = Right.cross(Forward);

    }

    Ray&lt;O&gt; get_raydir(O right, O up){
      Vec3&lt;O&gt; result(Forward + right * Width * Right + up * Height * Up); result.normalize();

      return Ray&lt;O&gt;(Origin, result);
    }

    void screenCords(O &amp;x, O &amp;y, O width, O height){
      x = ((2.0f * x) / width) -1.0f;
      y = ((2.0f * y) / height); 
    }
};
</code></pre>
<p>Main.cpp</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &quot;../Matrix.h&quot;
#include &quot;Camera.h&quot;
#include &lt;vector&gt;
#include &quot;Image.h&quot;
#include &quot;Shapes.h&quot;
#include &quot;Tracer.h&quot;
#include &quot;../Defs.h&quot;

template&lt;typename O&gt;
using Point3 = Vec3&lt;O&gt;;

template&lt;typename O&gt;
using Color = Vec3&lt;O&gt;;

int main(){
  const int img_ratio = 2;
  const int img_width = 640;
  const int img_height = 480;
  const int depth = 50; float t_Max = infinity; float t_Min = 0.001;

  float inv_width = 1 / float(img_width);
  float inv_height = 1 / float(img_height);

  std::vector&lt;Sphere&lt;float&gt;&gt; shapes;

  Camera&lt;float&gt; cam1(20.0f, img_ratio, Point3&lt;float&gt;(0.0f, 0.0f, 0.0f), Point3&lt;float&gt;(0.0f, 0.0f, -1.0f), Vec3&lt;float&gt;(0.0f, 1.0f, 0.0f));

  Sphere&lt;float&gt; cir1(0.2f, Point3&lt;float&gt;(0.2f, 0.0f, -1.0f));
  Sphere&lt;float&gt; cir2(7.0f, Point3&lt;float&gt;(0.0f, -7.0f, -1.0f));
  Sphere&lt;float&gt; cir3(0.5f, Point3&lt;float&gt;(1.0f, 0.0f, -1.0f));
  shapes.push_back(cir1);
  //shapes.push_back(cir2);
  //shapes.push_back(cir3);

  Tracer&lt;float&gt; tracer(shapes);

  std::cout &lt;&lt; &quot;P3\n&quot; &lt;&lt; img_width &lt;&lt; ' ' &lt;&lt; img_height &lt;&lt; &quot;\n255&quot; &lt;&lt; std::endl;

  Ray&lt;float&gt; ray(Point3&lt;float&gt;(0.0f), Vec3&lt;float&gt;(0.0f));

  for (int j = 0; j &lt; img_height; j++)
  {
    std::cerr &lt;&lt; &quot;\rScanlines remaining: &quot; &lt;&lt; j &lt;&lt; ' ' &lt;&lt; std::flush;
    for (int i = 0; i &lt; img_width; i++){

        float x = i;
        float y = j;

        cam1.screenCords(x, y, img_width, img_height);

        ray = cam1.get_raydir(x, y);
        //ray = Ray&lt;float&gt;(Vec3&lt;float&gt;(x1, y1, 1), Point3&lt;float&gt;(0.0f, 0.0f, 0.0f));
        tracer.iterator(ray, depth, t_Max, t_Min);
    }
  }
  std::cerr &lt;&lt; &quot;\n done &quot; &lt;&lt; std::endl;
}
</code></pre>
<p>I suspect the error is in one of these files since the spheres are actually being drawn with the colors based on normals (with the top and bottom normal colors unsurprisingly being bugged)</p>
<p>Here are a few examples of the output:</p>
<p><a href="https://i.sstatic.net/OFf42.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OFf42.png" alt="With one sphere centered at the origin" /></a></p>
<p><a href="https://i.sstatic.net/Bj1GH.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Bj1GH.png" alt="More spheres" /></a></p>

<raytracing><c++>
(Ray tracing) Trouble converting to screen coordinates, objects being stretched
Tippitytopkek
# (Ray tracing) Trouble converting to screen coordinates, objects being stretched

(Asked by: username_0 on 2021-02-07)

I followed along with *Ray Tracing in One Weekend* and managed to get the final output but I wanted to understand more about creating a camera and "painting" the screen since he didn't go over it much.

When I tried using a different way of creating a camera by spheres actually get elongated, making them look like more like ellipses. I've tried modifying the x and y assignments in `screenCords` but I've only managed to make more errors (such as objects wrapping around to the opposite side)

Camera.h:

```
#pragma once

#include "../Matrix.h"
#include "../Defs.h"
#include "Defs.h"

template<typename O>
using Point3 = Vec3<O>;

template<typename O>
using Color = Vec3<O>;

template <typename O>
class Camera{
  O Height;
  O Width;
  Vec3<O> Forward, Right, Up;
  Point3<O> Origin;

public:
  Camera(O fov, O aspect_ratio, Point3<O> origin, Point3<O> target, Vec3<O> upguide) {
    Height = atan(degrees_to_radians(fov));
    Width = Height * aspect_ratio;
    
    Origin = origin;

    Forward = target - origin;
    Forward.normalize();
    Right = Forward.cross(upguide);
    Right.normalize();
    Up = Right.cross(Forward);

    }

    Ray<O> get_raydir(O right, O up){
      Vec3<O> result(Forward + right * Width * Right + up * Height * Up); result.normalize();

      return Ray<O>(Origin, result);
    }

    void screenCords(O &x, O &y, O width, O height){
      x = ((2.0f * x) / width) -1.0f;
      y = ((2.0f * y) / height); 
    }
};

```

Main.cpp

```
#include <iostream>
#include <cmath>
#include "../Matrix.h"
#include "Camera.h"
#include <vector>
#include "Image.h"
#include "Shapes.h"
#include "Tracer.h"
#include "../Defs.h"

template<typename O>
using Point3 = Vec3<O>;

template<typename O>
using Color = Vec3<O>;

int main(){
  const int img_ratio = 2;
  const int img_width = 640;
  const int img_height = 480;
  const int depth = 50; float t_Max = infinity; float t_Min = 0.001;

  float inv_width = 1 / float(img_width);
  float inv_height = 1 / float(img_height);

  std::vector<Sphere<float>> shapes;

  Camera<float> cam1(20.0f, img_ratio, Point3<float>(0.0f, 0.0f, 0.0f), Point3<float>(0.0f, 0.0f, -1.0f), Vec3<float>(0.0f, 1.0f, 0.0f));

  Sphere<float> cir1(0.2f, Point3<float>(0.2f, 0.0f, -1.0f));
  Sphere<float> cir2(7.0f, Point3<float>(0.0f, -7.0f, -1.0f));
  Sphere<float> cir3(0.5f, Point3<float>(1.0f, 0.0f, -1.0f));
  shapes.push_back(cir1);
  //shapes.push_back(cir2);
  //shapes.push_back(cir3);

  Tracer<float> tracer(shapes);

  std::cout << "P3\n" << img_width << ' ' << img_height << "\n255" << std::endl;

  Ray<float> ray(Point3<float>(0.0f), Vec3<float>(0.0f));

  for (int j = 0; j < img_height; j++)
  {
    std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
    for (int i = 0; i < img_width; i++){

        float x = i;
        float y = j;

        cam1.screenCords(x, y, img_width, img_height);

        ray = cam1.get_raydir(x, y);
        //ray = Ray<float>(Vec3<float>(x1, y1, 1), Point3<float>(0.0f, 0.0f, 0.0f));
        tracer.iterator(ray, depth, t_Max, t_Min);
    }
  }
  std::cerr << "\n done " << std::endl;
}

```

I suspect the error is in one of these files since the spheres are actually being drawn with the colors based on normals (with the top and bottom normal colors unsurprisingly being bugged)

Here are a few examples of the output:

[![With one sphere centered at the origin](https://i.sstatic.net/OFf42.png)](https://i.sstatic.net/OFf42.png)

[![More spheres](https://i.sstatic.net/Bj1GH.png)](https://i.sstatic.net/Bj1GH.png)

**Tags:** raytracing, c++


---

## 1 answers

### Answer by username_1 on 2021-02-11. Score: 1 | Accepted answer

It looks like the parameters to your camera class are not correct

```
 const int img_ratio = 2;
 ...
 Camera<float> cam1(20.0f, img_ratio, Point3<float>(0.0f, 0.0f, 0.0f), Point3<float>(0.0f, 0.0f, -1.0f), Vec3<float>(0.0f, 1.0f, 0.0f));

```

In your code you set img\_ratio to 2, I would expect it should be something like img\_width/img\_height (or the reverse img\_height/img\_width). img\_ratio should also be also be a floating point to match the type the camera class is being instantiated with.

> **Comments:**
> **username_0**: Thank you so much! Someone already answered on the main stackoverflow site but that was basically the error.  

---

