Ejemplo n.º 1
0
Mesh* COLMeshConverter::convert(const COLBox& box)
{
	MeshGenerator gen;
	float* vertices;
	uint32_t* indices;
	unsigned int vertexCount, indexCount;
	gen.createBox(vertices, vertexCount, indices, indexCount, box.getMinimum(), box.getMaximum());

	const COLSurface& surface = box.getSurface();
	uint8_t matNum = surface.getMaterial();
	uint8_t* colors = new uint8_t[vertexCount*4];
	uint8_t r, g, b;
	getMaterialColors(matNum, r, g, b);

	for (unsigned int i = 0 ; i < vertexCount ; i++) {
		colors[i*4] = r;
		colors[i*4 + 1] = g;
		colors[i*4 + 2] = b;
		colors[i*4 + 3] = 255;
	}

	//printf("glBufferData COLMeshConverter 2\n");

	GLuint dataBuffer;
	glGenBuffers(1, &dataBuffer);
	glBindBuffer(GL_ARRAY_BUFFER, dataBuffer);

	glBufferData(GL_ARRAY_BUFFER, vertexCount*3*4 + vertexCount*4, NULL, GL_STATIC_DRAW);
	Engine::getInstance()->increaseTestMem(vertexCount*3*4 + vertexCount*4, __FILE__, __LINE__);

	glBufferSubData(GL_ARRAY_BUFFER, 0, vertexCount*3*4, vertices);
	glBufferSubData(GL_ARRAY_BUFFER, vertexCount*3*4, vertexCount*4, colors);

	//Mesh* mesh = new Mesh(vertexCount, VertexFormatTriangles, 0, dataBuffer, -1, -1, vertexCount*3*4);
	Mesh* mesh = new Mesh(vertexCount, VertexFormatTriangles, 0, dataBuffer, vertexCount*3*4 + vertexCount*4,
			0, 0, -1, -1, -1, -1, vertexCount*3*4, 0);

	Submesh* submesh = new Submesh(mesh, indexCount, indices);
	mesh->link();

	//mesh->setBounds(center[0], center[1], center[2], sphere.getRadius());

	delete[] vertices;
	delete[] indices;

	return mesh;
}
Ejemplo n.º 2
0
Mesh* COLMeshConverter::convert(const COLModel& model)
{
	const COLSphere* spheres = model.getSphereCount() != 0 ? &model.getSpheres()[0] : NULL;
	const COLBox* boxes = model.getBoxCount() != 0 ? &model.getBoxes()[0] : NULL;

	GLuint dataBuffer;
    glGenBuffers(1, &dataBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, dataBuffer);

    unsigned int vertexCount = 0;
    uint32_t sphereCount = model.getSphereCount();
    uint32_t boxCount = model.getBoxCount();

    float** boxVertexArrays = new float*[boxCount];
    float** sphereVertexArrays = new float*[sphereCount];
    uint32_t** boxIndexArrays = new uint32_t*[boxCount];
    uint32_t** sphereIndexArrays = new uint32_t*[sphereCount];
    unsigned int* boxVertexCounts = new unsigned int[boxCount];
    unsigned int* sphereVertexCounts = new unsigned int[sphereCount];
    unsigned int* boxIndexCounts = new unsigned int[boxCount];
    unsigned int* sphereIndexCounts = new unsigned int[sphereCount];

    MeshGenerator gen;

    for (uint32_t i = 0 ; i < sphereCount ; i++) {
		const COLSphere& sphere = spheres[i];
		float* tmpNormals;
		gen.createSphere(sphereVertexArrays[i], tmpNormals, sphereVertexCounts[i], sphereIndexArrays[i],
				sphereIndexCounts[i], sphere.getRadius(), 4, 4, sphere.getCenter());
		delete[] tmpNormals;
        vertexCount += sphereVertexCounts[i];
    }

    for (uint32_t i = 0 ; i < boxCount ; i++) {
        const COLBox& box = boxes[i];
        gen.createBox(boxVertexArrays[i], boxVertexCounts[i], boxIndexArrays[i], boxIndexCounts[i],
                  box.getMinimum(), box.getMaximum());
        vertexCount += boxVertexCounts[i];
    }

    unsigned int modelVertexCount;
    float* modelVertices;
    uint8_t* modelColors;
    uint32_t* modelIndices;
    unsigned int modelIndexCount;

    convertVertexModel(model.getVertices(), model.getVertexCount(),
    		model.getFaceCount() != 0 ? &model.getFaces()[0] : NULL, model.getFaceCount(),
    		modelVertexCount, modelVertices, modelColors, modelIndices, modelIndexCount);
    vertexCount += modelVertexCount;

    //printf("glBufferData COLMeshConverter 3\n");

    glBufferData(GL_ARRAY_BUFFER, vertexCount*3*4 + vertexCount*4, NULL, GL_STATIC_DRAW);
    Engine::getInstance()->increaseTestMem(vertexCount*3*4 + vertexCount*4, __FILE__, __LINE__);
    int vertexOffset = 0;
    int colorOffset = vertexCount*3*4;

    glBufferSubData(GL_ARRAY_BUFFER, 0, modelVertexCount*3*4, modelVertices);
    glBufferSubData(GL_ARRAY_BUFFER, colorOffset, modelVertexCount*4, modelColors);
    vertexOffset = modelVertexCount;

    /*Mesh* mesh = new Mesh(vertexCount, VertexFormatTriangles, MeshVertexColors, dataBuffer, -1, -1,
    		colorOffset);*/

    Mesh* mesh = new Mesh(vertexCount, VertexFormatTriangles, 0, dataBuffer, vertexCount*3*4 + vertexCount*4,
			0, 0, -1, -1, -1, -1, vertexCount*3*4, 0);

    delete[] modelVertices;
    delete[] modelColors;

    uint8_t r, g, b;

    for (uint32_t i = 0 ; i < sphereCount ; i++) {
    	const COLSphere& sphere = spheres[i];
    	unsigned int vertexCount = sphereVertexCounts[i];

    	// Create the vertex colors array
    	uint8_t* colors = new uint8_t[vertexCount*4];
    	const COLSurface& surface = sphere.getSurface();
    	uint8_t matNum = surface.getMaterial();
    	getMaterialColors(matNum, r, g, b);

        for (unsigned int j = 0 ; j < vertexCount ; j++) {
        	colors[j*4] = r;
        	colors[j*4+1] = g;
        	colors[j*4+2] = b;
        	colors[j*4+3] = 255;
        }

        // Fill the buffers with vertex data
        glBufferSubData(GL_ARRAY_BUFFER, vertexOffset*3*4, vertexCount*3*4, sphereVertexArrays[i]);
        glBufferSubData(GL_ARRAY_BUFFER, colorOffset + vertexOffset*4, vertexCount*4, colors);
        vertexOffset += vertexCount;

        delete[] colors;
    }

    for (uint32_t i = 0 ; i < boxCount ; i++) {
        const COLBox& box = boxes[i];
    	unsigned int vertexCount = boxVertexCounts[i];

    	// Create the vertex colors array
    	uint8_t* colors = new uint8_t[vertexCount*4];
    	const COLSurface& surface = box.getSurface();
    	uint8_t matNum = surface.getMaterial();
    	getMaterialColors(matNum, r, g, b);

        for (unsigned int j = 0 ; j < vertexCount ; j++) {
        	colors[j*4] = r;
        	colors[j*4+1] = g;
        	colors[j*4+2] = b;
        	colors[j*4+3] = 255;
        }

        // Fill the buffers with vertex data
        glBufferSubData(GL_ARRAY_BUFFER, vertexOffset*3*4, vertexCount*3*4, boxVertexArrays[i]);
        glBufferSubData(GL_ARRAY_BUFFER, colorOffset + vertexOffset*4, vertexCount*4, colors);
        vertexOffset += vertexCount;

        delete[] colors;
    }

    Submesh* submesh = new Submesh(mesh, modelIndexCount, modelIndices);
    vertexOffset = modelVertexCount;

    delete[] modelIndices;

    for (uint32_t i = 0 ; i < sphereCount ; i++) {
    	unsigned int indexCount = sphereIndexCounts[i];
    	uint32_t* indices = sphereIndexArrays[i];
    	for (unsigned int j = 0 ; j < indexCount ; j++) {
    		indices[j] += vertexOffset;
    	}

    	Submesh* submesh = new Submesh(mesh, indexCount, indices);
    	vertexOffset += sphereVertexCounts[i];

    	delete[] sphereVertexArrays[i];
    	delete[] sphereIndexArrays[i];
    }

    delete[] sphereVertexArrays;
    delete[] sphereIndexArrays;
    delete[] sphereVertexCounts;
    delete[] sphereIndexCounts;

    for (uint32_t i = 0 ; i < boxCount ; i++) {
    	unsigned int indexCount = boxIndexCounts[i];
    	uint32_t* indices = boxIndexArrays[i];
    	for (unsigned int j = 0 ; j < indexCount ; j++) {
    		indices[j] += vertexOffset;
    	}

        Submesh* submesh = new Submesh(mesh, indexCount, indices);
        vertexOffset += boxVertexCounts[i];

        delete[] boxVertexArrays[i];
        delete[] boxIndexArrays[i];
    }

    delete[] boxVertexArrays;
    delete[] boxIndexArrays;
    delete[] boxVertexCounts;
    delete[] boxIndexCounts;

    mesh->link();

    const COLBounds& bounds = model.getBounds();
    const Vector3& center = bounds.getCenter();
    mesh->setBounds(center.getX(), center.getY(), center.getZ(), bounds.getRadius());

    return mesh;
}