I'm interested in writing OpenGL shaders, but am not sure if my graphics card is good enough to support this or if my system is configured correctly to use the software alternative (Mesa). How do I tell if my computer will support OpenGL shaders? (I use Ubuntu 10.04) I've tried three tests so far, and I'm getting contradictory answers for each:
1) I downloaded, compiled and successfully ran an OpenGL shader program from the sample code in OpenGL A Primer 3rd Edition located here. However, some of the other code samples from that same chapter which implement other OpenGL shaders don't run. Some of them even cause my computer to crash or the output window to do funny things with flashing colors, very strange.
2) I ran the following command and got:
$ glxinfo | grep "OpenGL version"
OpenGL version string: 1.5 Mesa 7.7.1
This would seem to indicate that I'm running OpenGL 1.5, but the Mesa version (which as I understand it is a software implementation of OpenGL 2.0. Not as fast, but same functionally as the real deal in hardware) seems to be good enough to run OpenGL 2.0. How can I tell which drive my code is using, OpenGL 1.5 or Mesa 7.7.1?
3) I wrote some code to output the version of OpenGL on the computer and got the following output:
$ ./version
OpenGL Version major=1, minor=5
GLSL Version major=0, minor=0
This doesn't say anything about Mesa, and would lead me to believe I'm running OpenGL 1.5. Please help me understand what version I'm running, so I can know if I need to run out to the store and buy a new graphics card before I can be confident shaders will run. Thank you!
P.S. Here's the code:
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>
void getGlVersion(int *major, int *minor)
{
const char *verstr = (const char *) glGetString(GL_VERSION);
if ((verstr == NULL) || (sscanf(verstr,"%d.%d", major, minor) != 2))
{
*major = *minor = 0;
fprintf(stderr, "Invalid GL_VERSION format!!!\n");
}
}
void getGlslVersion(int *major, int *minor)
{
int gl_major, gl_minor;
getGlVersion(&gl_major, &gl_minor);
*major = *minor = 0;
if(gl_major == 1)
{
/* GL v1.x can provide GLSL v1.00 only as an extension */
const char *extstr = (const char *) glGetString(GL_EXTENSIONS);
if ((extstr != NULL) &&
(strstr(extstr, "GL_ARB_shading_language_100") != NULL))
{
*major = 1;
*minor = 0;
}
}
else if (gl_major >= 2)
{
/* GL v2.0 and greater must parse the version string */
const char *verstr =
(const char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
if((verstr == NULL) ||
(sscanf(verstr, "%d.%d", major, minor) != 2))
{
*major = *minor = 0;
fprintf(stderr,
"Invalid GL_SHADING_LANGUAGE_VERSION format!!!\n");
}
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
int major, minor;
getGlVersion(&major, &minor);
fprintf(stderr, "OpenGL Version major=%i, minor=%i\n", major, minor);
getGlslVersion(&major, &minor);
fprintf(stderr, "GLSL Version major=%i, minor=%i\n", major, minor);
}
void init() {}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display);
init();
glutMainLoop();
}
The only "GL Approved" way of getting the GL version is using glGetString(GL_VERSION), and this reports the GL version supported by the driver.
Mesa can implement any version of OpenGL, even if it's implemented in software or hardware, in your case you have OpenGL 1.5 IMPLEMENTED by MESA 7.7.1, which is the OpenGL implementation.
The only sure way of knowing whatever a certain HW supports a certain GL version is to check the manufacturer specifications, since drivers can be outdated and support a older GL version. A example is GeForce 8 cards, they originally supported up to OpenGL 2.1, and when OpenGL 3.x appeared, they supported it also, but only using updated drivers.