void ContextViewport::_render_single_object()

in roboschool/cpp-household/render-simple.cpp [328:392]


void ContextViewport::_render_single_object(const shared_ptr<Household::ShapeDetailLevels>& m, uint32_t options, int detail, const QMatrix4x4& at_pos)
{
	const std::vector<shared_ptr<Shape>>& shapes = m->detail_levels[detail];

	int cnt = shapes.size();
	for (int c=0; c<cnt; c++) {
		shared_ptr<Shape> t = shapes[c];
		if (!t->vao) {
			if (t->primitive_type!=Shape::MESH && t->primitive_type!=Shape::STATIC_MESH)
				primitives_to_mesh(m, detail, c);
			cx->_shape_to_vao(t);
			//if (t->primitive_type==Shape::DEBUG_LINES) {
			//if (options & VIEW_DEBUG_LINES)
			//render_lines_overlay(t);
		}

		btScalar origin_trans_n_rotate[16];
		t->origin.getOpenGLMatrix(origin_trans_n_rotate);
		QMatrix4x4 shape_pos;
		for (int i=0; i<16; i++) shape_pos.data()[i] = origin_trans_n_rotate[i];

		QMatrix4x4 obj_modelview = modelview * at_pos * shape_pos;
		cx->program_tex->setUniformValue(cx->location_input_matrix_modelview, obj_modelview);
		cx->program_tex->setUniformValue(cx->location_input_matrix_modelview_inverse_transpose, obj_modelview.inverted().transposed());

		uint32_t color = 0;
		uint32_t multiply_color = 0xFFFFFF;
		bool use_texture = false;
		bool meta = options & VIEW_METACLASS;
		if (!meta && t->material) {
			color = t->material->diffuse_color;
			multiply_color = t->material->multiply_color;
			use_texture = !t->t.empty() && t->material->texture;
		}
		if (options & VIEW_COLLISION_SHAPE) color ^= (0xFFFFFF & (uint32_t) (uintptr_t) t.get());
		int triangles = t->v.size();

		{
			float r = float(1/256.0) * ((multiply_color >> 16) & 255);
			float g = float(1/256.0) * ((multiply_color >>  8) & 255);
			float b = float(1/256.0) * ((multiply_color >>  0) & 255);
			cx->program_tex->setUniformValue(cx->location_multiply_color, r, g, b, 1);
		}
		{
			float a = float(1/256.0) * ((color >> 24) & 255);
			if (a==0) a = 1; // allow to specify colors in simple form 0xAABBCC
			float r = float(1/256.0) * ((color >> 16) & 255);
			float g = float(1/256.0) * ((color >>  8) & 255);
			float b = float(1/256.0) * ((color >>  0) & 255);
			cx->program_tex->setUniformValue(cx->location_uni_color, r, g, b, cx->pure_color_opacity*a);
		}

		glBindVertexArray(t->vao->handle);
		if (use_texture) {
			glActiveTexture(GL_TEXTURE0);
			glBindTexture(GL_TEXTURE_2D, t->material->texture);
		} else {
		}
		cx->program_tex->setUniformValue(cx->location_enable_texture, use_texture);
		cx->program_tex->setUniformValue(cx->location_texture, 0);

		glDrawArrays(GL_TRIANGLES, 0, triangles/3);
		glBindVertexArray(0);
	}
}