view render.c @ 6:b06a7e9cda60

cleanup
author jsipek@huey.fsl.cs.sunysb.edu
date Thu, 24 Nov 2005 01:34:26 -0400
parents 02aee7371541
children e681fac7212c
line wrap: on
line source

// System & parser
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

//  OpenGL
#include "GL/glut.h"

#define BUF_SIZE 1024

int infd;
int outfd;
char *buf, *ptr;

void display(void);

int valid_digit_char(char c)
{
	if (((c >= '0') && (c <= '9')) || (c == '-') || (c == '+') || (c == '.'))
		return 1;
	return 0;
}

char* update_ptr(char* buf)
{
	int i;
	int on_num;

	for(i=0, on_num=0; i<6; buf++) {
		if (!valid_digit_char(*buf) && on_num) {
			on_num = 0;
			i++;
		} else if (valid_digit_char(*buf) && !on_num)
			on_num = 1;
	}

	return buf;
}

void draw_poly(char* buf)
{
	float c1, c2, c3, f1, f2, f3;

	buf += 5;

	glBegin(GL_POLYGON);
	while(1) {
		if (sscanf(buf, "%f %f %f %f %f %f", &c1, &c2, &c3, &f1, &f2, &f3) != 6)
			break;
		glColor3f(c1, c2, c3);
		glVertex3f(f1, f2, f3);
		buf = update_ptr(buf);
	}
	glEnd();
}

void key(unsigned char c, int x, int y)
{
	char b[16];

	printf("key pressed: %c, mouse at (%d,%d)\n", c, x, y);

	memset(b, 0, 16);
	snprintf(b, 16, "%c %d %d\n", c, x, y);
	write(outfd, b, 16);

	display();
}

void display(void)
{
	ssize_t r;

	do {
		if ((ptr) >= (buf + BUF_SIZE))
			break;

		r = read(infd, ptr, 1);

		if ((r == -1) || (r == 0) || (*ptr == '\n')) {
			*ptr = '\0';
			break;
		}

		ptr += r;
	} while(1);

	printf("buf = \"%s\"\n", buf);
	fflush(stdout);
		
		if (!strncmp(buf, "CLEAR", 5)) {
			glClear(GL_COLOR_BUFFER_BIT);
		} else if (!strncmp(buf, "FLUSH", 5)) {
			glFlush();
			glutSwapBuffers();
		} else if (!strncmp(buf, "POLY", 4))
			draw_poly(buf);
		else {
			printf("unknown command\n");
			fflush(stdout);
		}

	ptr = buf;
}

void init()
{
	/* set clear color to black */
	glClearColor (0.0, 0.0, 0.0, 0.0);

	/* set fill color to white */
	glColor3f(1.0, 1.0, 1.0);

	/* set up standard orthogonal view with clipping */
	/* 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);
}

#define die() __die(__FILE__, __LINE__);

void __die(char* f, int l)
{
	fprintf(stderr, "Error encountered in %s:%d: %s (%d)\n", f, l, strerror(errno), errno);
}

int main(int argc, char** argv)
{
#define PIPE_IN		"/tmp/3de.in"
#define PIPE_OUT	"/tmp/3de.out"

	unlink(PIPE_IN);
	unlink(PIPE_OUT);

	if ((buf = (char*) malloc(sizeof(char)*BUF_SIZE)) == NULL)
		die();
	ptr = buf;

	if (mkfifo(PIPE_IN, 0777) == -1)
		die();

	if (mkfifo(PIPE_OUT, 0777) == -1)
		die();

	if ((infd = open(PIPE_IN, O_RDWR | O_NONBLOCK | O_NDELAY)) == -1)
		die();

	if ((outfd = open(PIPE_OUT, O_RDWR | O_NONBLOCK | O_NDELAY)) == -1)
		die();

	glutInit(&argc,argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(800,600);
	glutInitWindowPosition(0,0);
	glutCreateWindow("3D Engine");
	glutDisplayFunc(display);
	glutKeyboardFunc(key);
	init();
	glutMainLoop();

	close(infd);
	close(outfd);

	return 0;
}