changeset 22:9a34b00c0659

Text printing implemented
author Josef "Jeff" Sipek <jeffpc@optonline.net>
date Sat, 03 Dec 2005 02:35:49 -0500
parents d51cb04bdbee
children c5088395d545
files pentagon.sh render.c
diffstat 2 files changed, 65 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pentagon.sh	Sat Dec 03 00:36:15 2005 -0500
+++ b/pentagon.sh	Sat Dec 03 02:35:49 2005 -0500
@@ -16,6 +16,7 @@
 for x in `seq 3600`; do
 	echo "CLEAR" > $PIPE
 	echo "POLY " `echo -e "$OMEGA 0 0\n$PT1\n$PT2\n$PT3\n$PT4\n$PT5" | ./rotate | ./transform` > $PIPE
+	echo "TEXT 0 0 0 320 400 0 The Pentagon!" > $PIPE
 	echo "FLUSH" > $PIPE
 	OMEGA=`echo "$OMEGA+$INC" | bc`
 	sleep 0.01
--- a/render.c	Sat Dec 03 00:36:15 2005 -0500
+++ b/render.c	Sat Dec 03 02:35:49 2005 -0500
@@ -16,6 +16,9 @@
 #define BUF_SIZE	1024
 #define TIMER_PERIOD	10
 
+#define WIDTH		800
+#define HEIGHT		800
+
 #define PIPE_IN		"/tmp/3de.in"
 #define PIPE_OUT	"/tmp/3de.out"
 
@@ -42,12 +45,12 @@
 	return 0;
 }
 
-char* update_ptr(char* buf)
+char* update_ptr(char* buf, int n)
 {
 	int i;
 	int on_num;
 
-	for(i=0, on_num=0; i<6; buf++) {
+	for(i=0, on_num=0; i<n; buf++) {
 		if (!valid_digit_char(*buf) && on_num) {
 			on_num = 0;
 			i++;
@@ -58,6 +61,61 @@
 	return buf;
 }
 
+void setOrthographicProjection()
+{
+	// switch to projection mode
+	glMatrixMode(GL_PROJECTION);
+	// save previous matrix which contains the 
+	//settings for the perspective projection
+	glPushMatrix();
+	// reset matrix
+	glLoadIdentity();
+	// set a 2D orthographic projection
+	gluOrtho2D(0, WIDTH, 0, HEIGHT);
+	// invert the y axis, down is positive
+	glScalef(1, -1, 1);
+	// mover the origin from the bottom left corner
+	// to the upper left corner
+	glTranslatef(0, -HEIGHT, 0);
+	glMatrixMode(GL_MODELVIEW);
+}
+
+void resetPerspectiveProjection()
+{
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+	glMatrixMode(GL_MODELVIEW);
+}
+
+void draw_text(char *buf)
+{
+	unsigned int i;
+	float x, y ,z;
+	float c[3];
+
+	buf += 5;
+	
+	if (sscanf(buf, "%f %f %f %f %f %f", &c[0], &c[1], &c[2], &x, &y, &z)!=6)
+		return;
+	
+	// set up drawing screen
+	setOrthographicProjection();
+	glPushMatrix();
+	glLoadIdentity();
+	
+	glColor3f (c[0], c[1], c[2]);
+	glRasterPos3f(x, y, z);
+	buf = update_ptr(buf, 6);
+	
+	printf("about to print: \"%s\" @ %f %f %f\n", buf,x,y,z);
+	for (i = 0; i < strlen(buf); i++)
+		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, buf[i]);
+	
+	// restore
+	glPopMatrix();
+	resetPerspectiveProjection();
+};
+
 void draw_poly(char* buf)
 {
 	float c1, c2, c3, f1, f2, f3;
@@ -76,7 +134,7 @@
 			break;
 		glColor3f(c1, c2, c3);
 		glVertex3f(f1, f2, f3);
-		buf = update_ptr(buf);
+		buf = update_ptr(buf, 6);
 	}
 	glEnd();
 
@@ -188,6 +246,8 @@
 		rot_cam(buf);
 	else if (!strncmp(buf, "TRANSCAM", 8))
 		trans_cam(buf);
+	else if (!strncmp(buf, "TEXT", 4))
+		draw_text(buf);
 	else if (strlen(buf)) {
 		printf("unknown command\n");
 		fflush(stdout);
@@ -279,7 +339,7 @@
 
 	glutInit(&argc,argv);
 	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
-	glutInitWindowSize(800,800);
+	glutInitWindowSize(WIDTH, HEIGHT);
 	glutInitWindowPosition(0,0);
 	glutCreateWindow("3D Engine");
 	glEnable(GL_DEPTH_TEST);