Mercurial > 3de
changeset 1:88acf3d645e3 draw_triangle
Draws a triangle from sample.scn!
author | jsipek@huey.fsl.cs.sunysb.edu |
---|---|
date | Thu, 24 Nov 2005 00:10:41 -0400 |
parents | 443290507ab9 |
children | f51d90d60df2 |
files | render.c sample.scn |
diffstat | 2 files changed, 69 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/render.c Wed Nov 23 23:16:20 2005 -0400 +++ b/render.c Thu Nov 24 00:10:41 2005 -0400 @@ -14,10 +14,35 @@ #define BUF_SIZE 1024 -int fd; -//FILE* f; +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<3; 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 f1, f2, f3; @@ -30,15 +55,30 @@ break; printf("using color: %f %f %f\n", f1, f2, f3); glColor3f(f1, f2, f3); + buf = update_ptr(buf); 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); + 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; @@ -47,7 +87,7 @@ if ((ptr) >= (buf + BUF_SIZE)) break; - r = read(fd, ptr, 1); + r = read(infd, ptr, 1); if ((r == -1) || (r == 0) || (*ptr == '\n')) { *ptr = '\0'; @@ -60,24 +100,24 @@ printf("buf = \"%s\"\n", buf); fflush(stdout); - /* if (!strncmp(buf, "CLEAR", 5)) { + if (!strncmp(buf, "CLEAR", 5)) { printf("clearing..\n"); - fflush(stdout);*/ + fflush(stdout); glClear(GL_COLOR_BUFFER_BIT); -/* } else if (!strncmp(buf, "FLUSH", 5)) { - printf("flushing..\n"); */ - glBegin(GL_POLYGON); + } 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(); + glEnd();*/ glFlush(); -/* } else if (!strncmp(buf, "POLY", 4)) - draw_poly(buf);*/ - /* else { + } else if (!strncmp(buf, "POLY", 4)) + draw_poly(buf); + else { printf("unknown command\n"); fflush(stdout); - }*/ + } ptr = buf; @@ -90,8 +130,6 @@ glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd();*/ - - /* flush GL buffers */ } @@ -120,24 +158,27 @@ int main(int argc, char** argv) { - unlink("/tmp/3de.pipe"); +#define PIPE_IN "/tmp/3de.in" +#define PIPE_OUT "/tmp/3de.out" - if ((buf = (char*) malloc(sizeof(char)*BUF_SIZE)) == NULL) { + unlink(PIPE_IN); + unlink(PIPE_OUT); + + if ((buf = (char*) malloc(sizeof(char)*BUF_SIZE)) == NULL) die(); - } ptr = buf; - if (mkfifo("/tmp/3de.pipe", 0777) == -1) { + if (mkfifo(PIPE_IN, 0777) == -1) die(); - } - if ((fd = open("/tmp/3de.pipe", O_RDONLY | O_NONBLOCK | O_NDELAY)) == -1) { + if (mkfifo(PIPE_OUT, 0777) == -1) die(); - } - /*if (!(f = fdopen(fd, "r"))) { + 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_SINGLE | GLUT_RGB); @@ -145,10 +186,12 @@ glutInitWindowPosition(0,0); glutCreateWindow("3D Engine"); glutDisplayFunc(display); + glutKeyboardFunc(key); init(); glutMainLoop(); - close(fd); + close(infd); + close(outfd); return 0; }