a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
71 {
72 if(m_mesh.vertex_count() == 0)
73 return 0;
74
75
76 const std::vector<vec3>& positions= m_mesh.positions();
77 std::vector<unsigned> indices= m_mesh.indices();
78
79
80 std::map<vec3, unsigned, vec3_less> vertex;
81 std::vector<unsigned> remap( indices.size() );
82 for(unsigned i= 0; i < positions.size(); i++)
83 {
84 auto v= vertex.insert( { positions[i], i } );
85 remap[i]= v.first->second;
86 }
87
88 for(unsigned i= 0; i < indices.size(); i++)
89 indices[i]= remap[ indices[i] ];
90
91 printf(
"remap %u/%u vertices...\n",
unsigned(vertex.size()),
unsigned(remap.size()));
92
93
94 std::set<unsigned> cluster;
95 std::vector<unsigned> cluster_id(indices.size(), -1);
96 std::vector<unsigned> triangle_id;
97 triangle_id.reserve(indices.size()/3);
98
99 unsigned id= 0;
100 for(unsigned i= 0; i < indices.size(); i++)
101
102 {
103 unsigned a= indices[i];
104 cluster.insert(a);
105
106
107
108
109
110 if(cluster.size() >= 32)
111 {
112 id++;
113 cluster.clear();
114 }
115
116 triangle_id.push_back(id);
117 }
118 unsigned count= id+1;
119 cluster.clear();
120
121
122 std::vector<vec3> colors( count );
123
124 std::random_device hwseed;
125 std::default_random_engine rng( hwseed() );
126 std::uniform_real_distribution<float> uniform;
127
128 for(unsigned i= 0; i < colors.size(); i++)
129 colors[i]= { uniform(rng), uniform(rng), uniform(rng) };
130
131
132 std::vector<vec3> vertex_colors;
133 std::vector<vec3> vertex_positions;
134 vertex_colors.reserve( indices.size() );
135 vertex_positions.reserve( indices.size() );
136 for(unsigned i= 0; i +2 < indices.size(); i+= 3)
137 {
138 unsigned a= indices[i];
139 unsigned b= indices[i+1];
140 unsigned c= indices[i+2];
141
142 vertex_positions.push_back( positions[ a ] );
143 vertex_positions.push_back( positions[ b ] );
144 vertex_positions.push_back( positions[ c ] );
145
146 vertex_colors.push_back( colors[ triangle_id[i/3] ] );
147 vertex_colors.push_back( colors[ triangle_id[i/3] ] );
148 vertex_colors.push_back( colors[ triangle_id[i/3] ] );
149 }
150
151 glGenVertexArrays(1, &vao);
152 glBindVertexArray(vao);
153
154 glGenBuffers(1, &vertex_buffer);
155 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
156 glBufferData(GL_ARRAY_BUFFER, vertex_positions.size() * sizeof(vec3), vertex_positions.data(), GL_STATIC_DRAW);
157 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
158 glEnableVertexAttribArray(0);
159
160 glGenBuffers(1, &cluster_buffer);
161 glBindBuffer(GL_ARRAY_BUFFER, cluster_buffer);
162 glBufferData(GL_ARRAY_BUFFER, vertex_colors.size() * sizeof(vec3), vertex_colors.data(), GL_STATIC_DRAW);
163 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
164 glEnableVertexAttribArray(1);
165
166
167 Point pmin, pmax;
168 m_mesh.bounds(pmin, pmax);
170
171
174
175 m_program_display=
read_program(
"gkit2_tutos/M2/mesh_display.glsl");
177
178
179 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
180
181 glClearDepth(1.f);
182 glDepthFunc(GL_LESS);
183 glEnable(GL_DEPTH_TEST);
184
185 glFrontFace(GL_CCW);
186 glCullFace(GL_BACK);
187
188
189 return 1;
190 }
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
void lookat(const Point ¢er, const float size)
observe le point center a une distance size.
GLuint read_program(const char *filename, const char *definitions)
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.