examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

model.cpp (1973B)


      1 #include <iostream>
      2 #include <sstream>
      3 #include <vector>
      4 #include <set>
      5 
      6 using namespace std;
      7 
      8 struct vertex {
      9 	float x, y, z;
     10 };
     11 
     12 std::vector<vertex> v;
     13 
     14 string buf;
     15 
     16 void add_vertices(void)
     17 {
     18 	cout << "Adding Vertices...\n";
     19 	float x, y, z;
     20 	while (getline(cin, buf))
     21 	{
     22 		istringstream ss;
     23 		ss.str(buf);
     24 		ss >> x >> y >> z;
     25 		if (!ss)
     26 			break;
     27 		v.push_back({x, y, z});
     28 	}
     29 }
     30 
     31 struct primitives
     32 {
     33 	string type;
     34 	vector<unsigned char> indices;
     35 };
     36 
     37 primitives add_primitives(string type)
     38 {
     39 	primitives p;
     40 	p.type = type;
     41 
     42 	while (getline(cin, buf))
     43 	{
     44 		istringstream ss;
     45 		ss.str(buf);
     46 
     47 		unsigned const line_start = p.indices.size();
     48 
     49 		unsigned tmp;
     50 		while (ss >> tmp)
     51 		{
     52 			if (tmp > v.size())
     53 				throw std::runtime_error("Primitive index out of range!");
     54 
     55 			if (line_start && line_start == p.indices.size())
     56 				p.indices.push_back(0xff);
     57 
     58 			p.indices.push_back(tmp);
     59 		}
     60 
     61 		if (line_start == p.indices.size())
     62 			break;
     63 	}
     64 
     65 	if (p.indices.empty())
     66 		throw std::runtime_error("Empty Primitive array!");
     67 
     68 	return p;
     69 }
     70 
     71 void print_primitives(primitives const& p)
     72 {
     73 	cout << p.type << '\n';
     74 	unsigned n = 0;
     75 	for (auto P : p.indices)
     76 	{
     77 		if (P == 0xff)
     78 			cout << "Restart\n";
     79 		else
     80 			cout << "   " << n++ << ": " << +P << " [" << v[P].x << ' ' << v[P].y << ' ' << v[P].z << "]\n";
     81 	}
     82 }
     83 
     84 set<string> types = {
     85 	"GL_POINTS",
     86 	"GL_LINES",
     87 	"GL_LINE_STRIP",
     88 	"GL_LINE_LOOP",
     89 	"GL_TRIANGLES",
     90 	"GL_TRIANGLE_STRIP",
     91 	"GL_TRIANGLE_FAN",
     92 	"GL_QUADS",
     93 	"GL_QUAD_STRIP",
     94 };
     95 
     96 int main()
     97 {
     98 	std::vector<primitives> prims;
     99 
    100 	while (getline(cin, buf))
    101 	{
    102 		istringstream ss;
    103 		ss.str(buf);
    104 		ss >> buf;
    105 
    106 		if (buf == "GL_FLOAT")
    107 			add_vertices();
    108 
    109 		else if (types.count(buf))
    110 			prims.push_back(add_primitives(buf));
    111 	}
    112 
    113 	cout << "\nPrinting Vertices:\n";
    114 	for (auto V : v)
    115 		cout << '[' << V.x << ' ' << V.y << ' ' << V.z << "]\n";
    116 
    117 	unsigned n = 0;
    118 	cout << "\nPrinting Primitives:\n";
    119 	for (auto const& p : prims)
    120 	{
    121 		cout << n++ << ": ";
    122 		print_primitives(p);
    123 	}
    124 }