0
|
1 // System & parser
|
|
2 #include <string.h>
|
|
3 #include <stdlib.h>
|
|
4 #include <errno.h>
|
|
5 #define _GNU_SOURCE
|
|
6 #include <stdio.h>
|
|
7 #include <sys/types.h>
|
|
8 #include <sys/stat.h>
|
|
9 #include <fcntl.h>
|
|
10 #include <unistd.h>
|
|
11
|
|
12 // OpenGL
|
|
13 #include "GL/glut.h"
|
|
14
|
|
15 #define BUF_SIZE 1024
|
|
16
|
|
17 int fd;
|
|
18 //FILE* f;
|
|
19 char *buf, *ptr;
|
|
20
|
|
21 void draw_poly(char* buf)
|
|
22 {
|
|
23 float f1, f2, f3;
|
|
24
|
|
25 buf += 5;
|
|
26
|
|
27 glBegin(GL_POLYGON);
|
|
28 while(1) {
|
|
29 if (sscanf(buf, "%f %f %f", &f1, &f2, &f3) != 3)
|
|
30 break;
|
|
31 printf("using color: %f %f %f\n", f1, f2, f3);
|
|
32 glColor3f(f1, f2, f3);
|
|
33
|
|
34 if (sscanf(buf, "%f %f %f", &f1, &f2, &f3) != 3)
|
|
35 break;
|
|
36 printf("using vertex: %f %f %f\n", f1, f2, f3);
|
|
37 glVertex3f(f1, f2, f3);
|
|
38 }
|
|
39 glEnd();
|
|
40 }
|
|
41
|
|
42 void display(void)
|
|
43 {
|
|
44 ssize_t r;
|
|
45
|
|
46 do {
|
|
47 if ((ptr) >= (buf + BUF_SIZE))
|
|
48 break;
|
|
49
|
|
50 r = read(fd, ptr, 1);
|
|
51
|
|
52 if ((r == -1) || (r == 0) || (*ptr == '\n')) {
|
|
53 *ptr = '\0';
|
|
54 break;
|
|
55 }
|
|
56
|
|
57 ptr += r;
|
|
58 } while(1);
|
|
59
|
|
60 printf("buf = \"%s\"\n", buf);
|
|
61 fflush(stdout);
|
|
62
|
|
63 /* if (!strncmp(buf, "CLEAR", 5)) {
|
|
64 printf("clearing..\n");
|
|
65 fflush(stdout);*/
|
|
66 glClear(GL_COLOR_BUFFER_BIT);
|
|
67 /* } else if (!strncmp(buf, "FLUSH", 5)) {
|
|
68 printf("flushing..\n"); */
|
|
69 glBegin(GL_POLYGON);
|
|
70 glVertex3f(-0.5, -0.5, 0.5);
|
|
71 glVertex3f(-0.5, 0.5, 0.5);
|
|
72 glVertex3f(0.5, 0.5, 0.5);
|
|
73 glEnd();
|
|
74 glFlush();
|
|
75 /* } else if (!strncmp(buf, "POLY", 4))
|
|
76 draw_poly(buf);*/
|
|
77 /* else {
|
|
78 printf("unknown command\n");
|
|
79 fflush(stdout);
|
|
80 }*/
|
|
81
|
|
82 ptr = buf;
|
|
83
|
|
84 /* clear window */
|
|
85
|
|
86 /* draw unit square polygon */
|
|
87 /*glBegin(GL_POLYGON);
|
|
88 glVertex2f(-0.5, -0.5);
|
|
89 glVertex2f(-0.5, 0.5);
|
|
90 glVertex2f(0.5, 0.5);
|
|
91 glVertex2f(0.5, -0.5);
|
|
92 glEnd();*/
|
|
93
|
|
94 /* flush GL buffers */
|
|
95 }
|
|
96
|
|
97
|
|
98 void init()
|
|
99 {
|
|
100 /* set clear color to black */
|
|
101 glClearColor (0.0, 0.0, 0.0, 0.0);
|
|
102
|
|
103 /* set fill color to white */
|
|
104 glColor3f(1.0, 1.0, 1.0);
|
|
105
|
|
106 /* set up standard orthogonal view with clipping */
|
|
107 /* box as cube of side 2 centered at origin */
|
|
108 /* This is default view and these statement could be removed */
|
|
109 glMatrixMode (GL_PROJECTION);
|
|
110 glLoadIdentity ();
|
|
111 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
|
|
112 }
|
|
113
|
|
114 #define die() __die(__FILE__, __LINE__);
|
|
115
|
|
116 void __die(char* f, int l)
|
|
117 {
|
|
118 fprintf(stderr, "Error encountered in %s:%d: %s (%d)\n", f, l, strerror(errno), errno);
|
|
119 }
|
|
120
|
|
121 int main(int argc, char** argv)
|
|
122 {
|
|
123 unlink("/tmp/3de.pipe");
|
|
124
|
|
125 if ((buf = (char*) malloc(sizeof(char)*BUF_SIZE)) == NULL) {
|
|
126 die();
|
|
127 }
|
|
128 ptr = buf;
|
|
129
|
|
130 if (mkfifo("/tmp/3de.pipe", 0777) == -1) {
|
|
131 die();
|
|
132 }
|
|
133
|
|
134 if ((fd = open("/tmp/3de.pipe", O_RDONLY | O_NONBLOCK | O_NDELAY)) == -1) {
|
|
135 die();
|
|
136 }
|
|
137
|
|
138 /*if (!(f = fdopen(fd, "r"))) {
|
|
139 die();
|
|
140 }*/
|
|
141
|
|
142 glutInit(&argc,argv);
|
|
143 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
|
|
144 glutInitWindowSize(800,600);
|
|
145 glutInitWindowPosition(0,0);
|
|
146 glutCreateWindow("3D Engine");
|
|
147 glutDisplayFunc(display);
|
|
148 init();
|
|
149 glutMainLoop();
|
|
150
|
|
151 close(fd);
|
|
152
|
|
153 return 0;
|
|
154 }
|
|
155
|