glide_test1

diff mesh.cpp @ 0:f3ddb2bb7024

first 3dfx glide test, initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Mar 2014 06:27:58 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mesh.cpp	Sun Mar 09 06:27:58 2014 +0200
     1.3 @@ -0,0 +1,66 @@
     1.4 +#define _USE_MATH_DEFINES
     1.5 +#include <math.h>
     1.6 +#include "mesh.h"
     1.7 +
     1.8 +#ifndef M_PI
     1.9 +#define M_PI	3.14159265359
    1.10 +#endif
    1.11 +
    1.12 +void gen_sphere(std::vector<Vertex> &varr, float rad, int usub, int vsub)
    1.13 +{
    1.14 +}
    1.15 +
    1.16 +static void torus_vertex(Vertex *vert, float u, float v, float rad, float tube_rad)
    1.17 +{
    1.18 +	float theta = 2.0 * M_PI * u;
    1.19 +	float phi = 2.0 * M_PI * v;
    1.20 +
    1.21 +	float x = 0.0f;
    1.22 +	float y = sin(phi) * tube_rad;
    1.23 +	float z = cos(phi) * tube_rad + rad;
    1.24 +
    1.25 +	vert->pos.x = z * sin(theta);
    1.26 +	vert->pos.y = y;
    1.27 +	vert->pos.z = -x * sin(theta) + z * cos(theta);
    1.28 +	vert->pos.w = 1.0;
    1.29 +
    1.30 +	Vector3 cent;
    1.31 +	cent.x = sin(theta) * rad;
    1.32 +	cent.z = cos(theta) * rad;
    1.33 +
    1.34 +	vert->normal = normalize(Vector3(vert->pos) - cent);
    1.35 +	vert->color = Vector4(1.0, 1.0, 1.0, 1.0);
    1.36 +}
    1.37 +
    1.38 +void gen_torus(std::vector<Vertex> &varr, float rad, float tube_rad, int usub, int vsub)
    1.39 +{
    1.40 +	int i, j;
    1.41 +
    1.42 +	if(usub < 3) usub = 3;
    1.43 +	if(vsub < 3) vsub = 3;
    1.44 +
    1.45 +	float du = 1.0f / (float)usub;
    1.46 +	float dv = 1.0f / (float)vsub;
    1.47 +
    1.48 +	
    1.49 +	for(i=0; i<usub; i++) {
    1.50 +		float u = (float)i / (float)usub;
    1.51 +		for(j=0; j<vsub; j++) {
    1.52 +			float v = (float)j / (float)vsub;
    1.53 +			Vertex vert[4];
    1.54 +
    1.55 +			torus_vertex(vert, u, v, rad, tube_rad);
    1.56 +			torus_vertex(vert + 1, u, v + dv, rad, tube_rad);
    1.57 +			torus_vertex(vert + 2, u + du, v + dv, rad, tube_rad);
    1.58 +			torus_vertex(vert + 3, u + du, v, rad, tube_rad);
    1.59 +
    1.60 +			varr.push_back(vert[0]);
    1.61 +			varr.push_back(vert[1]);
    1.62 +			varr.push_back(vert[2]);
    1.63 +			varr.push_back(vert[0]);
    1.64 +			varr.push_back(vert[2]);
    1.65 +			varr.push_back(vert[3]);
    1.66 +
    1.67 +		}
    1.68 +	}
    1.69 +}
    1.70 \ No newline at end of file