Example #1
0
static void createVertexBuffersD3D9()
{
    HRESULT hr;
    LPVOID ptr = NULL;
    int sizeInBytes = sizeof(float)*3*NUM_VERTICES;
    D3DVERTEXELEMENT9 decl[] = {
        { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
        D3DDECL_END(),
    };

    hr = IDirect3DDevice9_CreateVertexBuffer(gDevicePtr, sizeInBytes, D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &gVertexBufferPtr, NULL);
    if(FAILED(hr) || !gVertexBufferPtr) {
        fprintf(stderr, "Failed to create vertex buffer\n");
        return;
    }
    hr = IDirect3DVertexBuffer9_Lock(gVertexBufferPtr, 0, 0, &ptr, D3DLOCK_DISCARD);
    if (FAILED(hr) || !ptr) {
        fprintf(stderr, "Failed to lock vertex buffer\n");
        return;
    }
    memcpy(ptr, icosahedronVertices, sizeInBytes);
    hr = IDirect3DVertexBuffer9_Unlock(gVertexBufferPtr);
    if (FAILED(hr)) {
        fprintf(stderr, "Failed to unlock vertex buffer\n");
        return;
    }

    sizeInBytes = sizeof(unsigned short)*3*NUM_INDICES;
    hr = IDirect3DDevice9_CreateIndexBuffer(gDevicePtr, sizeInBytes, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &gIndexBufferPtr, NULL);
    if(FAILED(hr) || !gIndexBufferPtr) {
        fprintf(stderr, "Failed to create index buffer\n");
        return;
    }
    hr = IDirect3DIndexBuffer9_Lock(gIndexBufferPtr, 0, 0, &ptr, D3DLOCK_DISCARD);
    if (FAILED(hr) || !ptr) {
        fprintf(stderr, "Failed to lock index buffer\n");
        return;
    }
    memcpy(ptr, icosahedronIndices, sizeInBytes);
    hr = IDirect3DIndexBuffer9_Unlock(gIndexBufferPtr);
    if (FAILED(hr)) {
        fprintf(stderr, "Failed to unlock index buffer\n");
        return;
    }

    hr = IDirect3DDevice9_CreateVertexDeclaration(gDevicePtr, decl, &gVertexDeclPtr);
    if(FAILED(hr) || !gVertexDeclPtr) {
        fprintf(stderr, "Failed to create vertex declaration\n");
        return;
    }
}
Example #2
0
object *load_object(IDirect3DDevice9 *device, char *filename){
	matrix position, rotation, scale;
	vector position_vector, scale_vector;
	quat rotation_quat;

	unsigned int submesh_counter;
	char test[4];
	object *temp;
	file *fp;
	vertex *dest;

	if(!filename) return NULL;

	fp = file_open(filename);
	if(!fp) return NULL;

	file_read(test,1,4,fp);
	if(memcmp(test,"KRO0",4)!=0){
		file_close(fp);
		return NULL;
	}

	temp = (object*)malloc(sizeof(object));
	if(!temp) return NULL;

	file_read(&position_vector,sizeof(float),3,fp);
	file_read(&rotation_quat,sizeof(float),4,fp);
	file_read(&scale_vector,sizeof(float),3,fp);

	file_read(&temp->vertex_count,1,4,fp);
	if(temp->vertex_count==0){
		/* dummy-object, skip loading */
		file_close(fp);
	
		temp->update = FALSE;
		temp->vertexbuffer = NULL;
		temp->vertices = 0;
		temp->submesh_count = 0;
		temp->submeshes = NULL;
		/* save prs */
		temp->prs.pos = position_vector;
		temp->prs.rot = rotation_quat;
		temp->prs.scale = scale_vector;

		/* build matrix */
		matrix_identity(temp->mat);
		matrix_translate(position, position_vector);
		matrix_from_quat(rotation, rotation_quat);
		matrix_scale(scale, scale_vector);
		matrix_multiply(temp->mat, temp->mat, position);
		matrix_multiply(temp->mat, temp->mat, rotation);
		matrix_multiply(temp->mat, temp->mat, scale);
		return temp;
	}

	temp->vertices = (vertex*)malloc(sizeof(vertex)*temp->vertex_count);
	if(!temp->vertices) return FALSE;
	file_read(temp->vertices,sizeof(vertex),temp->vertex_count,fp);

	file_read(&temp->morphtarget_count,1,4,fp);
	temp->morphtarget_vertices = malloc(sizeof(vector)*temp->vertex_count*temp->morphtarget_count);
	if(!temp->morphtarget_vertices) return FALSE;
	file_read(temp->morphtarget_vertices,sizeof(vector),temp->vertex_count*temp->morphtarget_count,fp);

	file_read(&temp->submesh_count,1,4,fp);
	temp->submeshes = (submesh*)malloc(sizeof(submesh)*temp->submesh_count);
	if(!temp->submeshes) return FALSE;

	for(submesh_counter=0;submesh_counter<temp->submesh_count;submesh_counter++){
		unsigned int* dest;
		char mat_name[256];
		char *material_name = file_loadstring(fp);

		sprintf(mat_name,"%s%s",get_dirname(filename),material_name); 
		temp->submeshes[submesh_counter].mat = material_load(device,mat_name);

		file_read(&temp->submeshes[submesh_counter].triangle_count,1,4,fp);
		temp->submeshes[submesh_counter].triangles = (unsigned short*)malloc(sizeof(unsigned short)*3*temp->submeshes[submesh_counter].triangle_count);
		if(!temp->submeshes[submesh_counter].triangles) return FALSE;
		file_read(temp->submeshes[submesh_counter].triangles,sizeof(unsigned short)*3,temp->submeshes[submesh_counter].triangle_count,fp);

		if(FAILED(IDirect3DDevice9_CreateIndexBuffer(device,sizeof(unsigned short)*3*temp->submeshes[submesh_counter].triangle_count,0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&temp->submeshes[submesh_counter].indexbuffer,NULL)))
			return FALSE;

		if(IDirect3DIndexBuffer9_Lock(temp->submeshes[submesh_counter].indexbuffer,0,0,&dest,0)==D3D_OK){
			memcpy(dest,temp->submeshes[submesh_counter].triangles,sizeof(unsigned short)*3*temp->submeshes[submesh_counter].triangle_count);
			IDirect3DIndexBuffer9_Unlock(temp->submeshes[submesh_counter].indexbuffer);
		}else return FALSE;
	}
	file_close(fp);

	if(temp->morphtarget_count > 0) {
		printf("something shall morph!");
		if(FAILED(IDirect3DDevice9_CreateVertexBuffer(device,sizeof(vertex)*temp->vertex_count,D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,OBJECT_VERTEX_TYPE,D3DPOOL_DEFAULT,&temp->vertexbuffer,NULL)))
			return FALSE;
	} else {
		if(FAILED(IDirect3DDevice9_CreateVertexBuffer(device,sizeof(vertex)*temp->vertex_count,0,OBJECT_VERTEX_TYPE,D3DPOOL_MANAGED,&temp->vertexbuffer,NULL)))
			return FALSE;
	}
	if(IDirect3DVertexBuffer9_Lock(temp->vertexbuffer, 0, 0, (BYTE**)&dest, 0)==D3D_OK){
		memcpy(dest,temp->vertices,sizeof(vertex)*temp->vertex_count);
		IDirect3DVertexBuffer9_Unlock(temp->vertexbuffer);
		temp->update = FALSE;
	}else return FALSE;

	/* save prs */
	temp->prs.pos = position_vector;
	temp->prs.rot = rotation_quat;
	temp->prs.scale = scale_vector;

	/* build matrix */
	matrix_identity(temp->mat);
	matrix_translate(position, position_vector);
	matrix_from_quat(rotation, rotation_quat);
	matrix_scale(scale, scale_vector);
	matrix_multiply(temp->mat, temp->mat, position);
	matrix_multiply(temp->mat, temp->mat, rotation);
	matrix_multiply(temp->mat, temp->mat, scale);

	return temp;
}