SFML Sphere projection being stretched, don't know why

Viewed 25

I tried doing a little personal project of projecting a sphere onto an image using SFML from scratch using the basic knowledge I know. I coded it up for the most part, but for some reason the sphere bulges out towards the left and right sides.Screenshot

After some testing, I probably am just doing something wrong but I'm not sure what. I'd THOUGHT it would be fine if the screen size was the same width and height, but I was wrong, I guess :/

main.cpp

#include <stdio.h>
#include <math.h>
#include "SFML/Graphics.hpp"

using namespace sf;



#define s_l (6 * MUL)
#define s_h (6 * MUL)

// 3d Point in space
struct S_Point {
    double x, y, z;
    /// Vertex_Point(double x, double y) : x{}, y{} {};
};

// Distance from a point to another point
double p_dist(S_Point p, S_Point s) {
    return sqrt(pow(s.x - p.x, 2) + pow(s.y - p.y, 2) + pow(s.z - p.z, 2));
}


int main() {
    // Window
        // Main Window
    RenderWindow window(VideoMode(s_l, s_h), "Conecept", Style::Default);


    // Sphere
    S_Point sphere(0, 0, 100);

    S_Point cam(0, 0, 0);

    Image image;
    image.create(s_l, s_h, Colour::Black);
    Texture texture;
    texture.create(s_l, s_h);
    Sprite pic;
    
    {
        double leng = 200.0;
        int x = 0;
        const double PI = 3.14159265358979323846;
        for (double i = PI / 3; i <= (2 * PI) / 3; i += (PI / 3) / (s_l - 1), x++) {
            int y = 0;
            for (double j = PI / 3; j <= (2 * PI) / 3; j += (PI / 3) / (s_h - 1), y++) {
                for (double l = 0; l <= leng; l += 1.0) {
                    if (p_dist(S_Point((-cos(i) * l) + i - (PI / 2), (cos(j) * l) - j + (PI / 2), sin(j) * l), sphere) < 50.0) {
                        image.setPixel(x, y, Colour((-255 / leng * l) + 255, (-255 / leng * l) + 255, (-255 / leng * l) + 255, 255));
                        break;
                    }
                }
            }
        }
    }
    printf("Done!\n");
    texture.update(image);
    pic.setTexture(texture);
    
    
    // Main Window Loop
    for (;;) {

        // Events-

        Event event;  // array of all events
            // See if window has closed
        for (; window.pollEvent(event);) {
            if (event.type == Event::Closed) {
                window.close();
                return 0;
            }
        }


        // Events
        


        // Events+

            // Renderer
        window.clear();
        
            // Objects to draw to screen
        window.draw(pic);
        
            // Display
        window.display();

    }

    return 0;
}
0 Answers
Related