/src/renderer/visual.cpp
https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 236 lines · 175 code · 47 blank · 14 comment · 11 complexity · 7afde4d616d7bf4267102cb6292b1c0b MD5 · raw file
- #include "renderer/visual.h"
- #include "common.h"
- #include "log.h"
- #include "load_data.h"
- //-----------------------------------------------------------------------------
-
- #define MAX_VISUALS 1024
- //-----------------------------------------------------------------------------
-
- visual_t visuals[MAX_VISUALS] = {0};
- uint32 visuals_count = 0;
- mesh_h dummy_mesh = 0;
- texture_h dummy_texture = 0;
-
- render_op render_list[MAX_VISUALS];
- uint32 render_list_size = 0;
- //-----------------------------------------------------------------------------
-
- static const char meshes_dir[] = "..\\meshes";
- //-----------------------------------------------------------------------------
-
- visual_h visual_create( const char* mesh_name,
- const char* texture0_path,
- const char* texture1_path )
- {
- ASSERT(visuals_count < MAX_VISUALS);
-
- visual_h vis = &visuals[visuals_count];
-
- if( mesh_name )
- {
- vis->mesh = mesh_load(meshes_dir, mesh_name);
- if( !vis->mesh )
- {
- log_write("WARNING: unable to load mesh %s", mesh_name);
- vis->mesh = dummy_mesh;
- }
- vis->v_layout = renderer::get_vertex_layout( vis->mesh->header.vertex_type );
- }
-
- if( texture0_path )
- {
- byte* texture_data;
- uint32 texture_data_size;
- if( load_data(texture0_path, &texture_data, &texture_data_size) )
- {
- log_write("WARNING: unable to load texture %s", texture0_path);
- vis->texture[0] = dummy_texture;
- }
- else
- {
- vis->texture[0] = renderer::texture_create(texture_data, texture_data_size);
- if( !vis->texture[0] )
- {
- log_write("WARNING: unable to create texture %s", texture0_path);
- vis->texture[0] = dummy_texture;
- }
-
- free(texture_data);
- }
- }
-
- if( texture1_path )
- {
- byte* texture_data;
- uint32 texture_data_size;
- if( load_data(texture1_path, &texture_data, &texture_data_size) )
- {
- log_write("WARNING: unable to load texture %s", texture1_path);
- vis->texture[1] = dummy_texture;
- }
- else
- {
- vis->texture[1] = renderer::texture_create(texture_data, texture_data_size);
- if( !vis->texture[0] )
- {
- log_write("WARNING: unable to create texture %s", texture1_path);
- vis->texture[1] = dummy_texture;
- }
-
- free(texture_data);
- }
- }
-
- vis->material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
- vis->material.Ambient = vis->material.Diffuse = vis->material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- vis->material.Power = 1.0f;
-
- vis->scaleu = 1.0f;
-
- D3DXMatrixIdentity(&vis->transform);
-
- visuals_count++;
-
- return vis;
- }
- //-----------------------------------------------------------------------------
-
- visual_h visual_create( mesh_h mesh, texture_h tex0, texture_h tex1 )
- {
- ASSERT(visuals_count < MAX_VISUALS);
-
- visual_h vis = &visuals[visuals_count];
-
- vis->mesh = mesh;
- vis->v_layout = renderer::get_vertex_layout( mesh->header.vertex_type );
- vis->texture[0] = tex0;
- vis->texture[1] = tex1;
-
- vis->material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
- vis->material.Ambient = vis->material.Diffuse = vis->material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- vis->material.Power = 1.0f;
-
- vis->scaleu = 1.0f;
-
- D3DXMatrixIdentity(&vis->transform);
-
- visuals_count++;
-
- return vis;
- }
- //-----------------------------------------------------------------------------
-
- void visual_update_transform( visual_h vis )
- {
- ASSERT(vis);
-
- D3DXMATRIX translation, rotation, scale;
- D3DXMatrixTranslation(&translation, vis->position.x, vis->position.y, vis->position.z );
- D3DXMatrixRotationYawPitchRoll(&rotation, vis->yaw, vis->pitch, vis->roll);
- D3DXMatrixScaling(&scale, vis->scaleu, vis->scaleu, vis->scaleu);
- vis->transform = scale * rotation * translation;
- }
- //-----------------------------------------------------------------------------
-
- void visual_set_position( visual_h vis, const D3DXVECTOR3& pos )
- {
- ASSERT(vis);
-
- vis->position = pos;
- visual_update_transform(vis);
- }
- //-----------------------------------------------------------------------------
-
- void visual_set_rotation( visual_h vis, float pitch, float yaw, float roll )
- {
- ASSERT(vis);
-
- vis->pitch = pitch;
- vis->yaw = yaw;
- vis->roll = roll;
- visual_update_transform(vis);
- }
- //-----------------------------------------------------------------------------
-
- void visual_set_scaleu( visual_h vis, float scale )
- {
- ASSERT(vis);
-
- vis->scaleu = scale;
- visual_update_transform(vis);
- }
- //-----------------------------------------------------------------------------
-
- void visual_remove( visual_h vis )
- {
- ASSERT(vis);
-
- memset( vis, 0, sizeof(visual_t) );
- }
- //-----------------------------------------------------------------------------
-
- void visual_draw( visual_h vis )
- {
- ASSERT(vis);
-
- ASSERT(vis->mesh);
-
- render_op rop;
- rop.vb = vis->mesh->vb;
- rop.vertex_count = vis->mesh->header.vertex_count;
- rop.vertex_layout = vis->v_layout;
- rop.vertex_size = vis->mesh->header.vertex_size;
- rop.ib = vis->mesh->ib;
- rop.transformation = vis->transform;
-
- rop.material.stages[0].texture = vis->texture[0];
- rop.material.stages[1].texture = vis->texture[1];
- rop.material.material = vis->material;
-
- for( uint32 i=0; i<vis->mesh->header.subset_count; i++ )
- {
- rop.index_count = vis->mesh->subsets[i].index_count;
- rop.index_offset = vis->mesh->subsets[i].index_offset;
- rop.vertex_offset = vis->mesh->subsets[i].vertex_offset;
- renderer::render(rop);
- }
- }
- //-----------------------------------------------------------------------------
-
- void visual_render( visual_h vis )
- {
- ASSERT(vis);
-
- for( uint32 i=0; i<vis->mesh->header.subset_count; i++ )
- {
- render_op* rop = &render_list[render_list_size];
-
- rop->vb = vis->mesh->vb;
- rop->vertex_count = vis->mesh->header.vertex_count;
- rop->vertex_layout = vis->v_layout;
- rop->vertex_size = vis->mesh->header.vertex_size;
- rop->ib = vis->mesh->ib;
- rop->transformation = vis->transform;
-
- rop->material.stages[0].texture = vis->texture[0];
- rop->material.stages[1].texture = vis->texture[1];
- rop->material.material = vis->material;
-
- rop->index_count = vis->mesh->subsets[i].index_count;
- rop->index_offset = vis->mesh->subsets[i].index_offset;
- rop->vertex_offset = vis->mesh->subsets[i].vertex_offset;
-
- render_list_size++;
- }
- }
- //-----------------------------------------------------------------------------
-
- void visuals_render_all()
- {
- for( uint32 i=0; i<render_list_size; i++ )
- renderer::render( render_list[i] );
-
- render_list_size = 0;
- }
- //-----------------------------------------------------------------------------