1 #include <X11/keysym.h>
5 #include <WINGs/WINGs.h>
6 #include <WINGs/WINGsP.h>
11 #define CONTENTMARGIN 30
20 float red
=252.0/256, green
=88.0/256, blue
=16.0/256;
21 float redb
=252.0/256, greenb
=242.0/256, blueb
=80.0/256;
23 int Attr
[] = { GLX_RGBA
,
35 glClearColor (256/256, 256/256, 256/256, 0.0);
36 glPolygonMode(GL_FRONT
, GL_FILL
);
37 glPolygonMode(GL_BACK
, GL_FILL
);
38 glEnable(GL_DEPTH_TEST
);
39 glShadeModel(GL_SMOOTH
);
41 glEnable(GL_LIGHTING
);
42 GLfloat ambientLight
[] = { 0.2f
, 0.2f
, 0.2f
, 1.0f
};
43 GLfloat diffuseLight
[] = { 0.8f
, 0.8f
, 0.8f
, 1.0f
};
44 GLfloat specularLight
[] = { 0.5f
, 0.5f
, 0.5f
, 1.0f
};
45 GLfloat position
[] = { 2.0f
, -0.1f
, 2.0f
, 1.0f
};
46 GLfloat position2
[] = { -2.0f
, -0.26f
, -4.0f
, 1.0f
};
47 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, ambientLight
);
48 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuseLight
);
49 //glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
50 glMateriali(GL_FRONT
, GL_SHININESS
, 98);
51 glLightfv(GL_LIGHT0
, GL_POSITION
, position2
);
52 //glLightfv(GL_LIGHT1, GL_POSITION, position2);
55 //glEnable(GL_LIGHT1);
56 glColorMaterial(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
);
57 //glColorMaterial(GL_FRONT, GL_SPECULAR);
58 glEnable(GL_COLOR_MATERIAL
);
59 glShadeModel(GL_SMOOTH
);
60 glCullFace( GL_BACK
);
61 glEnable( GL_CULL_FACE
);
63 glEnable(GL_POLYGON_SMOOTH
);
65 glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
66 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
67 glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);*/
71 void normvector(float a
, float aa
, float aaa
, float b
, float bb
, float bbb
, float c
, float cc
, float ccc
,float *result
){
72 float v1
[3];float v2
[3];float tmp
;
74 v1
[0]=(b
-a
);v1
[1]=(bb
-aa
);v1
[2]=(bbb
-aaa
);
75 v2
[0]=(b
-c
);v2
[1]=(bb
-cc
);v2
[2]=(bbb
-ccc
);
76 result
[0]=(v1
[1]*v2
[2]-v1
[2]*v2
[1]);
77 result
[1]=(v1
[2]*v2
[0]-v1
[0]*v2
[2]);
78 result
[2]=(v1
[0]*v2
[1]-v1
[1]*v2
[0]);
79 tmp
=sqrt(result
[0]*result
[0]+result
[1]*result
[1]+result
[2]*result
[2]);
86 void redraw(XEvent
* v
,void *xw
){
92 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
93 glPushMatrix(); glShadeModel(GL_SMOOTH
);
95 glRotatef(alpha
, 0, 1, 0);
96 if (alpha
> 360) alpha
=alpha
-360;
98 glBegin(GL_TRIANGLES
);
100 glColor3f(redb
,greenb
,blueb
);
101 normvector(-0.85f
, 0.0f
, 0.0f
,0.0f
, 0.0f
, 0.85f
,0.0f
, 0.6f
, 0.0f
,z
);
103 glVertex3f(-0.85f
, 0.0f
, 0.0f
);
104 glVertex3f(0.0f
, 0.0f
, 0.85f
);
105 glVertex3f(0.0f
, 0.6f
, 0.0f
);
107 normvector(0.0f
, 0.0f
,0.85f
,0.85f
, 0.0f
, 0.0f
,0.0f
, 0.60f
,0.0f
,z
);
109 glVertex3f(0.0f
, 0.0f
, 0.85f
);
110 glVertex3f(0.85f
, 0.0f
, 0.0f
);
111 glVertex3f(0.0f
, 0.6f
, 0.0f
);
113 glColor3f(red
,green
,blue
);
114 normvector(0.85f
, 0.0f
, 0.0f
,0.0f
, 0.0f
, -0.85f
,0.0f
, 0.6f
, 0.0f
,z
);
116 glVertex3f(0.85f
, 0.0f
, 0.0f
);
117 glVertex3f(0.0f
, 0.0f
, -0.85f
);
118 glVertex3f(0.0f
, 0.6f
, 0.0f
);
120 normvector(0.0f
, 0.0f
, -0.85f
,-0.85f
, 0.0f
, 0.0f
,0.0f
, 0.6f
, 0.0f
,z
);
122 glVertex3f(0.0f
, 0.0f
, -0.85f
);
123 glVertex3f(-0.85f
, 0.0f
, 0.0f
);
124 glVertex3f(0.0f
, 0.6f
, 0.0f
);
126 glColor3f(redb
,greenb
,blueb
);
127 normvector(-0.85f
, 0.0f
, 0.0f
,0.0f
, -1.0f
, 0.0f
,0.0f
, 0.0f
, 0.85f
,z
);
129 glVertex3f(-0.85f
, 0.0f
, 0.0f
);
130 glVertex3f(0.0f
, -1.0f
, 0.0f
);
131 glVertex3f(0.0f
, 0.0f
, 0.85f
);
133 normvector(0.0f
, 0.0f
, 0.85f
, 0.0f
, -1.0f
, 0.0f
,0.85f
, 0.0f
, 0.0f
,z
);
135 glVertex3f(0.0f
, 0.0f
, 0.85f
);
136 glVertex3f(0.0f
, -1.0f
, 0.0f
);
137 glVertex3f(0.85f
, 0.0f
, 0.0f
);
139 glColor3f(red
,green
,blue
);
140 normvector(0.85f
, 0.0f
, 0.0f
,0.0f
, -1.0f
, 0.0f
, 0.0f
, 0.0,-0.85f
,z
);
142 glVertex3f(0.85f
, 0.0f
, 0.0f
);
143 glVertex3f(0.0f
,-1.0f
, 0.0f
);
144 glVertex3f(0.0f
, 0.0f
, -0.85f
);
146 normvector(0.0f
, 0.0f
, -0.85f
,0.0f
, -1.0f
, 0.0f
, -0.85f
, 0.0f
,0.0f
,z
);
148 glVertex3f(0.0f
, 0.0f
, -0.85f
);
149 glVertex3f(0.0f
, -1.0f
, 0.0f
);
150 glVertex3f(-0.85f
, 0.0f
, 0.0f
);
155 glXSwapBuffers(display
, win
);
158 setsize(unsigned int width
, unsigned int height
)
160 glViewport(0, 0, width
, height
);
161 glMatrixMode(GL_PROJECTION
);
163 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
164 glMatrixMode (GL_MODELVIEW
);
167 void DoRotate(void *self
,void *xwindow
){
168 XEvent event
;int i
=0;
170 redraw(NULL
,(Window
*)xwindow
);
173 void redo(XEvent
* event
,void *xw
){
177 if (event
->xexpose
.count
!=0) break;
178 redraw(event
,&event
->xexpose
.window
);
180 case ConfigureNotify
: setsize(event
->xconfigure
.width
, event
->xconfigure
.height
); // assuming there will be an expose afterwards
185 void closeAll(WMWidget
*self
,void *data
){
186 WMDestroyWidget(self
);
190 static void resizeHandler(void *data
, WMNotification
*notif
){
191 struct couple
*tmp
;tmp
=(struct couple
*)data
;
192 WMSize size
= WMGetViewSize(WMWidgetView(tmp
->window
));
193 WMResizeWidget(tmp
->frame
, size
.width
-2*CONTENTMARGIN
, size
.height
-2*CONTENTMARGIN
);
196 void getargs(int argc
, char **argv
){
198 redb
=red
=(float)atoi(argv
[1])/256;
199 greenb
=green
=(float)atoi(argv
[2])/256;
200 blueb
=blue
=(float)atoi(argv
[3])/256;
203 redb
=(float)atoi(argv
[4])/256;
204 greenb
=(float)atoi(argv
[5])/256;
205 blueb
=(float)atoi(argv
[6])/256;
209 int main (int argc
, char **argv
){
217 /* Xlib and glX variables */
219 XVisualInfo
*xvVisualInfo
;
221 XSetWindowAttributes winAttr
;
222 GLXContext glXContext
;
225 WMInitializeApplication("GLWindow", &argc
, argv
);
226 display
= XOpenDisplay("");
227 screen
= WMCreateScreen(display
, DefaultScreen(display
));
229 if(!glXQueryExtension(display
, NULL
, NULL
)){wwarning("X server does not have GLX\n"); return 0; }
231 window
= WMCreateWindow(screen
, "Main");
232 WMResizeWidget(window
, CONTENTW
+2*CONTENTMARGIN
, CONTENTH
+2*CONTENTMARGIN
*CONTENTH
/CONTENTW
);
233 WMSetWindowAspectRatio(window
, CONTENTW
,CONTENTH
,CONTENTW
,CONTENTH
);
234 WMSetWindowCloseAction(window
, closeAll
, NULL
);
235 WMSetWindowTitle(window
,"GL Frame");
236 WMRealizeWidget(window
);
237 datacouple
.window
=window
;
238 WMSetViewNotifySizeChanges(WMWidgetView(window
), True
);
239 WMAddNotificationObserver(resizeHandler
, &datacouple
, WMViewSizeDidChangeNotification
, WMWidgetView(window
));
242 glframe
= WMCreateFrame(window
);
243 datacouple
.frame
=glframe
;
244 WMResizeWidget(glframe
, CONTENTW
, CONTENTH
);
245 WMMoveWidget(glframe
, CONTENTMARGIN
,CONTENTMARGIN
);
246 WMRealizeWidget(glframe
);
248 Button
=WMCreateButton(window
, WBTMomentaryPush
);
249 WMSetButtonAction(Button
, DoRotate
,&win
);
250 WMSetButtonText(Button
,"Turn");
251 WMMoveWidget(Button
, CONTENTMARGIN
,2);
252 WMRealizeWidget(Button
);
255 /* get the frame's X window value */
256 win
=W_VIEW_DRAWABLE(WMWidgetView(glframe
));
257 WMCreateEventHandler(WMWidgetView(glframe
), ExposureMask
|StructureNotifyMask
,redo
,&win
);
259 xvVisualInfo
= glXChooseVisual(display
, DefaultScreen(display
), Attr
);
260 if(xvVisualInfo
== NULL
) {wwarning("No visualinfo\n");return 0;}
262 cmColorMap
= XCreateColormap(display
,RootWindow(display
, DefaultScreen(display
)), xvVisualInfo
->visual
, AllocNone
);
264 winAttr
.colormap
= cmColorMap
;
265 winAttr
.border_pixel
= 0;
266 winAttr
.background_pixel
= 0;
267 winAttr
.event_mask
= ExposureMask
| ButtonPressMask
|StructureNotifyMask
| KeyPressMask
;
269 XChangeWindowAttributes(display
,win
,CWBorderPixel
| CWColormap
| CWEventMask
,&winAttr
);
270 glXContext
= glXCreateContext(display
, xvVisualInfo
, None
, True
);
271 if(!glXContext
) {wwarning("glX cannot create rendering context\n");return 0;}
273 glXMakeCurrent(display
, win
, glXContext
);
275 WMMapWidget(glframe
);
277 setsize(CONTENTW
,CONTENTH
);
280 WMScreenMainLoop(screen
);