#Writing to ppm file not working, getting garbage output

15 messages · Page 1 of 1 (latest)

heavy quail
#

I'm following TinyRayTracer and can't properly output to ppm file. I am on windows.

#include <limits>
#include <cmath>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm> //<-- max/min 
#include "geometry.h"
void render() {
    const int width = 1024;
    const int height = 768;
    std::vector<Vec3f> framebuffer(width*height);
    for (size_t j = 0; j < height; j++) {
        for (size_t i = 0; i < width; i++) {
            framebuffer[i + j * width] = Vec3f(j / float(height), i / float(width), 0);
        }
    }
    std::ofstream ofs; // save the framebuffer to file
    ofs.open("./out.ppm", std::ofstream::out | std::ofstream::binary); 
    ofs << "P6\n" << width << " " << height << "\n255\n";
    for (size_t i = 0; i < height*width; ++i) {
        for (size_t j = 0; j < 3; j++) {
            ofs << (char)(255 * std::max(0.f, std::min(1.f, framebuffer[i][j])));
        }
    }
    ofs.close();
}
int main() {
    render();
    return 0;
}

The line giving me issues is ofs.open("./out.ppm", std::ofstream::out | std::ofstream::binary);
I've also tried writing ofs.open("./out.ppm",std::ios::binary); and ofs.open("./out.ppm"); to no avail. Some help would be appreciated. I am on windows, using powershell to compile and run. compiling by using g++ .\main.cpp -o raytracer and running .\raytracer.exe

fallow kayak
fallow kayak
heavy quail
#

but i wasnt getting any errors opening file and i was writing to file

#

it just produced a bunch of garbage results

#

weird characters and all

fallow kayak
#

Add the new lines at the end of the pixel

#

You have 2 different problems in ur code

#

cout char tries to print the ASCII
Many of which are kinda weirdly rendered
Ppm requires you to cout the number in 0-255 range

urban wraith
fallow kayak
#

Yea

urban wraith
#

@heavy quail how does framebuffer[i][j] even work if its 1-dimensional?

#

Oh, framebuffer[i] gives the Vec3 and then [j] extracts the component