void draw_object_ex(IDirect3DDevice9 *device, object *obj, BOOL trans){
	unsigned int mesh;
	if(!device) return;
	if(!obj) return;

	if(obj->update){
		vertex *dest;
		generate_normals(obj);
		IDirect3DVertexBuffer9_Lock(obj->vertexbuffer, 0, 0, (BYTE**)&dest, 0 );
		memcpy(dest,obj->vertices,sizeof(vertex)*obj->vertex_count);
		IDirect3DVertexBuffer9_Unlock(obj->vertexbuffer);
		obj->update = FALSE;
	}

	IDirect3DDevice9_SetTransform( device, D3DTS_WORLD, (D3DMATRIX*)&obj->mat );
	IDirect3DDevice9_SetStreamSource(device, 0, obj->vertexbuffer, 0, sizeof(vertex));
	IDirect3DDevice9_SetFVF(device, OBJECT_VERTEX_TYPE);

	for(mesh=0;mesh<obj->submesh_count;mesh++){
		if(obj->submeshes[mesh].mat){
			if(obj->submeshes[mesh].mat->additive==trans){
				set_material(device,obj->submeshes[mesh].mat);
				IDirect3DDevice9_SetIndices(device,obj->submeshes[mesh].indexbuffer);
				IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0,0,obj->vertex_count, 0,obj->submeshes[mesh].triangle_count);
			}
		}else{
			if(!trans){
				IDirect3DDevice9_SetIndices(device,obj->submeshes[mesh].indexbuffer);
				IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLELIST, 0,0,obj->vertex_count, 0,obj->submeshes[mesh].triangle_count);
			}
		}
	}
}
Exemple #2
0
static int Direct3DRenderRegion(vout_display_t *vd,
                                d3d_region_t *region)
{
    vout_display_sys_t *sys = vd->sys;

    LPDIRECT3DDEVICE9 d3ddev = vd->sys->d3ddev;

    LPDIRECT3DVERTEXBUFFER9 d3dvtc = sys->d3dvtc;
    LPDIRECT3DTEXTURE9      d3dtex = region->texture;

    HRESULT hr;

    /* Import vertices */
    void *vertex;
    hr = IDirect3DVertexBuffer9_Lock(d3dvtc, 0, 0, &vertex, D3DLOCK_DISCARD);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return -1;
    }
    memcpy(vertex, region->vertex, sizeof(region->vertex));
    hr = IDirect3DVertexBuffer9_Unlock(d3dvtc);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return -1;
    }

    // Setup our texture. Using textures introduces the texture stage states,
    // which govern how textures get blended together (in the case of multiple
    // textures) and lighting information. In this case, we are modulating
    // (blending) our texture with the diffuse color of the vertices.
    hr = IDirect3DDevice9_SetTexture(d3ddev, 0, (LPDIRECT3DBASETEXTURE9)d3dtex);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return -1;
    }

    // Render the vertex buffer contents
    hr = IDirect3DDevice9_SetStreamSource(d3ddev, 0, d3dvtc, 0, sizeof(CUSTOMVERTEX));
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return -1;
    }

    // we use FVF instead of vertex shader
    hr = IDirect3DDevice9_SetFVF(d3ddev, D3DFVF_CUSTOMVERTEX);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return -1;
    }

    // draw rectangle
    hr = IDirect3DDevice9_DrawPrimitive(d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return -1;
    }
    return 0;
}
void D3DCacheLock(d3d_render_cache *pCache)
{
	int	i;

	IDirect3DVertexBuffer9_Lock((pCache)->xyzBuffer.pVBuffer,
                               0, TEMP_CACHE_MAX * sizeof(custom_xyz),
                               (void **)&(pCache)->xyzBuffer.u.pXYZ,
                               D3DLOCK_DISCARD);
	for (i = 0; i < TEMP_NUM_STAGES; i++)
		IDirect3DVertexBuffer9_Lock((pCache)->stBuffer[i].pVBuffer,
                                  0, TEMP_CACHE_MAX * sizeof(custom_st),
                                  (void **)&(pCache)->stBuffer[i].u.pST,
                                  D3DLOCK_DISCARD);
	IDirect3DVertexBuffer9_Lock((pCache)->bgraBuffer.pVBuffer,
                               0, TEMP_CACHE_MAX * sizeof(custom_bgra),
                               (void **)&(pCache)->bgraBuffer.u.pBGRA,
                               D3DLOCK_DISCARD);
	IDirect3DVertexBuffer9_Lock((pCache)->indexBuffer.pIBuffer,
                               0, TEMP_CACHE_MAX * sizeof(custom_index),
                               (void **)&(pCache)->indexBuffer.pIndex,
                               D3DLOCK_DISCARD);
}
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;
    }
}
Exemple #5
0
static void d3d9_init( sgui_context* context )
{
    float m[16], f, iNF, fov_deg = 60.0f, Near = 0.1f, Far = 100.0f;
    CUSTOMVERTEX vdat[ sizeof(indices)/sizeof(indices[0]) ];
    sgui_d3d9_context* ctx = (sgui_d3d9_context*)context;
    float aspectratio = (float)WIDTH / (float)HEIGHT;
    unsigned int i, idx, r, g, b;

    for( i=0; i<sizeof(indices)/sizeof(indices[0]); ++i )
    {
        idx = indices[ i ];
        r = vertices[ idx*6+3 ] * 255.0f;
        g = vertices[ idx*6+4 ] * 255.0f;
        b = vertices[ idx*6+5 ] * 255.0f;

        vdat[ i ].x = vertices[ idx*6   ];
        vdat[ i ].y = vertices[ idx*6+1 ];
        vdat[ i ].z = vertices[ idx*6+2 ];
        vdat[ i ].color = D3DCOLOR_ARGB(0xFF,r,g,b);
    }

    IDirect3DDevice9_Reset( ctx->device, &ctx->present );

    IDirect3DDevice9_CreateVertexBuffer( ctx->device, sizeof(vdat), 0,
                                         CUSTOMFVF, D3DPOOL_MANAGED,
                                         &v_buffer, NULL );

    /* load vertex data */
    IDirect3DVertexBuffer9_Lock( v_buffer, 0, 0, (void**)&pVoid, 0 );
    memcpy( pVoid, vdat, sizeof(vdat) );
    IDirect3DVertexBuffer9_Unlock( v_buffer );

    /* setup renderer state */
    IDirect3DDevice9_SetRenderState(ctx->device,D3DRS_CULLMODE,D3DCULL_NONE);
    IDirect3DDevice9_SetRenderState(ctx->device,D3DRS_LIGHTING,FALSE       );
    IDirect3DDevice9_SetRenderState(ctx->device,D3DRS_ZENABLE, TRUE        );

    /* set up perspective projection matrix */
    f   = 1.0 / tan( fov_deg * DEGTORAD * 0.5 );
	iNF = 1.0 / ( Near - Far );
    m[0]=f/aspectratio; m[4]=0; m[ 8]=0;              m[12]=0;
    m[1]=0;             m[5]=f; m[ 9]=0;              m[13]=0;
    m[2]=0;             m[6]=0; m[10]=(Far+Near)*iNF; m[14]=2*Far*Near*iNF;
    m[3]=0;             m[7]=0; m[11]=-1;             m[15]=0;
    IDirect3DDevice9_SetTransform(ctx->device,D3DTS_PROJECTION,(D3DMATRIX*)m);

    drawgui = 0;
}
Exemple #6
0
/**
 * It copies picture surface into a texture and renders into a scene.
 *
 * This function is intented for higher end 3D cards, with pixel shader support
 * and at least 64 MiB of video RAM.
 */
static void Direct3DRenderScene(vout_display_t *vd, LPDIRECT3DSURFACE9 surface)
{
    vout_display_sys_t *sys = vd->sys;
    LPDIRECT3DDEVICE9 d3ddev = sys->d3ddev;
    HRESULT hr;

    // check if device is still available
    hr = IDirect3DDevice9_TestCooperativeLevel(d3ddev);
    if (FAILED(hr)) {
        if (hr == D3DERR_DEVICENOTRESET && !sys->reset_device) {
            vout_display_SendEventPicturesInvalid(vd);
            sys->reset_device = true;
        }
        return;
    }
    /* */
    LPDIRECT3DTEXTURE9      d3dtex  = sys->d3dtex;
    LPDIRECT3DVERTEXBUFFER9 d3dvtc  = sys->d3dvtc;

    /* Clear the backbuffer and the zbuffer */
    hr = IDirect3DDevice9_Clear(d3ddev, 0, NULL, D3DCLEAR_TARGET,
                              D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }
    /*  retrieve picture surface */
    LPDIRECT3DSURFACE9 d3dsrc = surface;
    if (!d3dsrc) {
        msg_Dbg(vd, "no surface to render ?");
        return;
    }

    /* retrieve texture top-level surface */
    LPDIRECT3DSURFACE9 d3ddest;
    hr = IDirect3DTexture9_GetSurfaceLevel(d3dtex, 0, &d3ddest);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }

    /* Copy picture surface into texture surface
     * color space conversion and scaling happen here */
    RECT src = vd->sys->rect_src_clipped;
    RECT dst = vd->sys->rect_dest_clipped;

    hr = IDirect3DDevice9_StretchRect(d3ddev, d3dsrc, &src, d3ddest, &dst, D3DTEXF_LINEAR);
    IDirect3DSurface9_Release(d3ddest);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }

    /* Update the vertex buffer */
    CUSTOMVERTEX *vertices;
    hr = IDirect3DVertexBuffer9_Lock(d3dvtc, 0, 0, (void **)&vertices, D3DLOCK_DISCARD);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }

    /* Setup vertices */
    const float f_width  = vd->sys->d3dpp.BackBufferWidth;
    const float f_height = vd->sys->d3dpp.BackBufferHeight;

    /* -0.5f is a "feature" of DirectX and it seems to apply to Direct3d also */
    /* http://www.sjbrown.co.uk/2003/05/01/fix-directx-rasterisation/ */
    vertices[0].x       = -0.5f;       // left
    vertices[0].y       = -0.5f;       // top
    vertices[0].z       = 0.0f;
    vertices[0].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
    vertices[0].rhw     = 1.0f;
    vertices[0].tu      = 0.0f;
    vertices[0].tv      = 0.0f;

    vertices[1].x       = f_width - 0.5f;    // right
    vertices[1].y       = -0.5f;       // top
    vertices[1].z       = 0.0f;
    vertices[1].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
    vertices[1].rhw     = 1.0f;
    vertices[1].tu      = 1.0f;
    vertices[1].tv      = 0.0f;

    vertices[2].x       = f_width - 0.5f;    // right
    vertices[2].y       = f_height - 0.5f;   // bottom
    vertices[2].z       = 0.0f;
    vertices[2].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
    vertices[2].rhw     = 1.0f;
    vertices[2].tu      = 1.0f;
    vertices[2].tv      = 1.0f;

    vertices[3].x       = -0.5f;       // left
    vertices[3].y       = f_height - 0.5f;   // bottom
    vertices[3].z       = 0.0f;
    vertices[3].diffuse = D3DCOLOR_ARGB(255, 255, 255, 255);
    vertices[3].rhw     = 1.0f;
    vertices[3].tu      = 0.0f;
    vertices[3].tv      = 1.0f;

    hr= IDirect3DVertexBuffer9_Unlock(d3dvtc);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }

    // Begin the scene
    hr = IDirect3DDevice9_BeginScene(d3ddev);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }

    // Setup our texture. Using textures introduces the texture stage states,
    // which govern how textures get blended together (in the case of multiple
    // textures) and lighting information. In this case, we are modulating
    // (blending) our texture with the diffuse color of the vertices.
    hr = IDirect3DDevice9_SetTexture(d3ddev, 0, (LPDIRECT3DBASETEXTURE9)d3dtex);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        IDirect3DDevice9_EndScene(d3ddev);
        return;
    }

    // Render the vertex buffer contents
    hr = IDirect3DDevice9_SetStreamSource(d3ddev, 0, d3dvtc, 0, sizeof(CUSTOMVERTEX));
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        IDirect3DDevice9_EndScene(d3ddev);
        return;
    }

    // we use FVF instead of vertex shader
    hr = IDirect3DDevice9_SetFVF(d3ddev, D3DFVF_CUSTOMVERTEX);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        IDirect3DDevice9_EndScene(d3ddev);
        return;
    }

    // draw rectangle
    hr = IDirect3DDevice9_DrawPrimitive(d3ddev, D3DPT_TRIANGLEFAN, 0, 2);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        IDirect3DDevice9_EndScene(d3ddev);
        return;
    }

    // End the scene
    hr = IDirect3DDevice9_EndScene(d3ddev);
    if (FAILED(hr)) {
        msg_Dbg(vd, "%s:%d (hr=0x%0lX)", __FUNCTION__, __LINE__, hr);
        return;
    }
}
Exemple #7
0
void* JBKRender_LockVertexBuffer(JBKVertexBuffer buffer, uint32_t offset, uint32_t size) {
	void* data;
	DXCall( IDirect3DVertexBuffer9_Lock((IDirect3DVertexBuffer9*)buffer, offset, size, &data, 0) );
	return data;
}
Exemple #8
0
int main( void )
{
    LPDIRECT3DVERTEXBUFFER9 v_buffer;
    sgui_window_description desc;
    IDirect3DTexture9* texture;
    sgui_d3d9_context* ctx;
    sgui_canvas* texcanvas;
    IDirect3DDevice9* dev;
    sgui_widget* check2;
    sgui_widget* check;
    sgui_widget* butt;
    sgui_window* wnd;
    float a=0.0f;
    VOID* pVoid;
    float m[16];

    sgui_init( );

    /*************************** create a window **************************/
    desc.parent         = NULL;
    desc.share          = NULL;
    desc.width          = 640;
    desc.height         = 480;
    desc.flags          = SGUI_FIXED_SIZE|SGUI_DOUBLEBUFFERED;
    desc.backend        = SGUI_DIRECT3D_9;
    desc.bits_per_pixel = 32;
    desc.depth_bits     = 16;
    desc.stencil_bits   = 0;
    desc.samples        = 0;

    wnd = sgui_window_create_desc( &desc );

    sgui_window_set_title( wnd, "Direct3D 9 Texture Canvas" );
    sgui_window_move_center( wnd );
    sgui_window_set_visible( wnd, SGUI_VISIBLE );
    sgui_window_set_vsync( wnd, 1 );

    /************************ createtexture canvas ************************/
    texcanvas = sgui_tex_canvas_create( wnd, sgui_window_get_context( wnd ),
                                        128, 128 );

    butt = sgui_button_create( 10, 10, 60, 25, "Button", 0 );
    check = sgui_checkbox_create( 10, 40, "Direct3D" );
    check2 = sgui_checkbox_create( 10, 65, "Texture" );

    sgui_button_set_state( check, 1 );
    sgui_button_set_state( check2, 1 );

    sgui_widget_add_child( &texcanvas->root, butt );
    sgui_widget_add_child( &texcanvas->root, check );
    sgui_widget_add_child( &texcanvas->root, check2 );

    /************** connect keyboard input to texture canvas **************/
    sgui_event_connect( wnd, SGUI_KEY_PRESSED_EVENT,
                        sgui_canvas_send_window_event, texcanvas,
                        SGUI_FROM_EVENT, SGUI_EVENT );

    sgui_event_connect( wnd, SGUI_KEY_RELEASED_EVENT,
                        sgui_canvas_send_window_event, texcanvas,
                        SGUI_FROM_EVENT, SGUI_EVENT );

    sgui_event_connect( wnd, SGUI_CHAR_EVENT,
                        sgui_canvas_send_window_event, texcanvas,
                        SGUI_FROM_EVENT, SGUI_EVENT );

    /*************************** Direct3D setup ***************************/
    /* get the device context, set new present parameters */
    ctx = (sgui_d3d9_context*)sgui_window_get_context( wnd );
    dev = ctx->device;

    IDirect3DDevice9_Reset( dev, &ctx->present );

    /* create a vertex buffer */
    IDirect3DDevice9_CreateVertexBuffer( dev, sizeof(vertices), 0,
                                         CUSTOMFVF, D3DPOOL_MANAGED,
                                         &v_buffer, NULL );

    /* load vertex data */
    IDirect3DVertexBuffer9_Lock( v_buffer, 0, 0, (void**)&pVoid, 0 );
    memcpy( pVoid, vertices, sizeof(vertices) );
    IDirect3DVertexBuffer9_Unlock( v_buffer );

    /* setup renderer state */
    IDirect3DDevice9_SetRenderState( dev, D3DRS_CULLMODE, D3DCULL_NONE );
    IDirect3DDevice9_SetRenderState( dev, D3DRS_LIGHTING, FALSE );
    IDirect3DDevice9_SetRenderState( dev, D3DRS_ZENABLE, TRUE );

    /* set up perspective projection matrix */
    perspective( m, 45.0f, (float)desc.width/(float)desc.height,
                 0.1f, 100.0f );
    IDirect3DDevice9_SetTransform( dev, D3DTS_PROJECTION, (D3DMATRIX*)m );

    /* set up texturing, bind canvas texture to stage 0 */
    texture = sgui_tex_canvas_get_texture( texcanvas );

    IDirect3DDevice9_SetTexture( dev, 0, (IDirect3DBaseTexture9*)texture );
    IDirect3DDevice9_SetTextureStageState( dev, 0, D3DTSS_COLOROP,
                                           D3DTOP_BLENDTEXTUREALPHA );
    IDirect3DDevice9_SetTextureStageState( dev, 0, D3DTSS_COLORARG1,
                                           D3DTA_TEXTURE );
    IDirect3DDevice9_SetTextureStageState( dev, 0, D3DTSS_COLORARG2,
                                           D3DTA_DIFFUSE );
    IDirect3DDevice9_SetTextureStageState( dev, 0, D3DTSS_ALPHAOP,
                                           D3DTOP_DISABLE );

    IDirect3DDevice9_SetSamplerState(dev,0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
    IDirect3DDevice9_SetSamplerState(dev,0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);

    /****************************** main loop *****************************/
    while( sgui_main_loop_step( ) )
    {
        /* redraw widgets in dirty areas */
        sgui_canvas_redraw_widgets( texcanvas, 1 );

        /* draw scene */
        IDirect3DDevice9_Clear( dev, 0, NULL,
                                D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                                0, 1.0f, 0 );

        IDirect3DDevice9_BeginScene( dev );

        transform( m, a );
        IDirect3DDevice9_SetTransform( dev, D3DTS_VIEW, (D3DMATRIX*)m );
        a += 0.01f;

        IDirect3DDevice9_SetFVF( dev, CUSTOMFVF );
        IDirect3DDevice9_SetStreamSource( dev, 0, v_buffer, 0,
                                          sizeof(CUSTOMVERTEX) );

        IDirect3DDevice9_DrawPrimitive( dev, D3DPT_TRIANGLELIST, 0, 8 );

        IDirect3DDevice9_EndScene( dev );

        /* swap front and back buffer */
        sgui_window_swap_buffers( wnd );
    }

    /****************************** clean up ******************************/
    sgui_canvas_destroy( texcanvas );
    sgui_widget_destroy( check2 );
    sgui_widget_destroy( check );
    sgui_widget_destroy( butt );

    IDirect3DVertexBuffer9_Release( v_buffer );

    sgui_window_destroy( wnd );
    sgui_deinit( );
    return 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;
}