GLUT - OpenGL 3.1 Context - How?

Hi,

i use Linux with freeglut3 (2.4.0-8). Every time i create a context an opengl 2.1 context is created. Is there any way to define which kind of context will be created?

Via glXCreateContextAttribsARB it’s no problem to create the opengl 3.1 context but than i don’t know how to use glut in combination with that ‘manually’ created context.

Greetings,

Jotschi

As you know, GLUT itself has been frozen in time but it has a updated and extended opensource replacement freeglut. I have been using the latest SVN recently which contains the ability to create an ogl3 context. However, there is a new stable release that I haven’t tried yet “Freeglut 2.6.0 [Released: 27 November 2009]” that you could update to and try. – I will try it too when I get a chance since it would be nice to be using a stable branch instead of testing their SVN unstable branch all the time.

There is an example in the 2.6.0 release which shows how to get a 3.1 context … freeglut-2.6.0/progs/demos/smooth_opengl3/ pay particular attention to the following header line and two lines in the smooth_opengl3.c


   #include <GL/freeglut.h>
...

      glutInitContextVersion (3, 1);
      glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);

Since ubuntu isn’t up to date with this latest version, what I do is compile freeglut-2.6 to a local folder (this way I don’t clobber the system wide freeglut to be safe for now)


download and extract tarball (2.6 version)
tar zxvf freeglut-2.6.0.tar.gz
cd freeglut-2.6.0
./configure --prefix ~/external/freeglut
make
make install
cd 
progs/demos/smooth_opengl3
./smooth_opengl3
!!! you will see a gl3.1 example running :)


... with my method, the headers and lib files ended up in ~/external/freeglut

ls ~/external/freeglut

so to compile your own foo.cpp code you have to explicit where and which headers to use ...
the compile command line becomes

g++ foo.cpp -I$HOME/external/freeglut/include/ $HOME/external/freeglut/lib/libglut.a -lX11 -lGL -lXxf86vm 

I consider this a temporary solution that will be cleaner when ubuntu catches up to the 2.6 release!

Note, I use gl3.h from openGL registry to replace the initExtensionEntries() and other related crust – freglut did it that way to avoid having extra libraries in their source examples. And that example code consequently mangles glFunction names as gl_function name – I don’t like this so here is my more “standard” code if you like


/*
 * smooth_opengl3.c, based on smooth.c, which is (c) by SGI, see below.
 * This program demonstrates smooth shading in a way which is fully
 * OpenGL-3.1-compliant.
 * A smooth shaded polygon is drawn in a 2-D projection.
 */

/*
 * Original copyright notice from smooth.c:
 *
 * License Applicability. Except to the extent portions of this file are
 * made subject to an alternative license as permitted in the SGI Free
 * Software License B, Version 1.1 (the "License"), the contents of this
 * file are subject only to the provisions of the License. You may not use
 * this file except in compliance with the License. You may obtain a copy
 * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
 * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
 * 
 * http://oss.sgi.com/projects/FreeB
 * 
 * Note that, as provided in the License, the Software is distributed on an
 * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
 * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
 * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 * 
 * Original Code. The Original Code is: OpenGL Sample Implementation,
 * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
 * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
 * Copyright in any portions created by third parties is as indicated
 * elsewhere herein. All Rights Reserved.
 * 
 * Additional Notice Provisions: The application programming interfaces
 * established by SGI in conjunction with the Original Code are The
 * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
 * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
 * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
 * Window System(R) (Version 1.3), released October 19, 1998. This software
 * was created using the OpenGL(R) version 1.2.1 Sample Implementation
 * published by SGI, but has not been independently verified as being
 * compliant with the OpenGL(R) version 1.2.1 Specification.
 *
 */

#define GL3_PROTOTYPES
#include <GL3/gl3.h>
#include <GL/freeglut.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>

/* report GL errors, if any, to stderr */
void checkError(const char *functionName)
{
   GLenum error;
   while (( error = glGetError() ) != GL_NO_ERROR) {
      fprintf (stderr, "GL error 0x%X detected in %s
", error, functionName);
   }
}

/* vertex array data for a colored 2D triangle, consisting of RGB color values
   and XY coordinates */
const GLfloat varray[] = {
   1.0f, 0.0f, 0.0f, /* red */
   5.0f, 5.0f,       /* lower left */

   0.0f, 1.0f, 0.0f, /* green */
   25.0f, 5.0f,      /* lower right */

   0.0f, 0.0f, 1.0f, /* blue */
   5.0f, 25.0f       /* upper left */
};

/* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
enum {
  numColorComponents = 3,
  numVertexComponents = 2,
  stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
  numElements = sizeof(varray) / stride
};

/* the name of the vertex buffer object */
GLuint vertexBufferName;

void initBuffer(void)
{
   glGenBuffers (1, &vertexBufferName);
   glBindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
   glBufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
   checkError ("initBuffer");
}

const GLchar *vertexShaderSource[] = {
   "#version 140
",
   "uniform mat4 fg_ProjectionMatrix;
",
   "in vec4 fg_Color;
",
   "in vec4 fg_Vertex;
",
   "smooth out vec4 fg_SmoothColor;
",
   "void main()
",
   "{
",
   "   fg_SmoothColor = fg_Color;
",
   "   gl_Position = fg_ProjectionMatrix * fg_Vertex;
",
   "}
"
};

const GLchar *fragmentShaderSource[] = {
   "#version 140
",
   "smooth in vec4 fg_SmoothColor;
",
   "out vec4 fg_FragColor;
",
   "void main(void)
",
   "{
",
   "   fg_FragColor = fg_SmoothColor;
",
   "}
"
};

void compileAndCheck(GLuint shader)
{
   GLint status;
   glCompileShader (shader);
   glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
   if (status == GL_FALSE) {
     GLint infoLogLength;
     GLchar *infoLog;
     glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
     infoLog = (GLchar*) malloc (infoLogLength);
     glGetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
     fprintf (stderr, "compile log: %s
", infoLog);
     free (infoLog);
   }
}

GLuint compileShaderSource(GLenum type, GLsizei count, const GLchar **string)
{
   GLuint shader = glCreateShader (type);
   glShaderSource (shader, count, string, NULL);
   compileAndCheck (shader);
   return shader;
}

void linkAndCheck(GLuint program)
{
   GLint status;
   glLinkProgram (program);
   glGetProgramiv (program, GL_LINK_STATUS, &status);
   if (status == GL_FALSE) {
     GLint infoLogLength;
     GLchar *infoLog;
     glGetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
     infoLog = (GLchar*) malloc (infoLogLength);
     glGetProgramInfoLog (program, infoLogLength, NULL, infoLog);
     fprintf (stderr, "link log: %s
", infoLog);
     free (infoLog);
   }
}

GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
{
   GLuint program = glCreateProgram ();
   if (vertexShader != 0) {
      glAttachShader (program, vertexShader);
   }
   if (fragmentShader != 0) {
      glAttachShader (program, fragmentShader);
   }
   linkAndCheck (program);
   return program;
}

GLuint fgProjectionMatrixIndex;
GLuint fgColorIndex;
GLuint fgVertexIndex;

void initShader(void)
{
   const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(GLchar*);
   GLuint vertexShader =
     compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);

   const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(GLchar*);
   GLuint fragmentShader =
     compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);

   GLuint program = createProgram (vertexShader, fragmentShader);

   glUseProgram (program);

   fgProjectionMatrixIndex = glGetUniformLocation(program, "fg_ProjectionMatrix");

   fgColorIndex = glGetAttribLocation(program, "fg_Color");
   glEnableVertexAttribArray (fgColorIndex);

   fgVertexIndex = glGetAttribLocation(program, "fg_Vertex");
   glEnableVertexAttribArray (fgVertexIndex);

   checkError ("initShader");
}

void initRendering(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   checkError ("initRendering");
}

void init(void) 
{
   initBuffer ();
   initShader ();
   initRendering ();
}

void dumpInfo(void)
{
   printf ("Vendor: %s
", glGetString (GL_VENDOR));
   printf ("Renderer: %s
", glGetString (GL_RENDERER));
   printf ("Version: %s
", glGetString (GL_VERSION));
   printf ("GLSL: %s
", glGetString (GL_SHADING_LANGUAGE_VERSION));
   checkError ("dumpInfo");
}

const GLvoid *bufferObjectPtr (GLsizei index)
{
   return (const GLvoid *) (((char *) NULL) + index);
}

GLfloat projectionMatrix[16];

void triangle(void)
{
   glUniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);

   glBindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
   glVertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
                           stride, bufferObjectPtr (0));
   glVertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
                           stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
   glDrawArrays(GL_TRIANGLES, 0, numElements);
   checkError ("triangle");
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   triangle ();
   glFlush ();
   checkError ("display");
}

void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
                GLfloat n, GLfloat f)
{
   m[ 0] = 2.0f / (r - l);
   m[ 1] = 0.0f;
   m[ 2] = 0.0f;
   m[ 3] = 0.0f;

   m[ 4] = 0.0f;
   m[ 5] = 2.0f / (t - b);
   m[ 6] = 0.0f;
   m[ 7] = 0.0f;

   m[ 8] = 0.0f;
   m[ 9] = 0.0f;
   m[10] = -2.0f / (f - n);
   m[11] = 0.0f;

   m[12] = -(r + l) / (r - l);
   m[13] = -(t + b) / (t - b);
   m[14] = -(f + n) / (f - n);
   m[15] = 1.0f;
}

void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
{
   loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   if (w <= h) {
      loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
   } else {
      loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
   }
   checkError ("reshape");
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   /* add command line argument "classic" for a pre-3.x context */
   if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
      glutInitContextVersion (3, 1);
      glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
   }
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   dumpInfo ();
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc (keyboard);
   glutMainLoop();
   return 0;
}

One last comment on gl3 and linux, I try to keep the general gl3 files up to date with


wget http://www.opengl.org/registry/api/glext.h
wget http://www.opengl.org/registry/api/glxext.h
wget http://www.opengl.org/registry/api/gl3.h

sudo cp glext.h /usr/include/GL/
sudo cp glxext.h /usr/include/GL/
sudo cp gl3.h /usr/include/GL3/

Thank you. That did the trick :slight_smile:

:slight_smile: Glad that helped.

FYI I added to my above post a slightly modified version of the freeglut example – uses gl3.h instead of hard coding in the extensions to be more extensible later on …