KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > opengl > GLCanvas


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.opengl;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.widgets.*;
15 import org.eclipse.swt.internal.win32.*;
16 import org.eclipse.swt.internal.opengl.win32.*;
17
18 /**
19  * GLCanvas is a widget capable of displaying OpenGL content.
20  *
21  * @since 3.2
22  */

23
24 public class GLCanvas extends Canvas {
25     int context;
26     int pixelFormat;
27
28 /**
29  * Create a GLCanvas widget using the attributes described in the GLData
30  * object provided.
31  *
32  * @param parent a composite widget
33  * @param style the bitwise OR'ing of widget styles
34  * @param data the requested attributes of the GLCanvas
35  *
36  * @exception IllegalArgumentException
37  * <ul><li>ERROR_NULL_ARGUMENT when the data is null
38  * <li>ERROR_UNSUPPORTED_DEPTH when the requested attributes cannot be provided</ul>
39  * </ul>
40  */

41 public GLCanvas (Composite parent, int style, GLData data) {
42     super (parent, style);
43     if (data == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
44     PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR ();
45     pfd.nSize = (short) PIXELFORMATDESCRIPTOR.sizeof;
46     pfd.nVersion = 1;
47     pfd.dwFlags = WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL;
48     pfd.dwLayerMask = WGL.PFD_MAIN_PLANE;
49     pfd.iPixelType = (byte) WGL.PFD_TYPE_RGBA;
50     if (data.doubleBuffer) pfd.dwFlags |= WGL.PFD_DOUBLEBUFFER;
51     if (data.stereo) pfd.dwFlags |= WGL.PFD_STEREO;
52     pfd.cRedBits = (byte) data.redSize;
53     pfd.cGreenBits = (byte) data.greenSize;
54     pfd.cBlueBits = (byte) data.blueSize;
55     pfd.cAlphaBits = (byte) data.alphaSize;
56     pfd.cDepthBits = (byte) data.depthSize;
57     pfd.cStencilBits = (byte) data.stencilSize;
58     pfd.cAccumRedBits = (byte) data.accumRedSize;
59     pfd.cAccumGreenBits = (byte) data.accumGreenSize;
60     pfd.cAccumBlueBits = (byte) data.accumBlueSize;
61     pfd.cAccumAlphaBits = (byte) data.accumAlphaSize;
62     pfd.cAccumBits = (byte) (pfd.cAccumRedBits + pfd.cAccumGreenBits + pfd.cAccumBlueBits + pfd.cAccumAlphaBits);
63     
64     //FIXME - use wglChoosePixelFormatARB
65
// if (data.sampleBuffers > 0) {
66
// wglAttrib [pos++] = WGL.WGL_SAMPLE_BUFFERS_ARB;
67
// wglAttrib [pos++] = data.sampleBuffers;
68
// }
69
// if (data.samples > 0) {
70
// wglAttrib [pos++] = WGL.WGL_SAMPLES_ARB;
71
// wglAttrib [pos++] = data.samples;
72
// }
73

74     int hDC = OS.GetDC (handle);
75     pixelFormat = WGL.ChoosePixelFormat (hDC, pfd);
76     if (pixelFormat == 0 || !WGL.SetPixelFormat (hDC, pixelFormat, pfd)) {
77         OS.ReleaseDC (handle, hDC);
78         dispose ();
79         SWT.error (SWT.ERROR_UNSUPPORTED_DEPTH);
80     }
81     context = WGL.wglCreateContext (hDC);
82     if (context == 0) {
83         OS.ReleaseDC (handle, hDC);
84         SWT.error (SWT.ERROR_NO_HANDLES);
85     }
86     OS.ReleaseDC (handle, hDC);
87     //FIXME- share lists
88
//if (share != null) WGL.wglShareLists (context, share.context);
89

90     Listener listener = new Listener () {
91         public void handleEvent (Event event) {
92             switch (event.type) {
93                 case SWT.Dispose:
94                     WGL.wglDeleteContext (context);
95                     break;
96             }
97         }
98     };
99     addListener (SWT.Dispose, listener);
100 }
101
102 /**
103  * Returns a GLData object describing the created context.
104  *
105  * @return GLData description of the OpenGL context attributes
106  * @exception SWTException <ul>
107  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
108  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
109  * </ul>
110  */

111 public GLData getGLData () {
112     checkWidget ();
113     GLData data = new GLData ();
114     PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR ();
115     pfd.nSize = (short) PIXELFORMATDESCRIPTOR.sizeof;
116     int hDC = OS.GetDC (handle);
117     WGL.DescribePixelFormat (hDC, pixelFormat, PIXELFORMATDESCRIPTOR.sizeof, pfd);
118     OS.ReleaseDC (handle, hDC);
119     data.doubleBuffer = (pfd.dwFlags & WGL.PFD_DOUBLEBUFFER) != 0;
120     data.stereo = (pfd.dwFlags & WGL.PFD_STEREO) != 0;
121     data.redSize = pfd.cRedBits;
122     data.greenSize = pfd.cGreenBits;
123     data.blueSize = pfd.cBlueBits;
124     data.alphaSize = pfd.cAlphaBits;
125     data.depthSize = pfd.cDepthBits;
126     data.stencilSize = pfd.cStencilBits;
127     data.accumRedSize = pfd.cAccumRedBits;
128     data.accumGreenSize = pfd.cAccumGreenBits;
129     data.accumBlueSize = pfd.cAccumBlueBits;
130     data.accumAlphaSize = pfd.cAccumAlphaBits;
131     return data;
132 }
133
134 /**
135  * Returns a boolean indicating whether the receiver's OpenGL context
136  * is the current context.
137  *
138  * @return true if the receiver holds the current OpenGL context,
139  * false otherwise
140  * @exception SWTException <ul>
141  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
142  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
143  * </ul>
144  */

145 public boolean isCurrent () {
146     checkWidget ();
147     return WGL.wglGetCurrentContext () == context;
148 }
149
150 /**
151  * Sets the OpenGL context associated with this GLCanvas to be the
152  * current GL context.
153  *
154  * @exception SWTException <ul>
155  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
156  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
157  * </ul>
158  */

159 public void setCurrent () {
160     checkWidget ();
161     if (WGL.wglGetCurrentContext () == context) return;
162     int hDC = OS.GetDC (handle);
163     WGL.wglMakeCurrent (hDC, context);
164     OS.ReleaseDC (handle, hDC);
165 }
166
167 /**
168  * Swaps the front and back color buffers.
169  *
170  * @exception SWTException <ul>
171  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
172  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
173  * </ul>
174  */

175 public void swapBuffers () {
176     checkWidget ();
177     int hDC = OS.GetDC (handle);
178     WGL.SwapBuffers (hDC);
179     OS.ReleaseDC (handle, hDC);
180 }
181 }
182
Popular Tags