Replace glut with SDL2
This commit is contained in:
parent
aa59033d28
commit
480da24de4
27
src/Makefile
27
src/Makefile
@ -1,12 +1,31 @@
|
|||||||
include ../common.mk
|
include ../common.mk
|
||||||
|
|
||||||
TARGET = drow-engine
|
TARGET = drow-engine
|
||||||
HEADERS = err.h common.h print.h filereader.h gl_draw.h vector.h half_edge.h bezier.h
|
HEADERS = \
|
||||||
OBJECTS = print.o filereader.o gl_draw.o vector.o half_edge.o half_edge_AS.o bezier.o
|
err.h \
|
||||||
|
common.h \
|
||||||
|
print.h \
|
||||||
|
filereader.h \
|
||||||
|
gl_draw.h \
|
||||||
|
vector.h \
|
||||||
|
half_edge.h \
|
||||||
|
bezier.h \
|
||||||
|
gl_setup.h
|
||||||
|
|
||||||
|
OBJECTS = \
|
||||||
|
print.o \
|
||||||
|
filereader.o \
|
||||||
|
gl_draw.o \
|
||||||
|
vector.o \
|
||||||
|
half_edge.o \
|
||||||
|
half_edge_AS.o \
|
||||||
|
bezier.o \
|
||||||
|
gl_setup.o
|
||||||
|
|
||||||
INCS = -I.
|
INCS = -I.
|
||||||
|
|
||||||
CFLAGS += $(shell $(PKG_CONFIG) --cflags gl glu glib-2.0)
|
CFLAGS += $(shell $(PKG_CONFIG) --cflags gl glu glib-2.0 sdl2)
|
||||||
LIBS = $(shell $(PKG_CONFIG) --libs gl glu glib-2.0) -lglut -lm
|
LIBS = $(shell $(PKG_CONFIG) --libs gl glu glib-2.0 sdl2) -lglut -lm
|
||||||
CPPFLAGS += -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED -D_GNU_SOURCE
|
CPPFLAGS += -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED -D_GNU_SOURCE
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
|
350
src/gl_draw.c
350
src/gl_draw.c
@ -35,35 +35,23 @@
|
|||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define XY_WIRE_COUNT 10.0f
|
|
||||||
|
|
||||||
#define FPS_OUT_SIZE 17
|
|
||||||
|
|
||||||
#define ROT_FACTOR_PLANET_SUN (360.0 / yearabs)
|
|
||||||
#define ROT_FACTOR_PLANET (360.0 / 1.0)
|
|
||||||
#define ROT_FACTOR_MOON (360.0 / dayabs)
|
|
||||||
|
|
||||||
#define SYSTEM_POS_Z -15.0f
|
|
||||||
#define SYSTEM_POS_Z_BACK 15.0f
|
|
||||||
#define VISIBILITY_FACTOR 5.0f
|
|
||||||
#define FAR_CLIPPING_PLANE 60.0f
|
|
||||||
#define NEAR_CLIPPING_PLANE 1.0f
|
|
||||||
#define CAMERA_ANGLE 60.0f
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* globals
|
* globals
|
||||||
*/
|
*/
|
||||||
int year;
|
int year = 0;
|
||||||
int yearabs = 365;
|
int yearabs = 365;
|
||||||
int day;
|
int day = 0;
|
||||||
int dayabs = 30;
|
int dayabs = 30;
|
||||||
HE_obj *obj;
|
HE_obj *obj;
|
||||||
bool show_normals = false;
|
bool show_normals = false;
|
||||||
@ -72,27 +60,6 @@ bool draw_frame = false;
|
|||||||
float ball_speed = 1.0f;
|
float ball_speed = 1.0f;
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* static function declaration
|
|
||||||
*/
|
|
||||||
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,
|
|
||||||
int32_t const myyrot,
|
|
||||||
int32_t const myzrot,
|
|
||||||
float bez_inc);
|
|
||||||
static void draw_Planet_1(void);
|
|
||||||
static void draw_Planet_2(void);
|
|
||||||
static void gl_destroy(void);
|
|
||||||
static void draw_normals(HE_obj const * const obj,
|
|
||||||
float const scale_inc);
|
|
||||||
static void draw_vertices(HE_obj const * const obj,
|
|
||||||
bool disco);
|
|
||||||
static float calculateFPS();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draws the vertex normals of the object.
|
* Draws the vertex normals of the object.
|
||||||
@ -100,7 +67,7 @@ static float calculateFPS();
|
|||||||
* @param obj the object to draw the vertex normals of
|
* @param obj the object to draw the vertex normals of
|
||||||
* @param scale_inc the incrementor for scaling the normals
|
* @param scale_inc the incrementor for scaling the normals
|
||||||
*/
|
*/
|
||||||
static void draw_normals(HE_obj const * const obj,
|
void draw_normals(HE_obj const * const obj,
|
||||||
float const scale_inc)
|
float const scale_inc)
|
||||||
{
|
{
|
||||||
static float normals_scale_factor = 0.1f;
|
static float normals_scale_factor = 0.1f;
|
||||||
@ -139,7 +106,7 @@ static void draw_normals(HE_obj const * const obj,
|
|||||||
* @param obj the object of which we will draw the vertices
|
* @param obj the object of which we will draw the vertices
|
||||||
* @param disco_set determines whether we are in disco mode
|
* @param disco_set determines whether we are in disco mode
|
||||||
*/
|
*/
|
||||||
static void draw_vertices(HE_obj const * const obj,
|
void draw_vertices(HE_obj const * const obj,
|
||||||
bool disco_set)
|
bool disco_set)
|
||||||
{
|
{
|
||||||
/* color */
|
/* color */
|
||||||
@ -209,7 +176,7 @@ static void draw_vertices(HE_obj const * const obj,
|
|||||||
* @param bez the bezier curve to draw
|
* @param bez the bezier curve to draw
|
||||||
* @param step_factor_inc the step factor between calculated control points
|
* @param step_factor_inc the step factor between calculated control points
|
||||||
*/
|
*/
|
||||||
static void draw_bez(const bez_curv *bez, float step_factor_inc)
|
void draw_bez(const bez_curv *bez, float step_factor_inc)
|
||||||
{
|
{
|
||||||
static float line_width = 2;
|
static float line_width = 2;
|
||||||
static float point_size = 10;
|
static float point_size = 10;
|
||||||
@ -310,7 +277,7 @@ static void draw_bez(const bez_curv *bez, float step_factor_inc)
|
|||||||
* @param pos the position where the curve and the frame
|
* @param pos the position where the curve and the frame
|
||||||
* will cut
|
* will cut
|
||||||
*/
|
*/
|
||||||
static void draw_bez_frame(const bez_curv *bez,
|
void draw_bez_frame(const bez_curv *bez,
|
||||||
float pos)
|
float pos)
|
||||||
{
|
{
|
||||||
bez_curv cur_bez = *bez;
|
bez_curv cur_bez = *bez;
|
||||||
@ -346,18 +313,21 @@ static void draw_bez_frame(const bez_curv *bez,
|
|||||||
* @param bez the bezier curve to draw the ball on
|
* @param bez the bezier curve to draw the ball on
|
||||||
* @param pos the position of the ball
|
* @param pos the position of the ball
|
||||||
*/
|
*/
|
||||||
static void draw_ball(const bez_curv *bez,
|
void draw_ball(const bez_curv *bez,
|
||||||
const float pos)
|
const float pos)
|
||||||
{
|
{
|
||||||
const float ball_pos = pos;
|
const float ball_pos = pos;
|
||||||
|
vector *point;
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(0.0, 1.0, 0.0);
|
glColor3f(0.0, 1.0, 0.0);
|
||||||
vector *point = calculate_bezier_point(bez,
|
point = calculate_bezier_point(bez,
|
||||||
ball_pos);
|
ball_pos);
|
||||||
glTranslatef(point->x, point->y, point->z);
|
glTranslatef(point->x, point->y, point->z);
|
||||||
glutWireSphere(0.02f, 100, 100);
|
glutWireSphere(0.02f, 100, 100);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
|
free(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -370,7 +340,7 @@ static void draw_ball(const bez_curv *bez,
|
|||||||
* @param bez_inc the step factor between calculated control points
|
* @param bez_inc the step factor between calculated control points
|
||||||
* for the bezier curve
|
* for the bezier curve
|
||||||
*/
|
*/
|
||||||
static void draw_obj(int32_t const myxrot,
|
void draw_obj(int32_t const myxrot,
|
||||||
int32_t const myyrot,
|
int32_t const myyrot,
|
||||||
int32_t const myzrot,
|
int32_t const myzrot,
|
||||||
float bez_inc)
|
float bez_inc)
|
||||||
@ -440,7 +410,7 @@ static void draw_obj(int32_t const myxrot,
|
|||||||
* Rotates the planet around the sun, the moons around the planet
|
* Rotates the planet around the sun, the moons around the planet
|
||||||
* and the planet around its axis.
|
* and the planet around its axis.
|
||||||
*/
|
*/
|
||||||
static void draw_Planet_1(void)
|
void draw_Planet_1(void)
|
||||||
{
|
{
|
||||||
GLUquadric* quadric = NULL;
|
GLUquadric* quadric = NULL;
|
||||||
quadric = gluNewQuadric();
|
quadric = gluNewQuadric();
|
||||||
@ -515,7 +485,7 @@ static void draw_Planet_1(void)
|
|||||||
* The planet rotates around the sun and the moons around the planet
|
* The planet rotates around the sun and the moons around the planet
|
||||||
* and the planet around its axis.
|
* and the planet around its axis.
|
||||||
*/
|
*/
|
||||||
static void draw_Planet_2(void)
|
void draw_Planet_2(void)
|
||||||
{
|
{
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
const float moon_pos_fac = 2.5;
|
const float moon_pos_fac = 2.5;
|
||||||
@ -565,39 +535,23 @@ static void draw_Planet_2(void)
|
|||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the frames per second rate.
|
|
||||||
*
|
|
||||||
* @return the actual frames per second rate
|
|
||||||
*/
|
|
||||||
static float calculateFPS(void)
|
|
||||||
{
|
|
||||||
static int frameCount = 0;
|
|
||||||
static int currentTime = 0;
|
|
||||||
static int previousTime = 0;
|
|
||||||
static float fps = 0;
|
|
||||||
|
|
||||||
frameCount++;
|
|
||||||
currentTime = glutGet(GLUT_ELAPSED_TIME);
|
|
||||||
int timeInterval = currentTime - previousTime;
|
|
||||||
if (timeInterval > 1000) {
|
|
||||||
fps = frameCount / (timeInterval / 1000.0f);
|
|
||||||
previousTime = currentTime;
|
|
||||||
frameCount = 0;
|
|
||||||
}
|
|
||||||
return fps;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays the whole setup with the sun, planet one,
|
* Displays the whole setup with the sun, planet one,
|
||||||
* planet two and the frame rate
|
* planet two and the frame rate
|
||||||
*/
|
*/
|
||||||
void display(void)
|
void draw_scene(void)
|
||||||
{
|
{
|
||||||
char fps_out[FPS_OUT_SIZE];
|
day++;
|
||||||
char *tmp_ptr = fps_out;
|
if (day >= yearabs) {
|
||||||
float fps = calculateFPS();
|
day = 0;
|
||||||
int i;
|
year++;
|
||||||
|
}
|
||||||
|
if (year >= (INT_MAX - 1000) || year < 0) {
|
||||||
|
year = 0;
|
||||||
|
}
|
||||||
|
if (day < 0) {
|
||||||
|
day = 0;
|
||||||
|
}
|
||||||
|
|
||||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
@ -617,256 +571,10 @@ void display(void)
|
|||||||
glColor3f(1.0f, 1.0f, 1.0f);
|
glColor3f(1.0f, 1.0f, 1.0f);
|
||||||
glRasterPos2i(5, 10);
|
glRasterPos2i(5, 10);
|
||||||
|
|
||||||
sprintf(fps_out, "FPS: %f", fps);
|
|
||||||
|
|
||||||
for (i = 0; i < FPS_OUT_SIZE; ++i)
|
|
||||||
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *tmp_ptr++);
|
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
glutSwapBuffers();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the initial values to start the program.
|
|
||||||
*/
|
|
||||||
void init(char const * const filename)
|
|
||||||
{
|
|
||||||
obj = read_obj_file(filename);
|
|
||||||
if (!obj)
|
|
||||||
ABORT("Failed to read object file \"%s\"!", filename);
|
|
||||||
|
|
||||||
NORMALIZE_OBJECT(obj);
|
|
||||||
|
|
||||||
day = 0;
|
|
||||||
year = 0;
|
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
glShadeModel(GL_SMOOTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Is called when the window size changes.
|
|
||||||
* Fits the viewport to the new dimension.
|
|
||||||
*
|
|
||||||
* @param w the new width of the window
|
|
||||||
* @param h the new height of the window
|
|
||||||
*/
|
|
||||||
void reshape(GLsizei w, GLsizei h)
|
|
||||||
{
|
|
||||||
glViewport(0, 0, w, h);
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
|
||||||
glLoadIdentity();
|
|
||||||
gluPerspective(CAMERA_ANGLE,
|
|
||||||
(GLfloat) w / (GLfloat) h,
|
|
||||||
NEAR_CLIPPING_PLANE,
|
|
||||||
FAR_CLIPPING_PLANE);
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
|
||||||
glLoadIdentity();
|
|
||||||
glTranslatef(0.0, 0.0, -5.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Is called over and over again.
|
|
||||||
* Counts the years and days.
|
|
||||||
*/
|
|
||||||
void animate()
|
|
||||||
{
|
|
||||||
day++;
|
|
||||||
if (day >= yearabs) {
|
|
||||||
day = 0;
|
|
||||||
year++;
|
|
||||||
}
|
|
||||||
if (year >= (INT_MAX - 1000) || year < 0) {
|
|
||||||
year = 0;
|
|
||||||
}
|
|
||||||
if (day < 0) {
|
|
||||||
day = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
glutTimerFunc(TIMERMSECS, animate, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keyboard callback function,
|
|
||||||
*
|
|
||||||
* press t to increase the day
|
|
||||||
*
|
|
||||||
* press T to decrease the day
|
|
||||||
*
|
|
||||||
* press j to increase the year
|
|
||||||
*
|
|
||||||
* press J to decrease the year
|
|
||||||
*
|
|
||||||
* press b to toggle GL_CULL_FACE
|
|
||||||
*
|
|
||||||
* press D to toggle disco mode
|
|
||||||
*
|
|
||||||
* press S to toggle shade model between GL_SMOOTH and GL_FLAT
|
|
||||||
*
|
|
||||||
* press n to toggle normals
|
|
||||||
*
|
|
||||||
* press k to increase length of normals
|
|
||||||
*
|
|
||||||
* press l to decrease length of normals
|
|
||||||
*
|
|
||||||
* press x to rotate the middle object in x direction
|
|
||||||
*
|
|
||||||
* press X to rotate the middle object in -x direction
|
|
||||||
*
|
|
||||||
* press y to rotate the middle object in y direction
|
|
||||||
*
|
|
||||||
* press Y to rotate the middle object in -y direction
|
|
||||||
*
|
|
||||||
* press c to rotate the middle object in z direction
|
|
||||||
*
|
|
||||||
* press C to rotate the middle object in -z direction
|
|
||||||
*
|
|
||||||
* press w to translate the whole scene in y direction
|
|
||||||
*
|
|
||||||
* press s to translate the whole scene in -y direction
|
|
||||||
*
|
|
||||||
* press a to translate the whole scene in -x direction
|
|
||||||
*
|
|
||||||
* press d to translate the whole scene in x direction
|
|
||||||
*
|
|
||||||
* press q to quit
|
|
||||||
*
|
|
||||||
* @param key which was pressed
|
|
||||||
* @param x coordinate
|
|
||||||
* @param y coordinate
|
|
||||||
*/
|
|
||||||
void keyboard(unsigned char key, int x, int y)
|
|
||||||
{
|
|
||||||
switch (key) {
|
|
||||||
/* case 'b': */
|
|
||||||
/* if (glIsEnabled(GL_CULL_FACE)) */
|
|
||||||
/* glDisable(GL_CULL_FACE); */
|
|
||||||
/* else */
|
|
||||||
/* glEnable(GL_CULL_FACE); */
|
|
||||||
/* glutPostRedisplay(); */
|
|
||||||
/* break; */
|
|
||||||
case 't':
|
|
||||||
dayabs += 15;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'j':
|
|
||||||
yearabs += 50;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'T':
|
|
||||||
dayabs -= 15;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'J':
|
|
||||||
yearabs -= 50;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'x':
|
|
||||||
draw_obj(2, 0, 0, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'X':
|
|
||||||
draw_obj(-2, 0, 0, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'y':
|
|
||||||
draw_obj(0, 2, 0, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'Y':
|
|
||||||
draw_obj(0, -2, 0, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'c':
|
|
||||||
draw_obj(0, 0, 2, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
draw_obj(0, 0, -2, 0);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'D':
|
|
||||||
draw_vertices(obj, true);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
if (shademodel) {
|
|
||||||
glShadeModel(GL_FLAT);
|
|
||||||
shademodel = false;
|
|
||||||
} else {
|
|
||||||
glShadeModel(GL_SMOOTH);
|
|
||||||
shademodel = true;
|
|
||||||
}
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'b':
|
|
||||||
draw_obj(0, 0, 0, 0.02);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'B':
|
|
||||||
draw_obj(0, 0, 0, -0.02);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'k':
|
|
||||||
ball_speed += 0.2f;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'K':
|
|
||||||
if (ball_speed - 0.2f > 0)
|
|
||||||
ball_speed -= 0.2f;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
draw_frame = !draw_frame;
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
|
|
||||||
/* case 'k': */
|
|
||||||
/* draw_normals(obj, 0.01f); */
|
|
||||||
/* glutPostRedisplay(); */
|
|
||||||
/* break; */
|
|
||||||
/* case 'l': */
|
|
||||||
/* draw_normals(obj, -0.01f); */
|
|
||||||
/* glutPostRedisplay(); */
|
|
||||||
/* break; */
|
|
||||||
case 'w':
|
|
||||||
glTranslatef(0.0f, 1.0f, 0.0f);
|
|
||||||
break;
|
|
||||||
case 'a':
|
|
||||||
glTranslatef(-1.0f, 0.0f, 0.0f);
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
glTranslatef(0.0f, -1.0f, 0.0f);
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
glTranslatef(1.0f, 0.0f, 0.0f);
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
show_normals = !show_normals;
|
|
||||||
break;
|
|
||||||
case '+':
|
|
||||||
glTranslatef(0.0f, 0.0f, 1.0f);
|
|
||||||
break;
|
|
||||||
case '-':
|
|
||||||
glTranslatef(0.0f, 0.0f, -1.0f);
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
gl_destroy();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroy the gl session/window.
|
|
||||||
*/
|
|
||||||
static void gl_destroy(void)
|
|
||||||
{
|
|
||||||
delete_object(obj);
|
|
||||||
free(obj);
|
|
||||||
glutDestroyWindow(glutGetWindow());
|
|
||||||
}
|
|
||||||
|
@ -26,22 +26,62 @@
|
|||||||
#define _DROW_ENGINE_DRAW_H
|
#define _DROW_ENGINE_DRAW_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "bezier.h"
|
||||||
|
#include "half_edge.h"
|
||||||
|
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vsync timer for animate() function
|
* Vsync timer for animate() function
|
||||||
*/
|
*/
|
||||||
#define TIMERMSECS 25
|
#define TIMERMSECS 25
|
||||||
|
|
||||||
|
#define XY_WIRE_COUNT 10.0f
|
||||||
|
|
||||||
void init(char const * const filename);
|
#define FPS_OUT_SIZE 17
|
||||||
void display(void);
|
|
||||||
void reshape(GLsizei w, GLsizei h);
|
#define ROT_FACTOR_PLANET_SUN (360.0 / yearabs)
|
||||||
void animate();
|
#define ROT_FACTOR_PLANET (360.0 / 1.0)
|
||||||
void keyboard(unsigned char key, int x, int y);
|
#define ROT_FACTOR_MOON (360.0 / dayabs)
|
||||||
|
|
||||||
|
#define SYSTEM_POS_Z -15.0f
|
||||||
|
#define SYSTEM_POS_Z_BACK 15.0f
|
||||||
|
#define VISIBILITY_FACTOR 5.0f
|
||||||
|
#define FAR_CLIPPING_PLANE 60.0f
|
||||||
|
#define NEAR_CLIPPING_PLANE 1.0f
|
||||||
|
#define CAMERA_ANGLE 60.0f
|
||||||
|
|
||||||
|
extern int yearabs;
|
||||||
|
extern int dayabs;
|
||||||
|
extern HE_obj *obj;
|
||||||
|
extern bool show_normals;
|
||||||
|
extern bool shademodel;
|
||||||
|
extern bool draw_frame;
|
||||||
|
extern float ball_speed;
|
||||||
|
|
||||||
|
void draw_normals(HE_obj const * const obj,
|
||||||
|
float const scale_inc);
|
||||||
|
void draw_vertices(HE_obj const * const obj,
|
||||||
|
bool disco_set);
|
||||||
|
void draw_bez(const bez_curv *bez, float step_factor_inc);
|
||||||
|
void draw_bez_frame(const bez_curv *bez,
|
||||||
|
float pos);
|
||||||
|
void draw_ball(const bez_curv *bez,
|
||||||
|
const float pos);
|
||||||
|
void draw_obj(int32_t const myxrot,
|
||||||
|
int32_t const myyrot,
|
||||||
|
int32_t const myzrot,
|
||||||
|
float bez_inc);
|
||||||
|
void draw_Planet_1(void);
|
||||||
|
void draw_Planet_2(void);
|
||||||
|
void draw_scene(void);
|
||||||
|
|
||||||
|
|
||||||
#endif /* _DROW_ENGINE_DRAW_H */
|
#endif /* _DROW_ENGINE_DRAW_H */
|
||||||
|
346
src/gl_setup.c
Normal file
346
src/gl_setup.c
Normal file
@ -0,0 +1,346 @@
|
|||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file gl_setup.c
|
||||||
|
* Operations for setting up SDL and the OpenGL context.
|
||||||
|
* @brief set up SDL/OpenGL
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "err.h"
|
||||||
|
#include "filereader.h"
|
||||||
|
#include "gl_draw.h"
|
||||||
|
#include "gl_setup.h"
|
||||||
|
#include "half_edge.h"
|
||||||
|
|
||||||
|
#include <GL/glut.h>
|
||||||
|
#include <GL/gl.h>
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* static function declaration
|
||||||
|
*/
|
||||||
|
static void init_opengl(void);
|
||||||
|
static bool process_events(SDL_Window *win, SDL_GLContext glctx);
|
||||||
|
static void reshape(SDL_Window *win);
|
||||||
|
static bool process_window_events(SDL_Window *win,
|
||||||
|
SDL_GLContext glctx,
|
||||||
|
SDL_WindowEvent *win_event);
|
||||||
|
static bool process_keypress(SDL_KeyboardEvent *key_event);
|
||||||
|
static void gl_destroy(SDL_Window *win, SDL_GLContext glctx);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the initial values to start the program.
|
||||||
|
*/
|
||||||
|
static void init_opengl(void)
|
||||||
|
{
|
||||||
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glShadeModel(GL_SMOOTH);
|
||||||
|
|
||||||
|
glViewport(0, 0, 680, 480);
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
gluPerspective(CAMERA_ANGLE,
|
||||||
|
(GLfloat) 640 / (GLfloat) 480,
|
||||||
|
NEAR_CLIPPING_PLANE,
|
||||||
|
FAR_CLIPPING_PLANE);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glTranslatef(0.0, 0.0, -5.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool process_events(SDL_Window *win, SDL_GLContext glctx)
|
||||||
|
{
|
||||||
|
SDL_Event e;
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
while (SDL_PollEvent(&e))
|
||||||
|
if (e.type == SDL_QUIT) {
|
||||||
|
running = false;
|
||||||
|
} else if (e.type == SDL_WINDOWEVENT) {
|
||||||
|
running = process_window_events(win, glctx, &(e.window));
|
||||||
|
} else if (e.type == SDL_KEYDOWN) {
|
||||||
|
running = process_keypress(&(e.key));
|
||||||
|
}
|
||||||
|
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reshape(SDL_Window *win)
|
||||||
|
{
|
||||||
|
int w, h;
|
||||||
|
|
||||||
|
SDL_GetWindowSize(win, &w, &h);
|
||||||
|
|
||||||
|
glViewport(0, 0, w, h);
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
gluPerspective(CAMERA_ANGLE,
|
||||||
|
(GLfloat) w / (GLfloat) h,
|
||||||
|
NEAR_CLIPPING_PLANE,
|
||||||
|
FAR_CLIPPING_PLANE);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glTranslatef(0.0, 0.0, -5.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool process_window_events(SDL_Window *win,
|
||||||
|
SDL_GLContext glctx,
|
||||||
|
SDL_WindowEvent *win_event)
|
||||||
|
{
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
if (win_event->event == SDL_WINDOWEVENT_RESIZED)
|
||||||
|
reshape(win);
|
||||||
|
else if (win_event->event == SDL_WINDOWEVENT_CLOSE)
|
||||||
|
running = false;
|
||||||
|
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keyboard callback function,
|
||||||
|
*
|
||||||
|
* press t to increase the day
|
||||||
|
*
|
||||||
|
* press T to decrease the day
|
||||||
|
*
|
||||||
|
* press j to increase the year
|
||||||
|
*
|
||||||
|
* press J to decrease the year
|
||||||
|
*
|
||||||
|
* press b to toggle GL_CULL_FACE
|
||||||
|
*
|
||||||
|
* press D to toggle disco mode
|
||||||
|
*
|
||||||
|
* press S to toggle shade model between GL_SMOOTH and GL_FLAT
|
||||||
|
*
|
||||||
|
* press n to toggle normals
|
||||||
|
*
|
||||||
|
* press k to increase length of normals
|
||||||
|
*
|
||||||
|
* press l to decrease length of normals
|
||||||
|
*
|
||||||
|
* press x to rotate the middle object in x direction
|
||||||
|
*
|
||||||
|
* press X to rotate the middle object in -x direction
|
||||||
|
*
|
||||||
|
* press y to rotate the middle object in y direction
|
||||||
|
*
|
||||||
|
* press Y to rotate the middle object in -y direction
|
||||||
|
*
|
||||||
|
* press c to rotate the middle object in z direction
|
||||||
|
*
|
||||||
|
* press C to rotate the middle object in -z direction
|
||||||
|
*
|
||||||
|
* press w to translate the whole scene in y direction
|
||||||
|
*
|
||||||
|
* press s to translate the whole scene in -y direction
|
||||||
|
*
|
||||||
|
* press a to translate the whole scene in -x direction
|
||||||
|
*
|
||||||
|
* press d to translate the whole scene in x direction
|
||||||
|
*
|
||||||
|
* press q to quit
|
||||||
|
*
|
||||||
|
* @param key which was pressed
|
||||||
|
* @param x coordinate
|
||||||
|
* @param y coordinate
|
||||||
|
*/
|
||||||
|
static bool process_keypress(SDL_KeyboardEvent *key_event)
|
||||||
|
{
|
||||||
|
SDL_Keycode key = key_event->keysym.sym;
|
||||||
|
SDL_Keymod mod = key_event->keysym.mod;
|
||||||
|
bool running = true;
|
||||||
|
|
||||||
|
switch (key) {
|
||||||
|
/* case 'b': */
|
||||||
|
/* if (glIsEnabled(GL_CULL_FACE)) */
|
||||||
|
/* glDisable(GL_CULL_FACE); */
|
||||||
|
/* else */
|
||||||
|
/* glEnable(GL_CULL_FACE); */
|
||||||
|
/* break; */
|
||||||
|
case 't':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
dayabs -= 15;
|
||||||
|
} else {
|
||||||
|
dayabs += 15;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'j':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
yearabs -= 50;
|
||||||
|
} else {
|
||||||
|
yearabs += 50;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'x':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
draw_obj(-2, 0, 0, 0);
|
||||||
|
} else {
|
||||||
|
draw_obj(2, 0, 0, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'y':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
draw_obj(0, -2, 0, 0);
|
||||||
|
} else {
|
||||||
|
draw_obj(0, 2, 0, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
draw_obj(0, 0, -2, 0);
|
||||||
|
} else {
|
||||||
|
draw_obj(0, 0, 2, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
draw_vertices(obj, true);
|
||||||
|
} else {
|
||||||
|
glTranslatef(1.0f, 0.0f, 0.0f);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
if (shademodel) {
|
||||||
|
glShadeModel(GL_FLAT);
|
||||||
|
shademodel = false;
|
||||||
|
} else {
|
||||||
|
glShadeModel(GL_SMOOTH);
|
||||||
|
shademodel = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
glTranslatef(0.0f, -1.0f, 0.0f);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
draw_obj(0, 0, 0, -0.02);
|
||||||
|
} else {
|
||||||
|
draw_obj(0, 0, 0, 0.02);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'k':
|
||||||
|
if (mod & KMOD_SHIFT) {
|
||||||
|
if (ball_speed - 0.2f > 0)
|
||||||
|
ball_speed -= 0.2f;
|
||||||
|
} else {
|
||||||
|
ball_speed += 0.2f;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
draw_frame = !draw_frame;
|
||||||
|
break;
|
||||||
|
/* case 'k': */
|
||||||
|
/* draw_normals(obj, 0.01f); */
|
||||||
|
/* break; */
|
||||||
|
/* case 'l': */
|
||||||
|
/* draw_normals(obj, -0.01f); */
|
||||||
|
/* break; */
|
||||||
|
case 'w':
|
||||||
|
glTranslatef(0.0f, 1.0f, 0.0f);
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
glTranslatef(-1.0f, 0.0f, 0.0f);
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
show_normals = !show_normals;
|
||||||
|
break;
|
||||||
|
case '+':
|
||||||
|
glTranslatef(0.0f, 0.0f, 1.0f);
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
glTranslatef(0.0f, 0.0f, -1.0f);
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
running = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy the gl session/window.
|
||||||
|
*/
|
||||||
|
static void gl_destroy(SDL_Window *win, SDL_GLContext glctx)
|
||||||
|
{
|
||||||
|
delete_object(obj);
|
||||||
|
free(obj);
|
||||||
|
|
||||||
|
SDL_GL_DeleteContext(glctx);
|
||||||
|
SDL_DestroyWindow(win);
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_object(char const * const filename)
|
||||||
|
{
|
||||||
|
obj = read_obj_file(filename);
|
||||||
|
|
||||||
|
if (!obj)
|
||||||
|
ABORT("Failed to read object file \"%s\"!", filename);
|
||||||
|
|
||||||
|
NORMALIZE_OBJECT(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_sdl_loop(void)
|
||||||
|
{
|
||||||
|
SDL_Window *win;
|
||||||
|
SDL_GLContext glctx;
|
||||||
|
const int window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
|
||||||
|
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
|
fprintf(stderr, "Failed initalizing SDL!\n");
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(win = SDL_CreateWindow("Drow Engine", 100, 100, 640, 480,
|
||||||
|
window_flags))) {
|
||||||
|
fprintf(stderr, "Failed creating SDL window!\n");
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(glctx = SDL_GL_CreateContext(win))) {
|
||||||
|
fprintf(stderr, "Failed creating OpenGL context!\n");
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GL_SetSwapInterval(1);
|
||||||
|
|
||||||
|
init_opengl();
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
bool running = process_events(win, glctx);
|
||||||
|
|
||||||
|
if (!running)
|
||||||
|
break;
|
||||||
|
|
||||||
|
draw_scene();
|
||||||
|
SDL_GL_SwapWindow(win);
|
||||||
|
}
|
||||||
|
|
||||||
|
gl_destroy(win, glctx);
|
||||||
|
}
|
33
src/gl_setup.h
Normal file
33
src/gl_setup.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file gl_setup.h
|
||||||
|
* Header for the external API of gl_setup.c
|
||||||
|
* @brief header of gl_setup.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _DROW_ENGINE_SETUP_H
|
||||||
|
#define _DROW_ENGINE_SETUP_H
|
||||||
|
|
||||||
|
|
||||||
|
void init_object(char const * const filename);
|
||||||
|
void init_sdl_loop(void);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* _DROW_ENGINE_SETUP_H */
|
29
src/main.c
29
src/main.c
@ -24,7 +24,7 @@
|
|||||||
* @brief program entry point
|
* @brief program entry point
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "gl_draw.h"
|
#include "gl_setup.h"
|
||||||
#include "half_edge.h"
|
#include "half_edge.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
@ -34,7 +34,7 @@
|
|||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define filename __FILE__
|
#include <SDL.h>
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,36 +45,15 @@ char const * const helptext = "Usage: drow-engine <file.obj>\n";
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int windowX;
|
|
||||||
int windowY;
|
|
||||||
int screenX;
|
|
||||||
int screenY;
|
|
||||||
float factor = 0.80; /* percent of screen */
|
|
||||||
|
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
printf("%s", helptext);
|
printf("%s", helptext);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
glutInit(&argc, argv);
|
glutInit(&argc, argv);
|
||||||
screenX = glutGet(GLUT_SCREEN_WIDTH);
|
|
||||||
windowX = (screenX * factor);
|
|
||||||
screenY = glutGet(GLUT_SCREEN_HEIGHT);
|
|
||||||
windowY = (screenY * factor);
|
|
||||||
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
|
|
||||||
glutInitWindowPosition(((screenX / 2) - (windowX / 2)),
|
|
||||||
((screenY / 2) - (windowY / 2)));
|
|
||||||
glutInitWindowSize(windowX, windowY);
|
|
||||||
glutCreateWindow(filename);
|
|
||||||
glutSetWindowTitle("Drow engine");
|
|
||||||
|
|
||||||
init(argv[1]);
|
init_object(argv[1]);
|
||||||
|
init_sdl_loop();
|
||||||
glutKeyboardFunc(keyboard);
|
|
||||||
glutReshapeFunc(reshape);
|
|
||||||
glutDisplayFunc(display);
|
|
||||||
glutTimerFunc(TIMERMSECS, animate, 0);
|
|
||||||
glutMainLoop();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user