Implement rotation of the single object
This commit is contained in:
parent
03fca66adc
commit
ab95762d50
110
gl_draw.c
110
gl_draw.c
@ -16,6 +16,8 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "err.h"
|
||||||
|
#include "filereader.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
@ -28,7 +30,6 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define filename __FILE__
|
|
||||||
#define XY_WIRE_COUNT 10.0f
|
#define XY_WIRE_COUNT 10.0f
|
||||||
|
|
||||||
#define ROT_FACTOR_PLANET_SUN (360.0 / yearabs)
|
#define ROT_FACTOR_PLANET_SUN (360.0 / yearabs)
|
||||||
@ -47,18 +48,37 @@ HE_obj *obj;
|
|||||||
/*
|
/*
|
||||||
* static function declaration
|
* static function declaration
|
||||||
*/
|
*/
|
||||||
static void draw_Sun(void);
|
static void draw_obj(uint32_t xrot, uint32_t yrot, uint32_t zrot);
|
||||||
static void draw_Planet_1(void);
|
static void draw_Planet_1(void);
|
||||||
static void draw_Planet_2(void);
|
static void draw_Planet_2(void);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draws a sun which changes its color randomly.
|
* Draws an object.
|
||||||
|
*
|
||||||
|
* @param myxrot rotation increment around x-axis
|
||||||
|
* @param myyrot rotation increment around x-axis
|
||||||
|
* @param myzrot rotation increment around x-axis
|
||||||
*/
|
*/
|
||||||
static void draw_Sun(void)
|
static void draw_obj(uint32_t myxrot, uint32_t myyrot, uint32_t myzrot)
|
||||||
{
|
{
|
||||||
|
static uint32_t xrot = 0,
|
||||||
|
yrot = 0,
|
||||||
|
zrot = 0;
|
||||||
|
|
||||||
|
xrot += myxrot;
|
||||||
|
yrot += myyrot;
|
||||||
|
zrot += myzrot;
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
|
||||||
|
/* rotate according to static members */
|
||||||
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
||||||
|
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);
|
||||||
|
|
||||||
/* pull into middle of universe */
|
/* pull into middle of universe */
|
||||||
glTranslatef(-obj->faces->edge->vert->x,
|
glTranslatef(-obj->faces->edge->vert->x,
|
||||||
-obj->faces->edge->vert->y,
|
-obj->faces->edge->vert->y,
|
||||||
@ -74,7 +94,6 @@ static void draw_Sun(void)
|
|||||||
glVertex3f(tmp_edge->vert->x,
|
glVertex3f(tmp_edge->vert->x,
|
||||||
tmp_edge->vert->y,
|
tmp_edge->vert->y,
|
||||||
tmp_edge->vert->z);
|
tmp_edge->vert->z);
|
||||||
|
|
||||||
} while ((tmp_edge = tmp_edge->next) != obj->faces[i].edge);
|
} while ((tmp_edge = tmp_edge->next) != obj->faces[i].edge);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +115,7 @@ static void draw_Planet_1(void)
|
|||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
|
||||||
//Rotate around the sun
|
/* Rotate around the sun */
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
||||||
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
||||||
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, 1.0f);
|
||||||
@ -106,12 +125,13 @@ static void draw_Planet_1(void)
|
|||||||
|
|
||||||
glColor3f(1.0f, 0.0f, 0.0f);
|
glColor3f(1.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
//A rotation (full 360°) once a day is much too fast you woulden'd see a thing
|
/* A rotation (full 360°) once a day is much
|
||||||
|
* too fast you woulden'd see a thing */
|
||||||
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, 1.0f);
|
||||||
glutWireSphere(1.0f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
glutWireSphere(1.0f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
||||||
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, -1.0f);
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, -1.0f);
|
||||||
|
|
||||||
//Center axis
|
/* Center axis */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glLineWidth(3);
|
glLineWidth(3);
|
||||||
glColor3f(1.0, 1.0, 1.0);
|
glColor3f(1.0, 1.0, 1.0);
|
||||||
@ -121,33 +141,33 @@ static void draw_Planet_1(void)
|
|||||||
glEnd();
|
glEnd();
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
//circle1
|
/* circle1 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(0.8f, 0.0f, 0.2f);
|
glColor3f(0.8f, 0.0f, 0.2f);
|
||||||
//glRotatef(90, 0.0f, 1.0f, 0.0f); //"senkrecht zur Planetenachse"
|
/* glRotatef(90, 0.0f, 1.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
||||||
gluDisk(quadric, 1.2f, 1.3f, 32, 1);
|
gluDisk(quadric, 1.2f, 1.3f, 32, 1);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
//circle2
|
/* circle2 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(0.0f, 1.0f, 0.0f);
|
glColor3f(0.0f, 1.0f, 0.0f);
|
||||||
//glRotatef(90, 0.0f, 1.0f, 0.0f); //"senkrecht zur Planetenachse"
|
/* glRotatef(90, 0.0f, 1.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
||||||
gluDisk(quadric, 1.4f, 1.7f, 32, 1);
|
gluDisk(quadric, 1.4f, 1.7f, 32, 1);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
//Moon1
|
/* Moon1 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(0.0f, 0.0f, 1.0f);
|
glColor3f(0.0f, 0.0f, 1.0f);
|
||||||
//glRotatef((ROT_FACTOR_MOON * day), 1.0f, 0.0f, 0.0f); //"senkrecht zur Planetenachse"
|
/* glRotatef((ROT_FACTOR_MOON * day), 1.0f, 0.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
||||||
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
||||||
glTranslatef(0.0f, 2.0f, 0.0f);
|
glTranslatef(0.0f, 2.0f, 0.0f);
|
||||||
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
//Moon2
|
/* Moon2 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(0.0f, 1.0f, 1.0f);
|
glColor3f(0.0f, 1.0f, 1.0f);
|
||||||
//glRotatef((ROT_FACTOR_MOON * day), 1.0f, 0.0f, 0.0f); //"senkrecht zur Planetenachse"
|
/* glRotatef((ROT_FACTOR_MOON * day), 1.0f, 0.0f, 0.0f); [> "senkrecht zur Planetenachse" <] */
|
||||||
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
||||||
glTranslatef(0.0f, -2.0f, 0.0f);
|
glTranslatef(0.0f, -2.0f, 0.0f);
|
||||||
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
||||||
@ -167,7 +187,7 @@ static void draw_Planet_2(void)
|
|||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
const float moon_pos_fac = 2.5;
|
const float moon_pos_fac = 2.5;
|
||||||
|
|
||||||
//Rotate around the sun
|
/* Rotate around the sun */
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
||||||
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
glRotatef(90, 1.0f, 0.0f, 0.0f);
|
||||||
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_PLANET_SUN * day), 0.0f, 0.0f, 1.0f);
|
||||||
@ -175,13 +195,14 @@ static void draw_Planet_2(void)
|
|||||||
|
|
||||||
glColor3f(0.0f, 0.0f, 1.0f);
|
glColor3f(0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
//A rotation (full 360°) once a day is much too fast you woulden'd see a thing
|
/* A rotation (full 360°) once a day is much
|
||||||
|
* too fast you woulden'd see a thing */
|
||||||
const int rot_fac_day = 15;
|
const int rot_fac_day = 15;
|
||||||
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, 1.0f);
|
||||||
glutWireSphere(1.3f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
glutWireSphere(1.3f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
||||||
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, -1.0f);
|
glRotatef((ROT_FACTOR_PLANET * day) / rot_fac_day, 0.0f, 0.0f, -1.0f);
|
||||||
|
|
||||||
//Moon3
|
/* Moon3 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(1.0f, 1.0f, 1.0f);
|
glColor3f(1.0f, 1.0f, 1.0f);
|
||||||
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
||||||
@ -190,7 +211,7 @@ static void draw_Planet_2(void)
|
|||||||
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
//Moon4
|
/* Moon4 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(1.0f, 0.0f, 1.0f);
|
glColor3f(1.0f, 0.0f, 1.0f);
|
||||||
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
||||||
@ -199,7 +220,7 @@ static void draw_Planet_2(void)
|
|||||||
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
glutWireSphere(0.1f, XY_WIRE_COUNT, XY_WIRE_COUNT);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
//Moon5
|
/* Moon5 */
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glColor3f(1.0f, 0.0f, 0.0f);
|
glColor3f(1.0f, 0.0f, 0.0f);
|
||||||
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
glRotatef((ROT_FACTOR_MOON * day), 0.0f, 0.0f, 1.0f);
|
||||||
@ -219,7 +240,7 @@ void display(void)
|
|||||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
|
||||||
draw_Sun();
|
draw_obj(0, 0, 0);
|
||||||
draw_Planet_1();
|
draw_Planet_1();
|
||||||
draw_Planet_2();
|
draw_Planet_2();
|
||||||
|
|
||||||
@ -245,9 +266,11 @@ void display(void)
|
|||||||
/**
|
/**
|
||||||
* Sets the initial values to start the program.
|
* Sets the initial values to start the program.
|
||||||
*/
|
*/
|
||||||
void init(HE_obj *myobj)
|
void init(char const * const filename)
|
||||||
{
|
{
|
||||||
obj = myobj;
|
obj = read_obj_file(filename);
|
||||||
|
if (!obj)
|
||||||
|
ABORT("Failed to read object file \"%s\"!", filename);
|
||||||
day = 0;
|
day = 0;
|
||||||
year = 0;
|
year = 0;
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
@ -303,12 +326,9 @@ void animate()
|
|||||||
* press T to decrease the day
|
* press T to decrease the day
|
||||||
* press j to increase the year
|
* press j to increase the year
|
||||||
* press J to decrease the year
|
* press J to decrease the year
|
||||||
* press x to rotate the whole scene in x direction
|
* press x to rotate the middle object in x direction
|
||||||
* press X to rotate the whole scene in -x direction
|
* press y to rotate the middle object in y direction
|
||||||
* press y to rotate the whole scene in y direction
|
* press c to rotate the middle object in z direction
|
||||||
* press Y to rotate the whole scene in -y direction
|
|
||||||
* press c to rotate the whole scene in z direction
|
|
||||||
* press C to rotate the whole scene in -z direction
|
|
||||||
* press w to translate the whole scene in y direction
|
* press w to translate the whole scene in y direction
|
||||||
* press s 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 a to translate the whole scene in -x direction
|
||||||
@ -340,39 +360,15 @@ void keyboard(unsigned char key, int x, int y)
|
|||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
draw_obj(1, 0, 0);
|
||||||
glRotatef(1, 1.0f, 0.0f, 0.0f);
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
case 'y':
|
case 'y':
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
draw_obj(0, 1, 0);
|
||||||
glRotatef(1, 0.0f, 1.0f, 0.0f);
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
draw_obj(0, 0, 1);
|
||||||
glRotatef(1, 0.0f, 0.0f, 1.0f);
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'X':
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
|
||||||
glRotatef(1, -1.0f, 0.0f, 0.0f);
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'Y':
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
|
||||||
glRotatef(1, 0.0f, -1.0f, 0.0f);
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
||||||
glutPostRedisplay();
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z);
|
|
||||||
glRotatef(1, 0.0f, 0.0f, -1.0f);
|
|
||||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
break;
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
#include <GL/glu.h>
|
#include <GL/glu.h>
|
||||||
|
|
||||||
|
|
||||||
void init(HE_obj *myobj);
|
void init(char const * const filename);
|
||||||
void display(void);
|
void display(void);
|
||||||
void reshape(GLsizei w, GLsizei h);
|
void reshape(GLsizei w, GLsizei h);
|
||||||
void animate();
|
void animate();
|
||||||
|
13
main.c
13
main.c
@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "gl_draw.h"
|
#include "gl_draw.h"
|
||||||
#include "filereader.h"
|
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
@ -37,21 +36,15 @@ char const * const helptext = "Usage: drow-engine <file.obj>\n";
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
HE_obj *obj = NULL;
|
|
||||||
|
|
||||||
int windowX;
|
int windowX;
|
||||||
int windowY;
|
int windowY;
|
||||||
int screenX;
|
int screenX;
|
||||||
int screenY;
|
int screenY;
|
||||||
float factor = 0.80; // percent of screen
|
float factor = 0.80; // percent of screen
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc == 1) {
|
||||||
obj = read_obj_file(argv[1]);
|
|
||||||
print_vertices(obj);
|
|
||||||
print_edges(obj);
|
|
||||||
print_faces(obj);
|
|
||||||
} else {
|
|
||||||
printf("%s", helptext);
|
printf("%s", helptext);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
glutInit(&argc, argv);
|
glutInit(&argc, argv);
|
||||||
@ -65,7 +58,7 @@ int main(int argc, char *argv[])
|
|||||||
glutInitWindowSize(windowX, windowY);
|
glutInitWindowSize(windowX, windowY);
|
||||||
glutCreateWindow(filename);
|
glutCreateWindow(filename);
|
||||||
|
|
||||||
init(obj);
|
init(argv[1]);
|
||||||
|
|
||||||
glutKeyboardFunc(keyboard);
|
glutKeyboardFunc(keyboard);
|
||||||
glutReshapeFunc(reshape);
|
glutReshapeFunc(reshape);
|
||||||
|
Loading…
Reference in New Issue
Block a user