2014-05-08 20:46:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011-2014 hasufell
|
|
|
|
*
|
|
|
|
* This file is part of a hasufell project.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation version 2 of the License only.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-10 17:33:54 +00:00
|
|
|
/**
|
|
|
|
* @file gl_draw.c
|
|
|
|
* This file does the actual OpenGL and GLUT logic,
|
|
|
|
* drawing the objects, handling keyboard input and
|
|
|
|
* various GLUT-related callback functions.
|
|
|
|
* @brief OpenGL drawing
|
|
|
|
*/
|
|
|
|
|
2014-06-01 13:12:37 +00:00
|
|
|
#include "bezier.h"
|
2014-05-09 12:27:58 +00:00
|
|
|
#include "err.h"
|
|
|
|
#include "filereader.h"
|
2014-05-17 15:24:29 +00:00
|
|
|
#include "gl_draw.h"
|
2014-05-10 17:16:13 +00:00
|
|
|
#include "half_edge.h"
|
2014-06-01 00:13:45 +00:00
|
|
|
#include "print.h"
|
2014-05-08 20:45:16 +00:00
|
|
|
|
|
|
|
#include <GL/glut.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
|
2014-06-06 20:16:33 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2014-05-08 20:45:16 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-06-06 20:16:33 +00:00
|
|
|
#include <stdbool.h>
|
2014-05-08 20:45:16 +00:00
|
|
|
#include <stdio.h>
|
2014-05-15 22:32:38 +00:00
|
|
|
#include <string.h>
|
2014-05-08 20:45:16 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* globals
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
int year = 0;
|
2014-05-08 20:45:16 +00:00
|
|
|
int yearabs = 365;
|
2014-06-06 20:16:33 +00:00
|
|
|
int day = 0;
|
2014-05-08 20:45:16 +00:00
|
|
|
int dayabs = 30;
|
|
|
|
HE_obj *obj;
|
2014-05-12 18:20:03 +00:00
|
|
|
bool show_normals = false;
|
2014-05-15 16:33:43 +00:00
|
|
|
bool shademodel = true;
|
2014-06-01 17:12:07 +00:00
|
|
|
bool draw_frame = false;
|
|
|
|
float ball_speed = 1.0f;
|
|
|
|
|
2014-05-08 20:45:16 +00:00
|
|
|
|
2014-05-09 15:53:02 +00:00
|
|
|
|
2014-05-17 01:54:07 +00:00
|
|
|
/**
|
|
|
|
* Draws the vertex normals of the object.
|
|
|
|
*
|
|
|
|
* @param obj the object to draw the vertex normals of
|
|
|
|
* @param scale_inc the incrementor for scaling the normals
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_normals(HE_obj const * const obj,
|
2014-05-15 16:33:43 +00:00
|
|
|
float const scale_inc)
|
2014-05-12 18:20:03 +00:00
|
|
|
{
|
2014-05-17 01:32:14 +00:00
|
|
|
static float normals_scale_factor = 0.1f;
|
2014-05-15 16:33:43 +00:00
|
|
|
static float line_width = 2;
|
2014-05-12 18:20:03 +00:00
|
|
|
vector vec;
|
|
|
|
|
2014-05-15 16:33:43 +00:00
|
|
|
normals_scale_factor += scale_inc;
|
|
|
|
|
|
|
|
glPushMatrix();
|
2014-05-13 11:47:13 +00:00
|
|
|
|
2014-05-15 16:33:43 +00:00
|
|
|
glLineWidth(line_width);
|
|
|
|
glColor3f(1.0, 0.0, 0.0);
|
2014-05-13 11:47:13 +00:00
|
|
|
|
2014-05-15 16:33:43 +00:00
|
|
|
glBegin(GL_LINES);
|
|
|
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
2014-05-17 12:55:46 +00:00
|
|
|
/* be fault tolerant here, so we don't just
|
|
|
|
* kill the whole thing, because the normals failed to draw */
|
|
|
|
if (!vec_normal(&(obj->vertices[i]), &vec))
|
|
|
|
break;
|
2014-05-13 11:47:13 +00:00
|
|
|
|
|
|
|
glVertex3f(obj->vertices[i].vec->x,
|
|
|
|
obj->vertices[i].vec->y,
|
|
|
|
obj->vertices[i].vec->z);
|
2014-05-15 16:33:43 +00:00
|
|
|
glVertex3f(obj->vertices[i].vec->x + (vec.x * normals_scale_factor),
|
|
|
|
obj->vertices[i].vec->y + (vec.y * normals_scale_factor),
|
|
|
|
obj->vertices[i].vec->z + (vec.z * normals_scale_factor));
|
2014-05-12 18:20:03 +00:00
|
|
|
}
|
2014-05-15 16:33:43 +00:00
|
|
|
glEnd();
|
|
|
|
glPopMatrix();
|
2014-05-12 18:20:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 13:03:46 +00:00
|
|
|
/**
|
2014-05-17 01:54:07 +00:00
|
|
|
* Draws all vertices of the object by
|
|
|
|
* assembling a polygon for each face.
|
2014-05-09 13:03:46 +00:00
|
|
|
*
|
|
|
|
* @param obj the object of which we will draw the vertices
|
2014-05-15 16:33:43 +00:00
|
|
|
* @param disco_set determines whether we are in disco mode
|
2014-05-09 13:03:46 +00:00
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_vertices(HE_obj const * const obj,
|
2014-05-15 16:33:43 +00:00
|
|
|
bool disco_set)
|
2014-05-09 12:54:47 +00:00
|
|
|
{
|
2014-05-15 16:33:43 +00:00
|
|
|
/* color */
|
|
|
|
static float red = 90,
|
|
|
|
blue = 90,
|
|
|
|
green = 90;
|
|
|
|
static bool disco = false;
|
|
|
|
|
|
|
|
if (disco_set)
|
|
|
|
disco = !disco;
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
|
2014-05-09 12:54:47 +00:00
|
|
|
for (uint32_t i = 0; i < obj->fc; i++) { /* for all faces */
|
|
|
|
HE_edge *tmp_edge = obj->faces[i].edge;
|
|
|
|
|
2014-05-15 16:33:43 +00:00
|
|
|
/* add random values */
|
|
|
|
red += rand() / (RAND_MAX / ((rand() % 11) / 10.0f));
|
|
|
|
blue += rand() / (RAND_MAX / ((rand() % 11) / 10.0f));
|
|
|
|
green += rand() / (RAND_MAX / ((rand() % 11) / 10.0f));
|
|
|
|
|
|
|
|
/* values above 180 will cause negative color values */
|
|
|
|
red = fmodf(red, 181.0f);
|
|
|
|
blue = fmodf(blue, 181.0f);
|
|
|
|
green = fmodf(green, 181.0f);
|
|
|
|
|
|
|
|
if (disco) {
|
|
|
|
tmp_edge->vert->col->red =
|
|
|
|
(sin(red * i * (M_PI / 180)) / 2) + 0.5;
|
|
|
|
tmp_edge->vert->col->green =
|
|
|
|
(sin(green * i * (M_PI / 180)) / 2) + 0.5;
|
|
|
|
tmp_edge->vert->col->blue =
|
|
|
|
(sin(blue * i * (M_PI / 180)) / 2) + 0.5;
|
|
|
|
} else {
|
|
|
|
if (tmp_edge->vert->col->red == -1)
|
|
|
|
tmp_edge->vert->col->red =
|
|
|
|
(sin(red * i * (M_PI / 180)) / 2) + 0.5;
|
|
|
|
|
|
|
|
if (tmp_edge->vert->col->green == -1)
|
|
|
|
tmp_edge->vert->col->green =
|
|
|
|
(sin(green * i * (M_PI / 180)) / 2) + 0.5;
|
|
|
|
|
|
|
|
if (tmp_edge->vert->col->blue == -1)
|
|
|
|
tmp_edge->vert->col->blue =
|
|
|
|
(sin(blue * i * (M_PI / 180)) / 2) + 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
glBegin(GL_POLYGON);
|
2014-05-09 12:54:47 +00:00
|
|
|
do { /* for all edges of the face */
|
2014-05-15 16:33:43 +00:00
|
|
|
glColor3f(tmp_edge->vert->col->red,
|
|
|
|
tmp_edge->vert->col->green,
|
|
|
|
tmp_edge->vert->col->blue);
|
|
|
|
|
2014-05-12 17:48:46 +00:00
|
|
|
glVertex3f(tmp_edge->vert->vec->x,
|
|
|
|
tmp_edge->vert->vec->y,
|
|
|
|
tmp_edge->vert->vec->z);
|
2014-05-15 16:33:43 +00:00
|
|
|
|
2014-05-09 12:54:47 +00:00
|
|
|
} while ((tmp_edge = tmp_edge->next) != obj->faces[i].edge);
|
2014-05-15 16:33:43 +00:00
|
|
|
glEnd();
|
2014-05-09 12:54:47 +00:00
|
|
|
}
|
2014-05-15 16:33:43 +00:00
|
|
|
glPopMatrix();
|
2014-05-09 12:54:47 +00:00
|
|
|
}
|
2014-05-08 20:45:16 +00:00
|
|
|
|
2014-06-01 17:20:08 +00:00
|
|
|
/**
|
|
|
|
* Draw the bezier curve.
|
|
|
|
*
|
|
|
|
* @param bez the bezier curve to draw
|
|
|
|
* @param step_factor_inc the step factor between calculated control points
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_bez(const bez_curv *bez, float step_factor_inc)
|
2014-05-31 16:43:05 +00:00
|
|
|
{
|
|
|
|
static float line_width = 2;
|
2014-05-31 16:54:28 +00:00
|
|
|
static float point_size = 10;
|
2014-06-01 00:13:45 +00:00
|
|
|
static float step_factor = 0.1;
|
|
|
|
|
2014-06-07 20:44:16 +00:00
|
|
|
if ((step_factor + step_factor_inc) > 0.002 &&
|
|
|
|
(step_factor + step_factor_inc) < 0.50)
|
2014-06-01 00:13:45 +00:00
|
|
|
step_factor += step_factor_inc;
|
2014-05-31 16:43:05 +00:00
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
|
|
|
|
glLineWidth(line_width);
|
2014-05-31 16:54:28 +00:00
|
|
|
glPointSize(point_size);
|
2014-05-31 16:43:05 +00:00
|
|
|
glColor3f(1.0, 0.0, 0.0);
|
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
vector *v1 = NULL,
|
|
|
|
*v2 = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* draw frame
|
|
|
|
*/
|
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
for (uint32_t j = 0; j <= bez->deg; j++) {
|
|
|
|
glVertex3f(bez->vec[j].x,
|
|
|
|
bez->vec[j].y,
|
|
|
|
bez->vec[j].z);
|
|
|
|
}
|
|
|
|
glEnd();
|
2014-06-01 00:13:45 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
/*
|
|
|
|
* draw control points
|
|
|
|
*/
|
|
|
|
glBegin(GL_POINTS);
|
|
|
|
for (uint32_t j = 0; j <= bez->deg; j++) {
|
|
|
|
glVertex3f(bez->vec[j].x,
|
|
|
|
bez->vec[j].y,
|
|
|
|
bez->vec[j].z);
|
|
|
|
}
|
|
|
|
glEnd();
|
2014-05-31 16:43:05 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
glBegin(GL_LINES);
|
2014-06-01 00:13:45 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
/*
|
|
|
|
* line segments: first line
|
|
|
|
*/
|
|
|
|
v1 = calculate_bezier_point(bez, step_factor);
|
|
|
|
glVertex3f(bez->vec[0].x,
|
|
|
|
bez->vec[0].y,
|
|
|
|
bez->vec[0].z);
|
|
|
|
glVertex3f(v1->x,
|
|
|
|
v1->y,
|
|
|
|
v1->z);
|
|
|
|
|
|
|
|
for (float k = step_factor; k < 1 - step_factor; k += step_factor) {
|
|
|
|
free(v1);
|
|
|
|
free(v2);
|
|
|
|
|
|
|
|
v1 = calculate_bezier_point(bez, k);
|
|
|
|
v2 = calculate_bezier_point(bez, k +
|
|
|
|
step_factor);
|
2014-06-01 00:13:45 +00:00
|
|
|
|
|
|
|
/*
|
2014-06-01 17:12:07 +00:00
|
|
|
* line segments: middle lines
|
2014-06-01 00:13:45 +00:00
|
|
|
*/
|
|
|
|
glVertex3f(v1->x,
|
|
|
|
v1->y,
|
|
|
|
v1->z);
|
|
|
|
glVertex3f(v2->x,
|
|
|
|
v2->y,
|
|
|
|
v2->z);
|
2014-06-01 17:12:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* line segments: last line
|
|
|
|
*/
|
|
|
|
glVertex3f(v2->x,
|
|
|
|
v2->y,
|
|
|
|
v2->z);
|
|
|
|
glVertex3f(bez->vec[bez->deg].x,
|
|
|
|
bez->vec[bez->deg].y,
|
|
|
|
bez->vec[bez->deg].z);
|
2014-06-01 00:13:45 +00:00
|
|
|
|
|
|
|
free(v1);
|
|
|
|
free(v2);
|
|
|
|
|
2014-05-31 16:54:28 +00:00
|
|
|
glEnd();
|
2014-06-01 16:17:11 +00:00
|
|
|
|
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
2014-06-01 16:17:11 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_bez_frame(const bez_curv *bez,
|
2014-06-01 17:12:07 +00:00
|
|
|
float pos)
|
|
|
|
{
|
|
|
|
bez_curv cur_bez = *bez;
|
|
|
|
bez_curv next_bez = { NULL, 0 };
|
2014-06-01 16:17:11 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
while ((get_reduced_bez_curv(&cur_bez, &next_bez, pos))) {
|
2014-06-01 16:17:11 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
glBegin(GL_LINES);
|
2014-06-01 16:17:11 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
for (uint32_t j = 0; j < next_bez.deg; j++) {
|
|
|
|
glVertex3f(next_bez.vec[j].x,
|
|
|
|
next_bez.vec[j].y,
|
|
|
|
next_bez.vec[j].z);
|
|
|
|
glVertex3f(next_bez.vec[j + 1].x,
|
|
|
|
next_bez.vec[j + 1].y,
|
|
|
|
next_bez.vec[j + 1].z);
|
2014-06-01 16:17:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
/* don't free the original one! */
|
|
|
|
if (cur_bez.deg < bez->deg)
|
|
|
|
free(cur_bez.vec);
|
|
|
|
cur_bez = next_bez;
|
|
|
|
|
|
|
|
glEnd();
|
2014-05-31 16:54:28 +00:00
|
|
|
}
|
2014-06-01 17:12:07 +00:00
|
|
|
free(cur_bez.vec);
|
2014-05-31 16:54:28 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
}
|
2014-06-01 00:13:45 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_ball(const bez_curv *bez,
|
2014-06-01 17:12:07 +00:00
|
|
|
const float pos)
|
|
|
|
{
|
|
|
|
const float ball_pos = pos;
|
2014-06-06 20:16:33 +00:00
|
|
|
vector *point;
|
2014-06-01 17:12:07 +00:00
|
|
|
|
|
|
|
glPushMatrix();
|
2014-06-01 17:20:25 +00:00
|
|
|
glColor3f(0.0, 1.0, 0.0);
|
2014-06-06 20:16:33 +00:00
|
|
|
point = calculate_bezier_point(bez,
|
2014-06-01 17:12:07 +00:00
|
|
|
ball_pos);
|
|
|
|
glTranslatef(point->x, point->y, point->z);
|
|
|
|
glutWireSphere(0.02f, 100, 100);
|
2014-05-31 16:43:05 +00:00
|
|
|
glPopMatrix();
|
2014-06-06 20:16:33 +00:00
|
|
|
|
|
|
|
free(point);
|
2014-05-31 16:43:05 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 16:33:43 +00:00
|
|
|
|
2014-05-08 20:45:16 +00:00
|
|
|
/**
|
2014-05-09 12:27:58 +00:00
|
|
|
* Draws an object.
|
|
|
|
*
|
|
|
|
* @param myxrot rotation increment around x-axis
|
|
|
|
* @param myyrot rotation increment around x-axis
|
|
|
|
* @param myzrot rotation increment around x-axis
|
2014-06-01 17:20:08 +00:00
|
|
|
* @param bez_inc the step factor between calculated control points
|
|
|
|
* for the bezier curve
|
2014-05-08 20:45:16 +00:00
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_obj(int32_t const myxrot,
|
2014-05-12 17:49:52 +00:00
|
|
|
int32_t const myyrot,
|
2014-06-01 00:13:45 +00:00
|
|
|
int32_t const myzrot,
|
|
|
|
float bez_inc)
|
2014-05-08 20:45:16 +00:00
|
|
|
{
|
2014-05-12 18:55:16 +00:00
|
|
|
/* rotation */
|
2014-05-12 17:49:52 +00:00
|
|
|
static int32_t xrot = 0,
|
2014-05-09 12:27:58 +00:00
|
|
|
yrot = 0,
|
|
|
|
zrot = 0;
|
2014-06-01 17:12:07 +00:00
|
|
|
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;
|
2014-05-12 18:55:16 +00:00
|
|
|
|
2014-05-11 12:02:34 +00:00
|
|
|
vector center_vert;
|
2014-05-09 12:27:58 +00:00
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
FIND_CENTER(obj, ¢er_vert);
|
2014-05-11 12:02:34 +00:00
|
|
|
|
2014-05-12 18:55:16 +00:00
|
|
|
/* increment rotation, if any */
|
2014-05-09 12:27:58 +00:00
|
|
|
xrot += myxrot;
|
|
|
|
yrot += myyrot;
|
|
|
|
zrot += myzrot;
|
|
|
|
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* rotate according to static members */
|
|
|
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
2014-05-12 18:20:03 +00:00
|
|
|
glScalef(VISIBILITY_FACTOR,
|
|
|
|
VISIBILITY_FACTOR,
|
|
|
|
VISIBILITY_FACTOR);
|
2014-05-09 12:27:58 +00:00
|
|
|
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
|
|
|
|
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
|
|
|
|
glRotatef(zrot, 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
|
|
|
2014-05-08 20:45:16 +00:00
|
|
|
/* pull into middle of universe */
|
2014-05-11 12:02:34 +00:00
|
|
|
glTranslatef(-center_vert.x,
|
|
|
|
-center_vert.y,
|
|
|
|
-center_vert.z + SYSTEM_POS_Z);
|
2014-05-08 20:45:16 +00:00
|
|
|
|
2014-05-31 16:51:41 +00:00
|
|
|
if (obj->ec != 0) {
|
|
|
|
if (show_normals)
|
|
|
|
draw_normals(obj, 0);
|
|
|
|
|
2014-05-31 16:43:05 +00:00
|
|
|
draw_vertices(obj, false);
|
2014-05-31 16:51:41 +00:00
|
|
|
}
|
2014-05-31 16:43:05 +00:00
|
|
|
|
2014-06-01 17:12:07 +00:00
|
|
|
if (obj->bzc != 0) {
|
|
|
|
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);
|
|
|
|
}
|
2014-05-08 20:45:16 +00:00
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a planet with some Saturn like rings, two moons and an axis.
|
|
|
|
* Rotates the planet around the sun, the moons around the planet
|
|
|
|
* and the planet around its axis.
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_Planet_1(void)
|
2014-05-08 20:45:16 +00:00
|
|
|
{
|
|
|
|
GLUquadric* quadric = NULL;
|
|
|
|
quadric = gluNewQuadric();
|
|
|
|
const int rot_fac_day = 15;
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Rotate around the sun */
|
2014-05-08 20:45:16 +00:00
|
|
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
|
|
|
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
|
|
|
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(0.0f, 4.0f, 0.0f);
|
|
|
|
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, -1.0f);
|
|
|
|
glRotatef(315, 0.0f, 1.0f, 0.0f);
|
|
|
|
|
|
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* A rotation (full 360°) once a day is much
|
2014-05-17 15:24:29 +00:00
|
|
|
* too fast you wouldn't see a thing */
|
2014-05-08 20:45:16 +00:00
|
|
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, 1.0f);
|
|
|
|
glutWireSphere(1.0f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, -1.0f);
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Center axis */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glLineWidth(3);
|
|
|
|
glColor3f(1.0, 1.0, 1.0);
|
|
|
|
glBegin(GL_LINE_LOOP);
|
|
|
|
glVertex3f(0, 0, -5);
|
|
|
|
glVertex3f(0, 0, 5);
|
|
|
|
glEnd();
|
|
|
|
glPopMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* circle1 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(0.8f, 0.0f, 0.2f);
|
2014-05-09 12:27:58 +00:00
|
|
|
/* glRotatef(90, 0.0f, 1.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
2014-05-08 20:45:16 +00:00
|
|
|
gluDisk(quadric, 1.2f, 1.3f, 32, 1);
|
|
|
|
glPopMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* circle2 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(0.0f, 1.0f, 0.0f);
|
2014-05-09 12:27:58 +00:00
|
|
|
/* glRotatef(90, 0.0f, 1.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
2014-05-08 20:45:16 +00:00
|
|
|
gluDisk(quadric, 1.4f, 1.7f, 32, 1);
|
|
|
|
glPopMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Moon1 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(0.0f, 0.0f, 1.0f);
|
2014-05-09 12:27:58 +00:00
|
|
|
/* glRotatef((ROT_FACTOR_MOON * day), 1.0f, 0.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
2014-05-08 20:45:16 +00:00
|
|
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(0.0f, 2.0f, 0.0f);
|
|
|
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glPopMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Moon2 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(0.0f, 1.0f, 1.0f);
|
2014-05-09 12:27:58 +00:00
|
|
|
/* glRotatef((ROT_FACTOR_MOON * day), 1.0f, 0.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
2014-05-08 20:45:16 +00:00
|
|
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(0.0f, -2.0f, 0.0f);
|
|
|
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a Planet with three moons.
|
|
|
|
* The planet rotates around the sun and the moons around the planet
|
|
|
|
* and the planet around its axis.
|
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_Planet_2(void)
|
2014-05-08 20:45:16 +00:00
|
|
|
{
|
|
|
|
glPushMatrix();
|
|
|
|
const float moon_pos_fac = 2.5;
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Rotate around the sun */
|
2014-05-08 20:45:16 +00:00
|
|
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
|
|
|
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
|
|
|
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(-2.0f, -8.0f, 0.0f);
|
|
|
|
|
|
|
|
glColor3f(0.0f, 0.0f, 1.0f);
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* A rotation (full 360°) once a day is much
|
|
|
|
* too fast you woulden'd see a thing */
|
2014-05-08 20:45:16 +00:00
|
|
|
const int rot_fac_day = 15;
|
|
|
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, 1.0f);
|
|
|
|
glutWireSphere(1.3f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, -1.0f);
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Moon3 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(cos(0 * (M_PI / 180)) * moon_pos_fac,
|
|
|
|
sin(0 * (M_PI / 180)) * moon_pos_fac, 0.0f);
|
|
|
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glPopMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Moon4 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(1.0f, 0.0f, 1.0f);
|
|
|
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(cos(120 * (M_PI / 180)) * moon_pos_fac,
|
|
|
|
sin(120 * (M_PI / 180)) * moon_pos_fac, 0.0f);
|
|
|
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glPopMatrix();
|
|
|
|
|
2014-05-09 12:27:58 +00:00
|
|
|
/* Moon5 */
|
2014-05-08 20:45:16 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
|
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
|
|
|
glTranslatef(cos(240 * (M_PI / 180)) * moon_pos_fac,
|
|
|
|
sin(240 * (M_PI / 180)) * moon_pos_fac, 0.0f);
|
|
|
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-09 15:53:16 +00:00
|
|
|
* Displays the whole setup with the sun, planet one,
|
|
|
|
* planet two and the frame rate
|
2014-05-08 20:45:16 +00:00
|
|
|
*/
|
2014-06-06 20:16:33 +00:00
|
|
|
void draw_scene(void)
|
2014-05-08 20:45:16 +00:00
|
|
|
{
|
2014-06-06 20:16:33 +00:00
|
|
|
day++;
|
|
|
|
if (day >= yearabs) {
|
|
|
|
day = 0;
|
|
|
|
year++;
|
|
|
|
}
|
|
|
|
if (year >= (INT_MAX - 1000) || year < 0) {
|
|
|
|
year = 0;
|
|
|
|
}
|
|
|
|
if (day < 0) {
|
|
|
|
day = 0;
|
|
|
|
}
|
2014-05-15 22:32:38 +00:00
|
|
|
|
2014-05-08 20:45:16 +00:00
|
|
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
2014-06-01 00:13:45 +00:00
|
|
|
draw_obj(0, 0, 0, 0);
|
2014-05-08 20:45:16 +00:00
|
|
|
draw_Planet_1();
|
|
|
|
draw_Planet_2();
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
gluOrtho2D(0.0, 500, 0.0, 500);
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glPushMatrix();
|
|
|
|
glLoadIdentity();
|
|
|
|
glColor3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glRasterPos2i(5, 10);
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glPopMatrix();
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glPopMatrix();
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
|