Skip to content

Instantly share code, notes, and snippets.

@tobspr
Created August 27, 2013 10:37
Show Gist options
  • Save tobspr/6352004 to your computer and use it in GitHub Desktop.
Save tobspr/6352004 to your computer and use it in GitHub Desktop.
// This function looks up in the vertice table wheter a vertice at that position already exists
// - If yes, return the vertice index
// - If no, create a new vertice and return the created vertice index
//
// @param cfg: Pointer to a chunkConfig struct
// @param w_vertex: Pointer to the vertex writer
// @param w_texcoord: Pointer to the texcoord writer
// @param x: x position of the vertice
// @param y: y position of the vertice
inline VERTICE_INDEX GetOrCreateVertice(float x, float y, ChunkGenerator::chunkConfig *cfg)
{
int x_pos = int((x*POSARR_ADJ_F) + 0.5f);
int y_pos = int((y*POSARR_ADJ_F) + 0.5f);
int dict_index = x_pos + (y_pos * (cfg->subdivisions * POSARR_ADJ));
VERTICE_INDEX dict_entry = cfg->vertice_pool.vertice_dict[dict_index];
VERTICE_INDEX current_index = cfg->vertice_pool.current_vertice_index;
if (dict_entry >= 0 && dict_entry < 65535 && current_index > 0)
return dict_entry;
LVecBase3f* offset = cfg->base_position;
LVecBase2f* dim = cfg->dimensions;
LVecBase2f* tex_offset = cfg->texture_offset;
LVecBase2f* tex_scale = cfg->texture_scale;
int pool_index = ((int)current_index) * 5;
// instead of calling add_data3f we store all data in array and cast it later to binary
// Format: v.x, v.y, v.z, t.x, t.y
float base_scale = 1.0 / (cfg->subdivisions_f-1.0);
//float base_scale = 1.0;
cfg->vertice_pool.vertice_array[pool_index+0] = offset->get_x() + (x * base_scale * dim->get_x());
cfg->vertice_pool.vertice_array[pool_index+1] = offset->get_y() + (y * base_scale * dim->get_y());
cfg->vertice_pool.vertice_array[pool_index+2] = offset->get_z();
cfg->vertice_pool.vertice_array[pool_index+3] = tex_offset->get_x() + (x * tex_scale->get_x() * base_scale);
cfg->vertice_pool.vertice_array[pool_index+4] = tex_offset->get_y() + (y * tex_scale->get_y() * base_scale);
//printf("Vertice(%f, %f, index=%i)\n", x,y,current_index);
cfg->vertice_pool.vertice_dict[dict_index] = current_index;
cfg->vertice_pool.current_vertice_index++;
return current_index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment