Merging OCC22105, OCC22354, OCC22150 , OCC22199 , OCC22391 and OCC22108
[occt.git] / src / OpenGl / OpenGl_Extension.cxx
1 /*
2 * Fonction
3 * ~~~~~~~~
4 *   Gestion des extensions sous OpenGL
5 *
6 *
7 * Attention:
8 * ~~~~~~~~~~~
9 *  Ce package a ete teste sur SGI, OSF, SUN,  HP et WNT.
10 *
11 * Remarques:
12 * ~~~~~~~~~~~
13 *  Le InitExtensionGLX permet d'initialiser le display. Ceci est necessaire
14 *  pour travailler sur les extensions de GLX. On ne peut appeler QueryExtensionGLX
15 *  si on n'a pas fait cette manip.
16 *  Par contre QueryExtension gere les extensions a GL,  on n'a pas besoin du
17 *  Display.
18 *
19 *  Pour l'instant on ne gere pas les extensions a GLU et a WGL.
20 *
21 * Historique des modifications
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 *   14-10-97: FMN ; creation
24 *   23-10-97: FMN ; ajout gestion glx
25 *   04-11-97: FMN ; Gestion des differentes versions GLX
26 *   19-11-97: FMN ; Ajout GetCurrentDisplay
27 *   04-12-97: FMN ; Ajout supportsOneDotOne
28 *   22-07-98: FMN ; InitExtensionGLX n'est execute qu'une fois
29 *   28-07-98: FMN ; Renomme theDisplay en mytheDisplay
30 */
31 /*----------------------------------------------------------------------*/
32
33 /*----------------------------------------------------------------------*/
34 /*
35 * Includes
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h> 
40 #include <string.h>
41
42 #include <OpenGl_Extension.hxx>
43
44 #define XDEBUG 
45
46 /*----------------------------------------------------------------------*/
47 /*
48 * Types definis
49 */
50
51
52 /*----------------------------------------------------------------------*/
53 /*
54 * Variables statiques
55 */
56
57 static Display *mytheDisplay = NULL;
58 static int  screen_num;
59 static int  GLXmajor, GLXminor;
60 static int  GLmajor, GLminor;
61
62 static GLboolean flag_InitExtensionGLX = GL_FALSE;
63
64 static GLboolean OneDotOne = GL_FALSE;
65 static GLboolean initOneDotOne = GL_FALSE;
66
67 /*----------------------------------------------------------------------*/
68 /*
69 * Fonctions publiques 
70 */
71
72 /*----------------------------------------------------------------------*/
73 extern GLboolean CheckExtension(char *extName, const char *extString)
74 {
75   /*
76   ** Search for extName in the extensions string.  Use of strstr()
77   ** is not sufficient because extension names can be prefixes of
78   ** other extension names.  Could use strtok() but the constant
79   ** string returned by glGetString can be in read-only memory.
80   */
81   char *p = (char *)extString;
82   char *end;
83   int extNameLen;
84
85   extNameLen = strlen(extName);
86   end = p + strlen(p);
87
88   while (p < end) {
89     int n = strcspn(p, " ");
90     if ((extNameLen == n) && (strncmp(extName, p, n) == 0)) {
91       return GL_TRUE;
92     }
93     p += (n + 1);
94   }
95   return GL_FALSE;
96 }
97
98 extern GLboolean InitExtensionGLX(Display *display)
99 {
100 #ifndef WNT 
101
102   int dontcare; /* for returned arguments we don't care about */
103
104   if (display == NULL) return GL_FALSE;
105
106   if (!flag_InitExtensionGLX)
107   {       
108     /* does the server know about OpenGL & GLX? */
109     if(!XQueryExtension(display, "GLX", &dontcare, &dontcare, &dontcare)) {
110 #ifdef DEBUG
111       fprintf(stderr,"This system doesn't appear to support OpenGL\n");
112 #endif /* DEBUG */
113       return GL_FALSE;
114     }  
115
116     /* find the glx version */
117     if(glXQueryVersion(display, &GLXmajor, &GLXminor)) {
118 #ifdef DEBUG
119       printf("GLX Version: %d.%d\n", GLXmajor, GLXminor);
120 #endif /* DEBUG */
121     } else {
122 #ifdef DEBUG
123       fprintf(stderr, "Error: glXQueryVersion() failed.\n");
124 #endif /* DEBUG */
125       return GL_FALSE;
126     }
127
128     /* get screen number */
129     screen_num = DefaultScreen(display);
130
131     flag_InitExtensionGLX = GL_TRUE;
132     mytheDisplay = display;
133
134   } /* (!flag_InitExtensionGLX) */
135
136   return GL_TRUE;
137
138 #else
139
140   return GL_FALSE;
141
142 #endif /* WNT */
143 }
144
145 /*----------------------------------------------------------------------*/
146 extern GLboolean QueryExtensionGLX(char *extName)
147 {
148   GLboolean result = GL_FALSE;
149
150 #ifdef GLX_VERSION_1_1
151   if (flag_InitExtensionGLX)
152   {
153     if ( GLXminor > 1 || GLXmajor > 1 ) /* GLX_VERSION_1_2 */
154     {
155       /* Certaines extensions sont par defaut dans la version 1.2 */
156       if (strcmp(extName,"GLX_EXT_import_context")) return GL_TRUE;
157       result = CheckExtension(extName, glXQueryExtensionsString(mytheDisplay, screen_num));
158     }
159     else if( GLXminor > 0 || GLXmajor > 1 ) /* GLX_VERSION_1_1 */
160     {
161       result = CheckExtension(extName, glXQueryExtensionsString(mytheDisplay, screen_num));
162     }
163   }
164 #endif    
165 #ifdef DEBUG
166   printf("QueryExtensionGLX: %s %d", extName, result);
167 #endif 
168   return result;
169 }
170
171 /*----------------------------------------------------------------------*/
172 extern Display *GetCurrentDisplay(void)
173 {
174 #ifdef DEBUG
175   printf("GetCurrentDisplay %x \n", mytheDisplay);
176 #endif 
177   return mytheDisplay;  
178 }
179
180 /*----------------------------------------------------------------------*/
181 extern GLboolean QueryExtension(char *extName)
182 {
183   GLboolean result = GL_FALSE;
184
185   if (supportsOneDotOne()) /* GL_VERSION_1_1 ou more */
186   {
187     /* Certaines extensions sont par defaut dans la version 1.1 */
188     /* Certain extensions are the defaut in version 1.1 */
189     if ((strcmp(extName,"GL_EXT_vertex_array")) ||  
190       (strcmp(extName,"GL_EXT_polygon_offset")) ||  
191       (strcmp(extName,"GL_EXT_blend_logic_op")) ||  
192       (strcmp(extName,"GL_EXT_texture"))    ||
193       (strcmp(extName,"GL_EXT_copy_texture")) ||
194       (strcmp(extName,"GL_EXT_subtexture")) ||  
195       (strcmp(extName,"GL_EXT_texture_object"))) 
196       result =  GL_TRUE;  
197     else  
198       result = CheckExtension(extName, (char *)glGetString(GL_EXTENSIONS)); 
199   }
200   else  /* GL_VERSION_1_0 */
201   {
202     result = CheckExtension(extName, (char *)glGetString(GL_EXTENSIONS)); 
203   }
204
205 #ifdef DEBUG
206   printf("QueryExtension: %s %d \n", extName, result);
207 #endif 
208   return result;
209 }
210 /*----------------------------------------------------------------------*/
211
212 extern GLboolean supportsOneDotOne(void)
213 {
214   const GLubyte *version;
215
216   if (!initOneDotOne)
217   {
218     version = glGetString(GL_VERSION);
219     if(sscanf((const char *)version, "%d.%d", &GLmajor, &GLminor) == 2)
220     {
221       OneDotOne = (GLmajor>=1 && GLminor >=1);
222     }
223     initOneDotOne = GL_TRUE;
224   }
225 #ifdef DEBUG
226   printf("GL Version: %d.%d\n", GLmajor, GLminor);
227 #endif /* DEBUG */
228   return OneDotOne;
229 }
230
231 /*----------------------------------------------------------------------*/