PageRenderTime 53ms CodeModel.GetById 2ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/src/renderer/visual.cpp

https://bitbucket.org/vivkin/gam3b00bs/
C++ | 236 lines | 175 code | 47 blank | 14 comment | 11 complexity | 7afde4d616d7bf4267102cb6292b1c0b MD5 | raw file
  1#include "renderer/visual.h"
  2#include "common.h"
  3#include "log.h"
  4#include "load_data.h"
  5//-----------------------------------------------------------------------------
  6
  7#define MAX_VISUALS 1024
  8//-----------------------------------------------------------------------------
  9
 10visual_t	visuals[MAX_VISUALS] = {0};
 11uint32		visuals_count = 0;
 12mesh_h		dummy_mesh = 0;
 13texture_h	dummy_texture = 0;
 14
 15render_op	render_list[MAX_VISUALS];
 16uint32		render_list_size = 0;
 17//-----------------------------------------------------------------------------
 18
 19static const char meshes_dir[] = "..\\meshes";
 20//-----------------------------------------------------------------------------
 21
 22visual_h visual_create( const char* mesh_name, 
 23						const char* texture0_path, 
 24						const char* texture1_path )
 25{
 26	ASSERT(visuals_count < MAX_VISUALS);
 27
 28	visual_h vis = &visuals[visuals_count];
 29
 30	if( mesh_name )
 31	{
 32		vis->mesh = mesh_load(meshes_dir, mesh_name);
 33		if( !vis->mesh )
 34		{
 35			log_write("WARNING: unable to load mesh %s", mesh_name);
 36			vis->mesh = dummy_mesh;
 37		}
 38		vis->v_layout = renderer::get_vertex_layout( vis->mesh->header.vertex_type );
 39	}
 40
 41	if( texture0_path )
 42	{
 43		byte* texture_data;
 44		uint32 texture_data_size;
 45		if( load_data(texture0_path, &texture_data, &texture_data_size) )
 46		{
 47			log_write("WARNING: unable to load texture %s", texture0_path);
 48			vis->texture[0] = dummy_texture;
 49		}
 50		else
 51		{
 52			vis->texture[0] = renderer::texture_create(texture_data, texture_data_size);
 53			if( !vis->texture[0] )
 54			{
 55				log_write("WARNING: unable to create texture %s", texture0_path);
 56				vis->texture[0] = dummy_texture;
 57			}
 58
 59			free(texture_data);
 60		}
 61	}
 62
 63	if( texture1_path )
 64	{
 65		byte* texture_data;
 66		uint32 texture_data_size;
 67		if( load_data(texture1_path, &texture_data, &texture_data_size) )
 68		{
 69			log_write("WARNING: unable to load texture %s", texture1_path);
 70			vis->texture[1] = dummy_texture;
 71		}
 72		else
 73		{
 74			vis->texture[1] = renderer::texture_create(texture_data, texture_data_size);
 75			if( !vis->texture[0] )
 76			{
 77				log_write("WARNING: unable to create texture %s", texture1_path);
 78				vis->texture[1] = dummy_texture;
 79			}
 80
 81			free(texture_data);
 82		}
 83	}
 84
 85	vis->material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
 86	vis->material.Ambient = vis->material.Diffuse = vis->material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
 87	vis->material.Power = 1.0f;
 88
 89	vis->scaleu = 1.0f;
 90
 91	D3DXMatrixIdentity(&vis->transform);
 92
 93	visuals_count++;
 94
 95	return vis;
 96}
 97//-----------------------------------------------------------------------------
 98
 99visual_h visual_create( mesh_h mesh, texture_h tex0, texture_h tex1 )
100{
101	ASSERT(visuals_count < MAX_VISUALS);
102
103	visual_h vis = &visuals[visuals_count];
104
105	vis->mesh = mesh;
106	vis->v_layout = renderer::get_vertex_layout( mesh->header.vertex_type );
107	vis->texture[0] = tex0;
108	vis->texture[1] = tex1;
109
110	vis->material.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f);
111	vis->material.Ambient = vis->material.Diffuse = vis->material.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
112	vis->material.Power = 1.0f;
113
114	vis->scaleu = 1.0f;
115
116	D3DXMatrixIdentity(&vis->transform);
117
118	visuals_count++;
119
120	return vis;
121}
122//-----------------------------------------------------------------------------
123
124void visual_update_transform( visual_h vis )
125{
126	ASSERT(vis);
127
128	D3DXMATRIX translation, rotation, scale;
129	D3DXMatrixTranslation(&translation, vis->position.x, vis->position.y, vis->position.z );
130	D3DXMatrixRotationYawPitchRoll(&rotation, vis->yaw, vis->pitch, vis->roll);
131	D3DXMatrixScaling(&scale, vis->scaleu, vis->scaleu, vis->scaleu);
132	vis->transform = scale * rotation * translation;
133}
134//-----------------------------------------------------------------------------
135
136void visual_set_position( visual_h vis, const D3DXVECTOR3& pos )
137{
138	ASSERT(vis);
139
140	vis->position = pos;
141	visual_update_transform(vis);
142}
143//-----------------------------------------------------------------------------
144
145void visual_set_rotation( visual_h vis, float pitch, float yaw, float roll )
146{
147	ASSERT(vis);
148
149	vis->pitch = pitch;
150	vis->yaw = yaw;
151	vis->roll = roll;
152	visual_update_transform(vis);
153}
154//-----------------------------------------------------------------------------
155
156void visual_set_scaleu( visual_h vis, float scale )
157{
158	ASSERT(vis);
159
160	vis->scaleu = scale;
161	visual_update_transform(vis);
162}
163//-----------------------------------------------------------------------------
164
165void visual_remove( visual_h vis )
166{
167	ASSERT(vis);
168
169	memset( vis, 0, sizeof(visual_t) );
170}
171//-----------------------------------------------------------------------------
172
173void visual_draw( visual_h vis )
174{
175	ASSERT(vis);
176
177	ASSERT(vis->mesh);
178	
179	render_op rop;
180	rop.vb = vis->mesh->vb;
181	rop.vertex_count = vis->mesh->header.vertex_count;
182	rop.vertex_layout = vis->v_layout;
183	rop.vertex_size = vis->mesh->header.vertex_size;
184	rop.ib = vis->mesh->ib;
185	rop.transformation = vis->transform;
186
187	rop.material.stages[0].texture = vis->texture[0];
188	rop.material.stages[1].texture = vis->texture[1];
189	rop.material.material = vis->material;
190
191	for( uint32 i=0; i<vis->mesh->header.subset_count; i++ )
192	{
193		rop.index_count = vis->mesh->subsets[i].index_count;
194		rop.index_offset = vis->mesh->subsets[i].index_offset;
195		rop.vertex_offset = vis->mesh->subsets[i].vertex_offset;
196		renderer::render(rop);
197	}
198}
199//-----------------------------------------------------------------------------
200
201void visual_render( visual_h vis )
202{
203	ASSERT(vis);
204
205	for( uint32 i=0; i<vis->mesh->header.subset_count; i++ )
206	{
207		render_op* rop = &render_list[render_list_size];
208
209		rop->vb = vis->mesh->vb;
210		rop->vertex_count = vis->mesh->header.vertex_count;
211		rop->vertex_layout = vis->v_layout;
212		rop->vertex_size = vis->mesh->header.vertex_size;
213		rop->ib = vis->mesh->ib;
214		rop->transformation = vis->transform;
215
216		rop->material.stages[0].texture = vis->texture[0];
217		rop->material.stages[1].texture = vis->texture[1];
218		rop->material.material = vis->material;
219
220		rop->index_count = vis->mesh->subsets[i].index_count;
221		rop->index_offset = vis->mesh->subsets[i].index_offset;
222		rop->vertex_offset = vis->mesh->subsets[i].vertex_offset;
223
224		render_list_size++;
225	}
226}
227//-----------------------------------------------------------------------------
228
229void visuals_render_all()
230{
231	for( uint32 i=0; i<render_list_size; i++ )
232		renderer::render( render_list[i] );
233
234	render_list_size = 0;
235}
236//-----------------------------------------------------------------------------