第一章
生成图片
在vs中(我用的2019vs)新建你本次学习的项目,然后输入你的代码
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile;
outfile.open("firstpicture.txt");
int nx = 200;
int ny = 100;
outfile << "P3\n" << nx << " " << ny << "\n255\n";
for (int j = ny - 1; j >= 0; j--)
{
for (int i = 0; i < nx; i++)
{
float r = float(i) / float(nx);
float g = float(j) / float(ny);
float b = 0.2f;
int ir = int(255.99f*r);
int ig = int(255.99f*g);
int ib = int(255.99f*b);
outfile << ir << " " << ig << " " << ib << "\n";
}
}
outfile.close();
return 0;
}
在你的代码的同目录下找到firstpicture.txt 将其改为firstpicture.ppm
然后将其拖到ps()里去,就能够看到
按住ctrl+ ‘+’ 放大可以观察其像素点
我们可以改变代码中rgb的值,这是我是修改
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile;
outfile.open("firstImage.txt");
int nx = 200;
int ny = 100;
outfile << "P3\n" << nx << " " << ny << "\n255\n";
for (int j = ny - 1; j >= 0; j--)
{
for (int i = 0; i < nx; i++)
{
float r = float(j) / float(ny);
float g = float(i) / float(nx);
float b = 0.2f;
int ir = 50;
int ig = int(100.99f * g);
int ib = int(255.99f * b);
outfile << ir << " " << ig << " " << ib << "\n";
}
}
outfile.close();
return 0;
}
得到图片