| 1 |
/* |
| 2 |
* *************************************************************************** |
| 3 |
* SG = < Socket Graphics > |
| 4 |
* Copyright (C) 1994-- Michael Holst |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* |
| 20 |
* rcsid="$Id: main.c,v 1.6 2010/08/12 04:56:32 fetk Exp $" |
| 21 |
* *************************************************************************** |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* *************************************************************************** |
| 26 |
* File: main.c |
| 27 |
* |
| 28 |
* Purpose: Driver program for testing OpenGL. |
| 29 |
* |
| 30 |
* Author: Michael Holst (Hacked the program found in the glXIntro manpage) |
| 31 |
* *************************************************************************** |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <math.h> |
| 38 |
|
| 39 |
#if defined(HAVE_X11) |
| 40 |
#include <GL/glx.h> |
| 41 |
#elif defined(HAVE_WIN32) |
| 42 |
#elif defined(HAVE_NEXT) |
| 43 |
#include <AppKit/AppKit.h> |
| 44 |
#include <GL/osmesa.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <GL/gl.h> |
| 48 |
#include <GL/glu.h> |
| 49 |
|
| 50 |
#if defined(HAVE_X11) |
| 51 |
extern Bool WaitForNotify(Display *d, XEvent *e, char *arg); |
| 52 |
static int gl_width=300; |
| 53 |
static int gl_height=300; |
| 54 |
#elif defined(HAVE_WIN32) |
| 55 |
#elif defined(HAVE_NEXT) |
| 56 |
static int gl_width=300; |
| 57 |
static int gl_height=300; |
| 58 |
#endif |
| 59 |
|
| 60 |
#if defined(HAVE_X11) |
| 61 |
static int attributeList[] = { |
| 62 |
GLX_RGBA, |
| 63 |
GLX_DOUBLEBUFFER, |
| 64 |
None |
| 65 |
}; |
| 66 |
Bool WaitForNotify(Display *d, XEvent *e, char *arg) { |
| 67 |
return (e->type == MapNotify) && (e->xmap.window == (Window)arg); |
| 68 |
} |
| 69 |
#elif defined(HAVE_WIN32) |
| 70 |
#elif defined(HAVE_NEXT) |
| 71 |
#endif |
| 72 |
|
| 73 |
int main(int argc, char **argv) { |
| 74 |
GLclampf c[4]; |
| 75 |
#if defined(HAVE_X11) |
| 76 |
Display *dpy; |
| 77 |
XVisualInfo *vi; |
| 78 |
Colormap cmap; |
| 79 |
XSetWindowAttributes swa; |
| 80 |
Window win; |
| 81 |
GLXContext ctx; |
| 82 |
XEvent event; |
| 83 |
#elif defined(HAVE_WIN32) |
| 84 |
#elif defined(HAVE_NEXT) |
| 85 |
OSMesaContext ctx; |
| 86 |
unsigned char *buffer; |
| 87 |
NSWindow *myWindow; |
| 88 |
NSView *myView; |
| 89 |
NSMenu *myMenu; |
| 90 |
NSBitmapImageRep *bitmap; |
| 91 |
char name[50]; |
| 92 |
NSRect GR; |
| 93 |
#endif |
| 94 |
|
| 95 |
#if defined(HAVE_X11) |
| 96 |
/* get a connection */ |
| 97 |
dpy = XOpenDisplay(0); |
| 98 |
|
| 99 |
/* get an appropriate visual */ |
| 100 |
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList); |
| 101 |
|
| 102 |
/* create a color map */ |
| 103 |
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, |
| 104 |
AllocNone); |
| 105 |
|
| 106 |
/* create a window */ |
| 107 |
swa.colormap = cmap; |
| 108 |
swa.border_pixel = 0; |
| 109 |
swa.event_mask = StructureNotifyMask; |
| 110 |
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, |
| 111 |
(unsigned int)gl_width, (unsigned int)gl_height, |
| 112 |
0, vi->depth, InputOutput, vi->visual, |
| 113 |
CWBorderPixel|CWColormap|CWEventMask, &swa); |
| 114 |
XMapWindow(dpy, win); |
| 115 |
XIfEvent(dpy, &event, WaitForNotify, (char*)win); |
| 116 |
|
| 117 |
#elif defined(HAVE_WIN32) |
| 118 |
#elif defined(HAVE_NEXT) |
| 119 |
|
| 120 |
[[NSAutoreleasePool alloc] init]; |
| 121 |
NSApp=[NSApplication sharedApplication]; |
| 122 |
|
| 123 |
/* Allocate the image buffer */ |
| 124 |
buffer = malloc( gl_width * gl_height * 4 ); |
| 125 |
|
| 126 |
bitmap = [[ NSBitmapImageRep alloc] initWithBitmapDataPlanes:&buffer |
| 127 |
pixelsWide:gl_width |
| 128 |
pixelsHigh:gl_height |
| 129 |
bitsPerSample:8 samplesPerPixel:4 |
| 130 |
hasAlpha:YES isPlanar:NO |
| 131 |
colorSpaceName:NSDeviceRGBColorSpace |
| 132 |
bytesPerRow:0 bitsPerPixel:0]; |
| 133 |
GR = NSMakeRect(100, 100, gl_width, gl_height); |
| 134 |
|
| 135 |
myWindow = [[ NSWindow alloc] initWithContentRect:GR |
| 136 |
styleMask:NSTitledWindowMask| |
| 137 |
NSMiniaturizableWindowMask |
| 138 |
backing:NSBackingStoreBuffered defer:NO]; |
| 139 |
|
| 140 |
sprintf(name, "VGL: `%s'", argv[0]); |
| 141 |
|
| 142 |
myView = [[ NSView alloc] initWithFrame:GR]; |
| 143 |
|
| 144 |
myMenu = [[ NSMenu alloc] initWithTitle:@"VGL"]; |
| 145 |
[myMenu addItemWithTitle:@"Quit" |
| 146 |
action:@selector(terminate:) |
| 147 |
keyEquivalent:@"q"]; |
| 148 |
[myMenu sizeToFit]; |
| 149 |
|
| 150 |
[myWindow setTitle:[NSString stringWithCString:name]]; |
| 151 |
[myWindow display]; |
| 152 |
[myWindow setContentView:myView]; |
| 153 |
[myWindow makeKeyAndOrderFront:nil]; |
| 154 |
|
| 155 |
[NSApp setMainMenu:myMenu]; |
| 156 |
|
| 157 |
[myView lockFocus]; |
| 158 |
#endif |
| 159 |
|
| 160 |
#if defined(HAVE_X11) |
| 161 |
ctx = glXCreateContext(dpy, vi, 0, GL_TRUE); |
| 162 |
glXMakeCurrent(dpy, win, ctx); |
| 163 |
#elif defined(HAVE_WIN32) |
| 164 |
#elif defined(HAVE_NEXT) |
| 165 |
ctx = OSMesaCreateContext( GL_RGBA, NULL ); |
| 166 |
OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, gl_width, gl_height ); |
| 167 |
#endif |
| 168 |
|
| 169 |
c[0] = (GLclampf)1.0; |
| 170 |
c[1] = (GLclampf)1.0; |
| 171 |
c[2] = (GLclampf)0.0; |
| 172 |
c[3] = (GLclampf)1.0; |
| 173 |
glClearColor( c[0], c[1], c[2], c[3] ); |
| 174 |
glClear(GL_COLOR_BUFFER_BIT); |
| 175 |
glFlush(); |
| 176 |
|
| 177 |
#if defined(HAVE_X11) |
| 178 |
glXSwapBuffers(dpy,win); |
| 179 |
while(1); |
| 180 |
#elif defined(HAVE_WIN32) |
| 181 |
#elif defined(HAVE_NEXT) |
| 182 |
[bitmap draw]; |
| 183 |
[bitmap release]; |
| 184 |
[myWindow flushWindow]; |
| 185 |
[myView unlockFocus]; |
| 186 |
free( buffer ); |
| 187 |
OSMesaDestroyContext( ctx ); |
| 188 |
[NSApp run]; |
| 189 |
[NSApp release]; |
| 190 |
#endif |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|