Mercurial > 3de
changeset 0:443290507ab9
some minimal code
author | jsipek@huey.fsl.cs.sunysb.edu |
---|---|
date | Wed, 23 Nov 2005 23:16:20 -0400 |
parents | |
children | 88acf3d645e3 |
files | .hgignore Makefile render.c sample.scn |
diffstat | 4 files changed, 165 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Wed Nov 23 23:16:20 2005 -0400 @@ -0,0 +1,2 @@ +\.swp$ +^render$
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Wed Nov 23 23:16:20 2005 -0400 @@ -0,0 +1,5 @@ +all: + gcc render.c -o render -I/usr/X11R6/include/ -L/usr/X11R6/lib -lX11 -lXi -lXmu -lglut -lGL -lGLU -g -Wall + +clean: + rm -f render *~ *.o a.out
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/render.c Wed Nov 23 23:16:20 2005 -0400 @@ -0,0 +1,155 @@ +// 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 fd; +//FILE* f; +char *buf, *ptr; + +void draw_poly(char* buf) +{ + float f1, f2, f3; + + buf += 5; + + glBegin(GL_POLYGON); + while(1) { + if (sscanf(buf, "%f %f %f", &f1, &f2, &f3) != 3) + break; + printf("using color: %f %f %f\n", f1, f2, f3); + glColor3f(f1, f2, f3); + + if (sscanf(buf, "%f %f %f", &f1, &f2, &f3) != 3) + break; + printf("using vertex: %f %f %f\n", f1, f2, f3); + glVertex3f(f1, f2, f3); + } + glEnd(); +} + +void display(void) +{ + ssize_t r; + + do { + if ((ptr) >= (buf + BUF_SIZE)) + break; + + r = read(fd, 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)) { + printf("clearing..\n"); + fflush(stdout);*/ + glClear(GL_COLOR_BUFFER_BIT); +/* } else if (!strncmp(buf, "FLUSH", 5)) { + printf("flushing..\n"); */ + glBegin(GL_POLYGON); + glVertex3f(-0.5, -0.5, 0.5); + glVertex3f(-0.5, 0.5, 0.5); + glVertex3f(0.5, 0.5, 0.5); + glEnd(); + glFlush(); +/* } else if (!strncmp(buf, "POLY", 4)) + draw_poly(buf);*/ + /* else { + printf("unknown command\n"); + fflush(stdout); + }*/ + + ptr = buf; + + /* clear window */ + + /* draw unit square polygon */ + /*glBegin(GL_POLYGON); + glVertex2f(-0.5, -0.5); + glVertex2f(-0.5, 0.5); + glVertex2f(0.5, 0.5); + glVertex2f(0.5, -0.5); + glEnd();*/ + + /* flush GL buffers */ +} + + +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) +{ + unlink("/tmp/3de.pipe"); + + if ((buf = (char*) malloc(sizeof(char)*BUF_SIZE)) == NULL) { + die(); + } + ptr = buf; + + if (mkfifo("/tmp/3de.pipe", 0777) == -1) { + die(); + } + + if ((fd = open("/tmp/3de.pipe", O_RDONLY | O_NONBLOCK | O_NDELAY)) == -1) { + die(); + } + + /*if (!(f = fdopen(fd, "r"))) { + die(); + }*/ + + glutInit(&argc,argv); + glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(800,600); + glutInitWindowPosition(0,0); + glutCreateWindow("3D Engine"); + glutDisplayFunc(display); + init(); + glutMainLoop(); + + close(fd); + + return 0; +} +