Allow to draw frame and ball, small refactor
This commit is contained in:
parent
f83b778323
commit
b01e00497d
143
src/gl_draw.c
143
src/gl_draw.c
@ -57,6 +57,10 @@
|
|||||||
#define NEAR_CLIPPING_PLANE 1.0f
|
#define NEAR_CLIPPING_PLANE 1.0f
|
||||||
#define CAMERA_ANGLE 60.0f
|
#define CAMERA_ANGLE 60.0f
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* globals
|
||||||
|
*/
|
||||||
int year;
|
int year;
|
||||||
int yearabs = 365;
|
int yearabs = 365;
|
||||||
int day;
|
int day;
|
||||||
@ -64,11 +68,18 @@ int dayabs = 30;
|
|||||||
HE_obj *obj;
|
HE_obj *obj;
|
||||||
bool show_normals = false;
|
bool show_normals = false;
|
||||||
bool shademodel = true;
|
bool shademodel = true;
|
||||||
|
bool draw_frame = false;
|
||||||
|
float ball_speed = 1.0f;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* static function declaration
|
* static function declaration
|
||||||
*/
|
*/
|
||||||
static void draw_bez(HE_obj const * const obj, float step_factor_inc);
|
static void draw_bez(const bez_curv *bez, float step_factor_inc);
|
||||||
|
static void draw_bez_frame(const bez_curv *bez,
|
||||||
|
float pos);
|
||||||
|
static void draw_ball(const bez_curv *bez,
|
||||||
|
const float pos);
|
||||||
static void draw_obj(int32_t const myxrot,
|
static void draw_obj(int32_t const myxrot,
|
||||||
int32_t const myyrot,
|
int32_t const myyrot,
|
||||||
int32_t const myzrot,
|
int32_t const myzrot,
|
||||||
@ -192,9 +203,8 @@ static void draw_vertices(HE_obj const * const obj,
|
|||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
static void draw_bez(const bez_curv *bez, float step_factor_inc)
|
||||||
{
|
{
|
||||||
uint32_t i = 0;
|
|
||||||
static float line_width = 2;
|
static float line_width = 2;
|
||||||
static float point_size = 10;
|
static float point_size = 10;
|
||||||
static float step_factor = 0.1;
|
static float step_factor = 0.1;
|
||||||
@ -209,7 +219,6 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
glPointSize(point_size);
|
glPointSize(point_size);
|
||||||
glColor3f(1.0, 0.0, 0.0);
|
glColor3f(1.0, 0.0, 0.0);
|
||||||
|
|
||||||
while (i < obj->bzc) { /* for all bezier curves */
|
|
||||||
vector *v1 = NULL,
|
vector *v1 = NULL,
|
||||||
*v2 = NULL;
|
*v2 = NULL;
|
||||||
|
|
||||||
@ -217,10 +226,10 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
* draw frame
|
* draw frame
|
||||||
*/
|
*/
|
||||||
glBegin(GL_LINE_STRIP);
|
glBegin(GL_LINE_STRIP);
|
||||||
for (uint32_t j = 0; j <= obj->bez_curves[i].deg; j++) {
|
for (uint32_t j = 0; j <= bez->deg; j++) {
|
||||||
glVertex3f(obj->bez_curves[i].vec[j].x,
|
glVertex3f(bez->vec[j].x,
|
||||||
obj->bez_curves[i].vec[j].y,
|
bez->vec[j].y,
|
||||||
obj->bez_curves[i].vec[j].z);
|
bez->vec[j].z);
|
||||||
}
|
}
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
@ -228,10 +237,10 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
* draw control points
|
* draw control points
|
||||||
*/
|
*/
|
||||||
glBegin(GL_POINTS);
|
glBegin(GL_POINTS);
|
||||||
for (uint32_t j = 0; j <= obj->bez_curves[i].deg; j++) {
|
for (uint32_t j = 0; j <= bez->deg; j++) {
|
||||||
glVertex3f(obj->bez_curves[i].vec[j].x,
|
glVertex3f(bez->vec[j].x,
|
||||||
obj->bez_curves[i].vec[j].y,
|
bez->vec[j].y,
|
||||||
obj->bez_curves[i].vec[j].z);
|
bez->vec[j].z);
|
||||||
}
|
}
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
@ -240,10 +249,10 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
/*
|
/*
|
||||||
* line segments: first line
|
* line segments: first line
|
||||||
*/
|
*/
|
||||||
v1 = calculate_bezier_point(&(obj->bez_curves[i]), step_factor);
|
v1 = calculate_bezier_point(bez, step_factor);
|
||||||
glVertex3f(obj->bez_curves[i].vec[0].x,
|
glVertex3f(bez->vec[0].x,
|
||||||
obj->bez_curves[i].vec[0].y,
|
bez->vec[0].y,
|
||||||
obj->bez_curves[i].vec[0].z);
|
bez->vec[0].z);
|
||||||
glVertex3f(v1->x,
|
glVertex3f(v1->x,
|
||||||
v1->y,
|
v1->y,
|
||||||
v1->z);
|
v1->z);
|
||||||
@ -252,8 +261,8 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
free(v1);
|
free(v1);
|
||||||
free(v2);
|
free(v2);
|
||||||
|
|
||||||
v1 = calculate_bezier_point(&(obj->bez_curves[i]), k);
|
v1 = calculate_bezier_point(bez, k);
|
||||||
v2 = calculate_bezier_point(&(obj->bez_curves[i]), k +
|
v2 = calculate_bezier_point(bez, k +
|
||||||
step_factor);
|
step_factor);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -274,21 +283,34 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
glVertex3f(v2->x,
|
glVertex3f(v2->x,
|
||||||
v2->y,
|
v2->y,
|
||||||
v2->z);
|
v2->z);
|
||||||
glVertex3f(obj->bez_curves[i].vec[obj->bez_curves[i].deg].x,
|
glVertex3f(bez->vec[bez->deg].x,
|
||||||
obj->bez_curves[i].vec[obj->bez_curves[i].deg].y,
|
bez->vec[bez->deg].y,
|
||||||
obj->bez_curves[i].vec[obj->bez_curves[i].deg].z);
|
bez->vec[bez->deg].z);
|
||||||
|
|
||||||
free(v1);
|
free(v1);
|
||||||
free(v2);
|
free(v2);
|
||||||
|
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
{
|
|
||||||
const float ball_pos = 0.2;
|
glPopMatrix();
|
||||||
bez_curv cur_bez = obj->bez_curves[i];
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the bezier frame of the given bezier curve
|
||||||
|
* which will cut the curve at the given position.
|
||||||
|
*
|
||||||
|
* @param bez the bezier curve to draw the frame of
|
||||||
|
* @param pos the position where the curve and the frame
|
||||||
|
* will cut
|
||||||
|
*/
|
||||||
|
static void draw_bez_frame(const bez_curv *bez,
|
||||||
|
float pos)
|
||||||
|
{
|
||||||
|
bez_curv cur_bez = *bez;
|
||||||
bez_curv next_bez = { NULL, 0 };
|
bez_curv next_bez = { NULL, 0 };
|
||||||
|
|
||||||
while ((get_reduced_bez_curv(&cur_bez, &next_bez, ball_pos))) {
|
while ((get_reduced_bez_curv(&cur_bez, &next_bez, pos))) {
|
||||||
|
|
||||||
glBegin(GL_LINES);
|
glBegin(GL_LINES);
|
||||||
|
|
||||||
@ -302,19 +324,32 @@ static void draw_bez(HE_obj const * const obj, float step_factor_inc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* don't free the original one! */
|
/* don't free the original one! */
|
||||||
if (cur_bez.deg < obj->bez_curves[i].deg)
|
if (cur_bez.deg < bez->deg)
|
||||||
free(cur_bez.vec);
|
free(cur_bez.vec);
|
||||||
cur_bez = next_bez;
|
cur_bez = next_bez;
|
||||||
|
|
||||||
glEnd();
|
glEnd();
|
||||||
}
|
}
|
||||||
free(cur_bez.vec);
|
free(cur_bez.vec);
|
||||||
}
|
|
||||||
|
|
||||||
i++;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws a ball on the bezier curve at the given position.
|
||||||
|
*
|
||||||
|
* @param bez the bezier curve to draw the ball on
|
||||||
|
* @param pos the position of the ball
|
||||||
|
*/
|
||||||
|
static void draw_ball(const bez_curv *bez,
|
||||||
|
const float pos)
|
||||||
|
{
|
||||||
|
const float ball_pos = pos;
|
||||||
|
|
||||||
|
glPushMatrix();
|
||||||
|
vector *point = calculate_bezier_point(bez,
|
||||||
|
ball_pos);
|
||||||
|
glTranslatef(point->x, point->y, point->z);
|
||||||
|
glutWireSphere(0.02f, 100, 100);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,6 +370,18 @@ static void draw_obj(int32_t const myxrot,
|
|||||||
static int32_t xrot = 0,
|
static int32_t xrot = 0,
|
||||||
yrot = 0,
|
yrot = 0,
|
||||||
zrot = 0;
|
zrot = 0;
|
||||||
|
static float ball_inc = 0;
|
||||||
|
static bool ball_to_right = true;
|
||||||
|
|
||||||
|
if (ball_inc > 0.98)
|
||||||
|
ball_to_right = false;
|
||||||
|
else if (ball_inc < 0.02)
|
||||||
|
ball_to_right = true;
|
||||||
|
|
||||||
|
if (ball_to_right)
|
||||||
|
ball_inc += 0.01f * ball_speed;
|
||||||
|
else
|
||||||
|
ball_inc -= 0.01f * ball_speed;
|
||||||
|
|
||||||
vector center_vert;
|
vector center_vert;
|
||||||
|
|
||||||
@ -370,8 +417,12 @@ static void draw_obj(int32_t const myxrot,
|
|||||||
draw_vertices(obj, false);
|
draw_vertices(obj, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj->bzc != 0)
|
if (obj->bzc != 0) {
|
||||||
draw_bez(obj, bez_inc);
|
draw_bez(&(obj->bez_curves[0]), bez_inc);
|
||||||
|
draw_ball(&(obj->bez_curves[0]), ball_inc);
|
||||||
|
if(draw_frame)
|
||||||
|
draw_bez_frame(&(obj->bez_curves[0]), ball_inc);
|
||||||
|
}
|
||||||
|
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
@ -684,13 +735,13 @@ void animate()
|
|||||||
void keyboard(unsigned char key, int x, int y)
|
void keyboard(unsigned char key, int x, int y)
|
||||||
{
|
{
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'b':
|
/* case 'b': */
|
||||||
if (glIsEnabled(GL_CULL_FACE))
|
/* if (glIsEnabled(GL_CULL_FACE)) */
|
||||||
glDisable(GL_CULL_FACE);
|
/* glDisable(GL_CULL_FACE); */
|
||||||
else
|
/* else */
|
||||||
glEnable(GL_CULL_FACE);
|
/* glEnable(GL_CULL_FACE); */
|
||||||
glutPostRedisplay();
|
/* glutPostRedisplay(); */
|
||||||
break;
|
/* break; */
|
||||||
case 't':
|
case 't':
|
||||||
dayabs += 15;
|
dayabs += 15;
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
@ -745,14 +796,26 @@ void keyboard(unsigned char key, int x, int y)
|
|||||||
}
|
}
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
case 'k':
|
case 'b':
|
||||||
draw_obj(0, 0, 0, 0.02);
|
draw_obj(0, 0, 0, 0.02);
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
case 'K':
|
case 'B':
|
||||||
draw_obj(0, 0, 0, -0.02);
|
draw_obj(0, 0, 0, -0.02);
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
|
case 'k':
|
||||||
|
ball_speed += 0.2f;
|
||||||
|
glutPostRedisplay();
|
||||||
|
break;
|
||||||
|
case 'K':
|
||||||
|
ball_speed -= 0.2f;
|
||||||
|
glutPostRedisplay();
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
draw_frame = !draw_frame;
|
||||||
|
glutPostRedisplay();
|
||||||
|
break;
|
||||||
|
|
||||||
/* case 'k': */
|
/* case 'k': */
|
||||||
/* draw_normals(obj, 0.01f); */
|
/* draw_normals(obj, 0.01f); */
|
||||||
|
Loading…
Reference in New Issue
Block a user