changeset 19:4722afb8751c

Added rotation and translation
author Josef "Jeff" Sipek <jeffpc@optonline.net>
date Sat, 03 Dec 2005 00:35:05 -0500
parents 22df18cf00c3
children ebc55ce699ec
files render.c
diffstat 1 files changed, 65 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/render.c	Sat Dec 03 00:32:38 2005 -0500
+++ b/render.c	Sat Dec 03 00:35:05 2005 -0500
@@ -28,6 +28,13 @@
 
 void display(void);
 
+float cam_x;
+float cam_y;
+float cam_z;
+float cam_ax;
+float cam_ay;
+float cam_az;
+
 int valid_digit_char(char c)
 {
 	if (((c >= '0') && (c <= '9')) || (c == '-') || (c == '+') || (c == '.'))
@@ -57,6 +64,12 @@
 
 	buf += 5;
 
+	glPushMatrix();
+	glRotatef(cam_ax, 1.0, 0.0, 0.0);
+	glRotatef(cam_ay, 0.0, 1.0, 0.0);
+	glRotatef(cam_az, 0.0, 0.0, 1.0);
+	glTranslatef(cam_x, cam_y, cam_z);
+
 	glBegin(GL_POLYGON);
 	while(1) {
 		if (sscanf(buf, "%f %f %f %f %f %f", &c1, &c2, &c3, &f1, &f2, &f3) != 6)
@@ -66,6 +79,36 @@
 		buf = update_ptr(buf);
 	}
 	glEnd();
+
+	glPopMatrix();
+}
+
+void rot_cam(char* buf)
+{
+	float x,y,z;
+
+	buf += 7;
+
+	if (sscanf(buf, "%f %f %f", &x, &y, &z) != 3)
+		return;
+
+	cam_ax += x;
+	cam_ay += y;
+	cam_az += z;
+}
+
+void trans_cam(char* buf)
+{
+	float x,y,z;
+
+	buf += 9;
+
+	if (sscanf(buf, "%f %f %f", &x, &y, &z) != 3)
+		return;
+
+	cam_x += x;
+	cam_y += y;
+	cam_z += z;
 }
 
 void __key(int c, int x, int y)
@@ -141,6 +184,10 @@
 		glutSwapBuffers();
 	} else if (!strncmp(buf, "POLY", 4))
 		draw_poly(buf);
+	else if (!strncmp(buf, "ROTCAM", 6))
+		rot_cam(buf);
+	else if (!strncmp(buf, "TRANSCAM", 8))
+		trans_cam(buf);
 	else if (strlen(buf)) {
 		printf("unknown command\n");
 		fflush(stdout);
@@ -163,8 +210,23 @@
 	/* box as cube of side 2 centered at origin */
 	/* This is default view and these statement could be removed */
 	glMatrixMode (GL_PROJECTION);
-	glLoadIdentity ();
-	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
+
+	//sets up the projection matrix for a perspective transform
+	gluPerspective( 90,     //view angle
+			1.0,    //aspect ratio
+			0.0,   //near clip
+			200.0); //far clip
+	glMatrixMode(GL_MODELVIEW);
+
+	/*glLoadIdentity ();
+	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);*/
+
+	cam_x = 0.0;
+	cam_y = 0.0;
+	cam_z = 0.0;
+	cam_ax = 0.0;
+	cam_ay = 0.0;
+	cam_az = 0.0;
 }
 
 #define die() __die(__FILE__, __LINE__);
@@ -216,7 +278,7 @@
 	atexit(goodbye);
 
 	glutInit(&argc,argv);
-	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
+	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
 	glutInitWindowSize(800,800);
 	glutInitWindowPosition(0,0);
 	glutCreateWindow("3D Engine");