intmain(int argc, char** argv){ TGAImage image(100, 100, TGAImage::RGB); image.set(52, 41, red); image.flip_vertically(); // i want to have the origin at the left bottom corner of the image image.write_tga_file("output.tga"); return0; }
voidline(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color){ for(float t=0.0; t<1.0; t += 0.01){ int x = x0 + (x1-x0)*t; int y = y0 + (y1-y0)*t; image.set(x,y,color); } }
voidline(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color){ bool steep = false; if (std::abs(x0-x1)<std::abs(y0-y1)) { std::swap(x0, y0); std::swap(x1, y1); steep = true; } if (x0>x1) { std::swap(x0, x1); std::swap(y0, y1); } int dx = x1-x0; int dy = y1-y0; int derror2 = std::abs(dy)*2; int error2 = 0; int y = y0; for (int x=x0; x<=x1; x++) { if (steep) { image.set(y, x, color); } else { image.set(x, y, color); } error2 += derror2; if (error2 > dx) { y += (y1>y0?1:-1); error2 -= dx*2; } } }
那么我们就可以利用线来绘制三维模型的mesh网格了 这里就涉及到obj文件的读取,问题不大
定义好存储obj的数据结构
1 2 3 4 5 6 7 8 9 10 11 12
classObj { public: int vn = 0; int fn = 0; std::vector<float> vx_list; //顶点x坐标 std::vector<float> vy_list;//顶点y坐标 std::vector<int> f_list;//三角面片信息
Obj(); voidread_obj(std::string filename);//读取的函数
};
读取obj
接下来我们就要分析一下obj文件的构成,来决定怎么读取。
记事本打开一个obj,可以看到有这样的
1 2 3 4 5 6
v -0.000581696 -0.734665 -0.623267 v 0.000283538 -1 0.286843 v -0.117277 -0.973564 0.306907 v -0.382144 -0.890788 0.221243 v -0.247144 -0.942602 0.276051 v -0.656078 -0.718512 -0.109025
前面的v表示这是一个顶点的三维坐标
还有这样的
1 2 3 4 5 6 7 8 9 10
f 1091/1258/1091 1209/1261/1209 1131/1266/1131 f 1258/1339/1258 1206/1252/1206 1205/1255/1205 f 1258/1339/1258 1208/1256/1208 1206/1252/1206 f 1258/1339/1258 1215/1270/1215 1214/1267/1214 f 1189/1236/1189 1138/1184/1138 1160/1207/1160 f 1185/1231/1185 1138/1184/1138 1189/1236/1189 f 1202/1248/1202 1220/1281/1220 1200/1246/1200 f 1200/1246/1200 1220/1281/1220 1198/1247/1198 f 1201/1249/1201 1200/1246/1200 1199/1245/1199 f 1201/1249/1201 1202/1248/1202 1200/1246/1200