Added OpenGL 3.0 context creation API entries
authorSven Panne <sven.panne@aedion.de>
Sun, 2 Nov 2008 21:30:53 +0000 (21:30 +0000)
committerSven Panne <sven.panne@aedion.de>
Sun, 2 Nov 2008 21:30:53 +0000 (21:30 +0000)
   glutInitContextVersion,
   glutInitContextFlags

and their related constants

   GLUT_INIT_MAJOR_VERSION
   GLUT_INIT_MINOR_VERSION
   GLUT_INIT_FLAGS

   GLUT_DEBUG
   GLUT_FORWARD_COMPATIBLE

Note that this works with GLX only currently, the glutInitContext* API entries
have no effect for WGL yet.

TODO: Centralize the context creation code for WGL (the harder part) and use the
new wglCreateContextAttribsARB API entry (the easy part, re-use most of the GLX
code).

git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@749 7f0cb862-5218-0410-a997-914c9d46530a

include/GL/freeglut_ext.h
src/freeglut_init.c
src/freeglut_internal.h
src/freeglut_state.c
src/freeglut_window.c

index 3028994..7dae3e9 100644 (file)
 #define  GLUT_AUX3                          0x4000
 #define  GLUT_AUX4                          0x8000
 
+/*
+ * Context-related flags, see freeglut_state.c
+ */
+#define  GLUT_INIT_MAJOR_VERSION            0x0200
+#define  GLUT_INIT_MINOR_VERSION            0x0201
+#define  GLUT_INIT_FLAGS                    0x0202
+
+/*
+ * Flags for glutInitContextFlags, see freeglut_init.c
+ */
+#define  GLUT_DEBUG                         0x0001
+#define  GLUT_FORWARD_COMPATIBLE            0x0002
 
 /*
  * Process loop function, see freeglut_main.c
@@ -162,6 +174,12 @@ void    glutJoystickGetMinRange( int ident, float *axes );
 void    glutJoystickGetMaxRange( int ident, float *axes );
 void    glutJoystickGetCenter( int ident, float *axes );
 
+/*
+ * Initialization functions, see freeglut_init.c
+ */
+FGAPI void    FGAPIENTRY glutInitContextVersion( int majorVersion, int minorVersion );
+FGAPI void    FGAPIENTRY glutInitContextFlags( int flags );
+
 #ifdef __cplusplus
     }
 #endif
index d602a71..bf5b5ac 100644 (file)
@@ -85,7 +85,10 @@ SFG_State fgState = { { -1, -1, GL_FALSE },  /* Position */
                       GL_FALSE,               /* JoysticksInitialised */
                       GL_FALSE,               /* InputDevsInitialised */
                       0,                      /* AuxiliaryBufferNumber */
-                      0                       /* SampleNumber */
+                      0,                      /* SampleNumber */
+                      1,                      /* MajorVersion */
+                      0,                      /* MajorVersion */
+                      0                       /* ContextFlags */
 };
 
 
@@ -418,6 +421,10 @@ void fgDeinitialize( void )
     fgState.JoysticksInitialised = GL_FALSE;
     fgState.InputDevsInitialised = GL_FALSE;
 
+    fgState.MajorVersion = 1;
+    fgState.MinorVersion = 0;
+    fgState.ContextFlags = 0;
+
     fgState.Initialised = GL_FALSE;
 
     fgState.Position.X = -1;
@@ -1076,4 +1083,20 @@ void FGAPIENTRY glutInitDisplayString( const char* displayMode )
     fgState.DisplayMode = glut_state_flag;
 }
 
+/* -- SETTING OPENGL 3.0 CONTEXT CREATION PARAMETERS ---------------------- */
+
+void FGAPIENTRY glutInitContextVersion( int majorVersion, int minorVersion )
+{
+    /* We will make use of these valuse when creating a new OpenGL context... */
+    fgState.MajorVersion = majorVersion;
+    fgState.MinorVersion = minorVersion;
+}
+
+
+void FGAPIENTRY glutInitContextFlags( int flags )
+{
+    /* We will make use of this value when creating a new OpenGL context... */
+    fgState.ContextFlags = flags;
+}
+
 /*** END OF FILE ***/
index d593a2c..8330e9d 100644 (file)
@@ -303,6 +303,10 @@ struct tagSFG_State
 
     int              AuxiliaryBufferNumber;  /* Number of auxiliary buffers */
     int              SampleNumber;         /*  Number of samples per pixel  */
+
+    int              MajorVersion;         /* Major OpenGL context version  */
+    int              MinorVersion;         /* Minor OpenGL context version  */
+    int              ContextFlags;         /* OpenGL context flags          */
 };
 
 /* The structure used by display initialization in freeglut_init.c */
index cafdfb9..0ab3efa 100644 (file)
@@ -203,6 +203,9 @@ int FGAPIENTRY glutGet( GLenum eWhat )
     case GLUT_INIT_WINDOW_HEIGHT:   return fgState.Size.Use ?
                                            fgState.Size.Y : -1     ;
     case GLUT_INIT_DISPLAY_MODE:    return fgState.DisplayMode     ;
+    case GLUT_INIT_MAJOR_VERSION:   return fgState.MajorVersion    ;
+    case GLUT_INIT_MINOR_VERSION:   return fgState.MinorVersion    ;
+    case GLUT_INIT_FLAGS:           return fgState.ContextFlags    ;
 
 #if TARGET_HOST_POSIX_X11
     /*
index 8450e06..cd66c40 100644 (file)
@@ -485,17 +485,84 @@ void fgSetWindow ( SFG_Window *window )
 
 
 #if TARGET_HOST_POSIX_X11
+
+#ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
+#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
+#endif
+
+#ifndef GLX_CONTEXT_MINOR_VERSION_ARB
+#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
+#endif
+
+#ifndef GLX_CONTEXT_FLAGS_ARB
+#define GLX_CONTEXT_FLAGS_ARB 0x2094
+#endif
+
+#ifndef GLX_CONTEXT_DEBUG_BIT_ARB
+#define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001
+#endif
+
+#ifndef GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
+#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
+#endif
+
+typedef GLXContext (*CreateContextAttribsProc)(Display *dpy, GLXFBConfig config,
+                                              GLXContext share_list, Bool direct,
+                                              const int *attrib_list);
+
 static GLXContext fghCreateNewContext( SFG_Window* window )
 {
+  /* for color model calculation */
   int menu = ( window->IsMenu && !fgStructure.MenuContext );
   int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
+
+  /* "classic" context creation */
   Display *dpy = fgDisplay.Display;
   GLXFBConfig config = *(window->Window.FBConfig);
   int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
   GLXContext share_list = NULL;
   Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
-
-  return glXCreateNewContext( dpy, config, render_type, share_list, direct );
+  GLXContext context;
+
+  /* new context creation */
+  int attribs[7];
+  CreateContextAttribsProc createContextAttribs;
+
+  /* If nothing fancy has been required, simply use the old context creation GLX API entry */
+  if ( fgState.MajorVersion == 1 && fgState.MinorVersion == 0 && fgState.ContextFlags == 0)
+  {
+    context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
+    if ( context == NULL ) {
+      fgError( "could not create new OpenGL context" );
+    }
+    return context;
+  }
+
+  /* color index mode is not available anymore with OpenGL 3.0 */
+  if ( render_type == GLX_COLOR_INDEX_TYPE ) {
+    fgWarning( "color index mode is deprecated, using RGBA mode" );
+  }
+
+  attribs[0] = GLX_CONTEXT_MAJOR_VERSION_ARB;
+  attribs[1] = fgState.MajorVersion;
+  attribs[2] = GLX_CONTEXT_MINOR_VERSION_ARB;
+  attribs[3] = fgState.MinorVersion;
+  attribs[4] = GLX_CONTEXT_FLAGS_ARB;
+  attribs[5] = ((fgState.ContextFlags & GLUT_DEBUG) ? GLX_CONTEXT_DEBUG_BIT_ARB : 0) |
+    ((fgState.ContextFlags & GLUT_FORWARD_COMPATIBLE) ? GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB : 0);
+  attribs[6] = 0;
+
+  createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
+  if ( createContextAttribs == NULL ) {
+    fgError( "glXCreateContextAttribsARB not found" );
+  }
+
+  context = createContextAttribs( dpy, config, share_list, direct, attribs );
+  if ( context == NULL ) {
+    fgError( "could not create new OpenGL %d.%d context (flags %x)",
+            fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags );
+  }
+  return context;
 }
 #endif