windmill-gint/src/windmill_utility.cpp

241 lines
6.2 KiB
C++

//----------------------------------------------------------------------------------------------------
//
// WINDMILL
//
// version : 2.0
// Moteur de rendu 3D base sur la methode de rasterisation avec buffer de profondeur
//
// Cree par Olivier Lanneau, alias NineStars
// Planet-casio.fr
//
//----------------------------------------------------------------------------------------------------
#include "windmill.hpp"
void debug_pop(char const *fmt, ...);
extern Texture tex_white;
extern Texture tex_light;
extern Texture tex_dark;
extern Texture tex_black;
//----------------------------------------------------------------------------------------------------
// DIVERS
//----------------------------------------------------------------------------------------------------
/*void WMesh::from_mesh(const Mesh* mesh, int margin)
{
// Double allocated memory because 3D clipping could generate more vertices and copy mesh into working mesh
WMesh wmesh;
v_length = mesh->v_length;
v_length_allocated = mesh->v_length * margin;
v = (Vertex*) malloc(v_length_allocated * sizeof(Vertex));
for (int i = 0; i < mesh->v_length; i++)
{
//v[i].FromVertexPoint(mesh->v[i])
v[i].x = mesh->v[i].x;
v[i].y = mesh->v[i].y;
v[i].z = mesh->v[i].z;
}
t_length = mesh->t_length;
t_length_allocated = mesh->t_length * margin;
t = (TexturePoint*) malloc(t_length_allocated * sizeof(TexturePoint));
memcpy(t, mesh->t, mesh->t_length * sizeof(TexturePoint));
f_length = mesh->f_length;
f_length_allocated = mesh->f_length * margin;
f = (Face*) malloc(f_length_allocated * sizeof(Face));
memcpy(f, mesh->f, mesh->f_length * sizeof(Face));
return wmesh;
}*/
/*WMesh Windmill::create_working_mesh(const WMesh* wmesh)
{
WMesh output_wmesh;
output_v_length = 0;
output_v_length_allocated = wmesh->v_length_allocated;
output_v = (Vertex*) malloc(output_v_length_allocated * sizeof(Vertex));
output_t_length = 0;
output_t_length_allocated = wmesh->t_length_allocated;
output_t = (TexturePoint*) malloc(output_t_length_allocated * sizeof(TexturePoint));
output_f_length = 0;
output_f_length_allocated = wmesh->f_length_allocated;
output_f = (Face*) malloc(output_f_length_allocated * sizeof(Face));
return output_wmesh;
}*/
void Windmill::copy_camera()
{
memcpy(&camera, camera2, sizeof(Camera)); // a supprimer cf remarque lephe
}
void Windmill::sort_object()
{
qsort(map->object, map->object_length, sizeof(Object*), compare_object);
}
int compare_object(void const *a, void const *b)
{
Object* object_a = *(Object **) a;
Object* object_b = *(Object **) b;
return (object_a->position.z - object_b->position.z);
}
void Windmill::clear_z_buffer()
{
memset(z_buffer, MAX_DEPTH_Z_BUFFER, z_buffer_size * sizeof(short));
}
float Windmill::distance(float dx, float dy, float dz)
{
return sqrtf(dx*dx + dy*dy + dz*dz);
}
//----------------------------------------------------------------------------------------------------
// DEBUG
//----------------------------------------------------------------------------------------------------
void Windmill::show_coordinates()
{
// coordonnees
float tab_coordinates[5] = {camera.x, camera.y, camera.z, to_deg(camera.yaw), to_deg(camera.pitch)};
for (int i = 0; i < 5; i++)
{
dprint_opt(1, 1+8*i, C_BLACK, C_WHITE, 0, 0, "%i", (int)(tab_coordinates[i]));
}
}
void Windmill::show_repere()
{
// repere
int repere_x = 112;
int repere_y = 55;
int repere_size = -12;
// repere
Vertex v[3];
const char* letter[] = {"x", "y", "z"};
v[0].set_xyz(repere_size, 0 , 0 );
v[1].set_xyz(0 , repere_size, 0 );
v[2].set_xyz(0 , 0 , repere_size);
for (int i = 0; i < 3; i++)
{
float x3D = (camera.a4 * v[i].x + camera.a5 * v[i].y + camera.a6 * v[i].z) / 128;
float y3D = (camera.a7 * v[i].x + camera.a8 * v[i].y + camera.a9 * v[i].z) / 128;
//float z3D = (-(camera.a1 * v[i].x + camera.a2 * v[i].y + camera.a3 * v[i].z)) / 128;
float x2D = x3D;
float y2D = y3D;
//int color = (z3D < 0 ? C_BLACK : C_LIGHT);
dline(repere_x, repere_y, x2D + repere_x, y2D + repere_y, C_BLACK);
dtext(x2D + repere_x, y2D + repere_y-2, C_BLACK, letter[i]);
}
}
//----------------------------------------------------------------------------------------------------
// VERTEX
//----------------------------------------------------------------------------------------------------
Vertex::Vertex(){}
Vertex::Vertex(int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
Vertex::Vertex(int _x, int _y, int _z, float _u, float _v)
{
x = _x;
y = _y;
z = _z;
u = _u;
v = _v;
}
void Vertex::set_texture(TexturePoint t)
{
u = t.u;
v = t.v;
}
void Vertex::set_xyz(int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
//----------------------------------------------------------------------------------------------------
// WMESH
//----------------------------------------------------------------------------------------------------
void WMesh::from_mesh(const Mesh* mesh, int margin)
{
// Double allocated memory because 3D clipping could generate more vertices and copy mesh into working mesh
v_length = mesh->v_length;
v_length_allocated = mesh->v_length * margin;
v = (Vertex*) malloc(v_length_allocated * sizeof(Vertex));
for (int i = 0; i < mesh->v_length; i++)
{
//v[i].FromVertexPoint(mesh->v[i])
v[i].x = mesh->v[i].x;
v[i].y = mesh->v[i].y;
v[i].z = mesh->v[i].z;
}
t_length = mesh->t_length;
t_length_allocated = mesh->t_length * margin;
t = (TexturePoint*) malloc(t_length_allocated * sizeof(TexturePoint));
memcpy(t, mesh->t, mesh->t_length * sizeof(TexturePoint));
f_length = mesh->f_length;
f_length_allocated = mesh->f_length * margin;
f = (Face*) malloc(f_length_allocated * sizeof(Face));
memcpy(f, mesh->f, mesh->f_length * sizeof(Face));
}
void WMesh::free_memory()
{
free(v);
free(t);
free(f);
}
void WMesh::add_vertex(Vertex vertex)
{
v[v_length].x = vertex.x;
v[v_length].y = vertex.y;
v[v_length].z = vertex.z;
t[t_length].u = vertex.u;
t[t_length].v = vertex.v;
if (v_length < v_length_allocated) v_length++;
if (t_length < t_length_allocated) t_length++;
}