3 #include <X11/keysym.h>
7 #include <WINGs/WINGs.h>
8 #include <WINGs/WINGsP.h>
13 #define CONTENTMARGIN 30
22 float red=252.0/256, green=88.0/256, blue=16.0/256;
23 float redb=252.0/256, greenb=242.0/256, blueb=80.0/256;
25 int Attr[] = { GLX_RGBA,
37 glClearColor (256/256, 256/256, 256/256, 0.0);
38 glPolygonMode(GL_FRONT, GL_FILL);
39 glPolygonMode(GL_BACK, GL_FILL);
40 glEnable(GL_DEPTH_TEST);
41 glShadeModel(GL_SMOOTH);
43 glEnable(GL_LIGHTING);
44 GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
45 GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
46 GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
47 GLfloat position[] = { 2.0f, -0.1f, 2.0f, 1.0f };
48 GLfloat position2[] = { -2.0f, -0.26f, -4.0f, 1.0f };
49 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
50 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
51 //glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
52 glMateriali(GL_FRONT, GL_SHININESS, 98);
53 glLightfv(GL_LIGHT0, GL_POSITION, position2);
54 //glLightfv(GL_LIGHT1, GL_POSITION, position2);
57 //glEnable(GL_LIGHT1);
58 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
59 //glColorMaterial(GL_FRONT, GL_SPECULAR);
60 glEnable(GL_COLOR_MATERIAL);
61 glShadeModel(GL_SMOOTH);
62 glCullFace( GL_BACK );
63 glEnable( GL_CULL_FACE );
65 glEnable(GL_POLYGON_SMOOTH);
67 glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
68 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
69 glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);*/
73 void normvector(float a, float aa, float aaa, float b, float bb, float bbb, float c, float cc, float ccc,float *result){
74 float v1[3];float v2[3];float tmp;
76 v1[0]=(b-a);v1[1]=(bb-aa);v1[2]=(bbb-aaa);
77 v2[0]=(b-c);v2[1]=(bb-cc);v2[2]=(bbb-ccc);
78 result[0]=(v1[1]*v2[2]-v1[2]*v2[1]);
79 result[1]=(v1[2]*v2[0]-v1[0]*v2[2]);
80 result[2]=(v1[0]*v2[1]-v1[1]*v2[0]);
81 tmp=sqrt(result[0]*result[0]+result[1]*result[1]+result[2]*result[2]);
88 void redraw(XEvent * v,void *xw){
94 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
95 glPushMatrix(); glShadeModel(GL_SMOOTH);
97 glRotatef(alpha, 0, 1, 0);
98 if (alpha > 360) alpha =alpha-360;
100 glBegin(GL_TRIANGLES);
102 glColor3f(redb,greenb,blueb);
103 normvector(-0.85f, 0.0f, 0.0f,0.0f, 0.0f, 0.85f,0.0f, 0.6f, 0.0f,z);
105 glVertex3f(-0.85f, 0.0f, 0.0f);
106 glVertex3f(0.0f, 0.0f, 0.85f);
107 glVertex3f(0.0f, 0.6f, 0.0f);
109 normvector(0.0f, 0.0f,0.85f,0.85f, 0.0f, 0.0f,0.0f, 0.60f,0.0f,z);
111 glVertex3f(0.0f, 0.0f, 0.85f);
112 glVertex3f(0.85f, 0.0f, 0.0f);
113 glVertex3f(0.0f, 0.6f, 0.0f);
115 glColor3f(red,green,blue);
116 normvector(0.85f, 0.0f, 0.0f,0.0f, 0.0f, -0.85f,0.0f, 0.6f, 0.0f,z);
118 glVertex3f(0.85f, 0.0f, 0.0f);
119 glVertex3f(0.0f, 0.0f, -0.85f);
120 glVertex3f(0.0f, 0.6f, 0.0f);
122 normvector(0.0f, 0.0f, -0.85f,-0.85f, 0.0f, 0.0f,0.0f, 0.6f, 0.0f,z);
124 glVertex3f(0.0f, 0.0f, -0.85f);
125 glVertex3f(-0.85f, 0.0f, 0.0f);
126 glVertex3f(0.0f, 0.6f, 0.0f);
128 glColor3f(redb,greenb,blueb);
129 normvector(-0.85f, 0.0f, 0.0f,0.0f, -1.0f, 0.0f,0.0f, 0.0f, 0.85f,z);
131 glVertex3f(-0.85f, 0.0f, 0.0f);
132 glVertex3f(0.0f, -1.0f, 0.0f);
133 glVertex3f(0.0f, 0.0f, 0.85f);
135 normvector(0.0f, 0.0f, 0.85f, 0.0f, -1.0f, 0.0f,0.85f, 0.0f, 0.0f ,z);
137 glVertex3f(0.0f, 0.0f, 0.85f);
138 glVertex3f(0.0f, -1.0f, 0.0f);
139 glVertex3f(0.85f, 0.0f, 0.0f);
141 glColor3f(red,green,blue);
142 normvector(0.85f, 0.0f, 0.0f,0.0f, -1.0f, 0.0f, 0.0f, 0.0,-0.85f ,z);
144 glVertex3f(0.85f, 0.0f, 0.0f);
145 glVertex3f(0.0f,-1.0f, 0.0f);
146 glVertex3f(0.0f, 0.0f, -0.85f);
148 normvector(0.0f, 0.0f, -0.85f,0.0f, -1.0f, 0.0f, -0.85f, 0.0f,0.0f ,z);
150 glVertex3f(0.0f, 0.0f, -0.85f);
151 glVertex3f(0.0f, -1.0f, 0.0f);
152 glVertex3f(-0.85f, 0.0f, 0.0f);
157 glXSwapBuffers(display, win);
160 setsize(unsigned int width, unsigned int height)
162 glViewport(0, 0, width, height);
163 glMatrixMode(GL_PROJECTION);
165 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
166 glMatrixMode (GL_MODELVIEW);
169 void DoRotate(void *self,void *xwindow){
170 XEvent event;int i=0;
172 redraw(NULL,(Window *)xwindow);
175 void redo(XEvent * event,void *xw){
179 if (event->xexpose.count!=0) break;
180 redraw(event,&event->xexpose.window);
182 case ConfigureNotify: setsize(event->xconfigure.width, event->xconfigure.height); // assuming there will be an expose afterwards
187 void closeAll(WMWidget *self,void *data){
188 WMDestroyWidget(self);
192 static void resizeHandler(void *data, WMNotification *notif){
193 struct couple *tmp;tmp=(struct couple *)data;
194 WMSize size = WMGetViewSize(WMWidgetView(tmp->window));
195 WMResizeWidget(tmp->frame, size.width -2*CONTENTMARGIN, size.height-2*CONTENTMARGIN);
198 void getargs(int argc, char **argv){
200 redb=red=(float)atoi(argv[1])/256;
201 greenb=green=(float)atoi(argv[2])/256;
202 blueb=blue=(float)atoi(argv[3])/256;
205 redb=(float)atoi(argv[4])/256;
206 greenb=(float)atoi(argv[5])/256;
207 blueb=(float)atoi(argv[6])/256;
211 int main (int argc, char **argv){
219 /* Xlib and glX variables */
221 XVisualInfo *xvVisualInfo;
223 XSetWindowAttributes winAttr;
224 GLXContext glXContext;
227 WMInitializeApplication("GLWindow", &argc, argv);
228 display = XOpenDisplay("");
229 screen = WMCreateScreen(display, DefaultScreen(display));
231 if(!glXQueryExtension(display, NULL, NULL)){wwarning("X server does not have GLX\n"); return 0; }
233 window = WMCreateWindow(screen, "Main");
234 WMResizeWidget(window, CONTENTW+2*CONTENTMARGIN, CONTENTH+2*CONTENTMARGIN*CONTENTH/CONTENTW);
235 WMSetWindowAspectRatio(window, CONTENTW,CONTENTH,CONTENTW,CONTENTH);
236 WMSetWindowCloseAction(window, closeAll, NULL);
237 WMSetWindowTitle(window,"GL Frame");
238 WMRealizeWidget(window);
239 datacouple.window=window;
240 WMSetViewNotifySizeChanges(WMWidgetView(window), True);
241 WMAddNotificationObserver(resizeHandler, &datacouple, WMViewSizeDidChangeNotification, WMWidgetView(window));
244 glframe = WMCreateFrame(window);
245 datacouple.frame=glframe;
246 WMResizeWidget(glframe, CONTENTW, CONTENTH);
247 WMMoveWidget(glframe, CONTENTMARGIN,CONTENTMARGIN);
248 WMRealizeWidget(glframe);
250 Button=WMCreateButton(window, WBTMomentaryPush);
251 WMSetButtonAction(Button, DoRotate,&win);
252 WMSetButtonText(Button,"Turn");
253 WMMoveWidget(Button, CONTENTMARGIN,2);
254 WMRealizeWidget(Button);
257 /* get the frame's X window value */
258 win =W_VIEW_DRAWABLE(WMWidgetView(glframe));
259 WMCreateEventHandler(WMWidgetView(glframe), ExposureMask|StructureNotifyMask,redo,&win);
261 xvVisualInfo = glXChooseVisual(display, DefaultScreen(display), Attr);
262 if(xvVisualInfo == NULL) {wwarning("No visualinfo\n");return 0;}
264 cmColorMap = XCreateColormap(display,RootWindow(display, DefaultScreen(display)), xvVisualInfo->visual, AllocNone);
266 winAttr.colormap = cmColorMap;
267 winAttr.border_pixel = 0;
268 winAttr.background_pixel = 0;
269 winAttr.event_mask = ExposureMask | ButtonPressMask |StructureNotifyMask| KeyPressMask;
271 XChangeWindowAttributes(display,win,CWBorderPixel | CWColormap | CWEventMask,&winAttr);
272 glXContext = glXCreateContext(display, xvVisualInfo, None, True);
273 if(!glXContext) {wwarning("glX cannot create rendering context\n");return 0;}
275 glXMakeCurrent(display, win, glXContext);
277 WMMapWidget(glframe);
279 setsize(CONTENTW,CONTENTH);
282 WMScreenMainLoop(screen);