Programowanie w systemie UNIX/c grafika/OpenGL/glut

Z Wikibooks, biblioteki wolnych podręczników.

GLUT = Graphics Library Utility Tool-kit (ang.) jest to wysokopoziomowa biblioteka zawierająca niezależny od sprzętu dostęp do okien (ang. window system)[1] Programowanie z użyciem biblioteki Glut są sterowane zdarzeniami [2]

Wersje[edytuj]

  • oryginalna (Bardzo stara biblioteka)
  • freeglut [3]
  • openglut

Jak sprawdzić jaką wersję używamy:

locate glut.h

otrzymujemy:

/usr/include/GL/freeglut.h
/usr/include/GL/glut.h


Możemy używać:

#ifdef FREEGLUT 
#include <GL/freeglut.h> 
#else 
#include <GL/glut.h> 
#endif

Instalacja[edytuj]

opis [4]

freeglut[edytuj]

Download (HTTP): http://downloads.sourceforge.net/freeglut/freeglut-2.8.1.tar.gz
Download MD5 sum: 918ffbddcffbac83c218bc52355b6d5a
Download size: 984 KB
Estimated disk space required: 11 MB
Estimated build time: 0.1 SBU
Freeglut Dependencies : Required GLU-9.0.0
Test suite : no 
Installed Library : libglut.so


Instrukcje:

./configure --prefix=/usr --disable-static && make

Teraz jako root:

make install

Interakcja[edytuj]

Program sterowany zdarzeniami:[5]

  • mysz (ruch, kliknięcie)
  • klawiatura: glutKeyboardFunc
  • menu
  • okno (przesunięcie lub zmiana rozmiaru): glutReshapeFunc
  • system (Idle, timer): glutIdleFunc, glutTimerFunc
  • program (glutDisplayFunc)


Typy zdarzeń związanych z myszą:

  • kliknięcie (glutMouseFunc)
  • ruch (glutMotionFunc, glutPassiveMotionFunc)
  • przekraczanie granic okna (glutEntryFunc)

Programy[edytuj]

Użycie GLUT do sprawdzenia wersji OpenGL[edytuj]

#include <stdio.h> /* printf */
#include <GL/glut.h> /* glut graphics library */
/* 
Linux c console program 
gcc f.c -lglut -lGL
./a.out
*/
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("red 3D lighted cube");
  printf("GL_VERSION = %s\n",glGetString(GL_VERSION) ); /* GL_VERSION = 2.1.2 NVIDIA 195.36.24 */
  return 0; 
}

Więcej informacji[edytuj]

Ten program wyświetla informacje o glut.[6]

Kompilacja:

gcc v.c -lglut -lGL

Uruchomienie:

./a.out
#include <stdio.h>

#if defined(HAVE_FREEGLUT)

#ifdef WIN32
#include "../include/GL/freeglut.h"
#else
#include <GL/freeglut.h>
#endif

#else

#include <GL/glut.h>

#endif

#define PROGRAM "glversion"

int main(int argc, char **argv)
{
  char *version = NULL;
  char *vendor = NULL;
  char *renderer = NULL;
  char *extensions = NULL;
  GLuint idWindow = 0;
  int	glutVersion;

  glutInit(&argc, argv);
  glutInitWindowSize(1,1);
  glutInitDisplayMode(GLUT_RGBA);
  idWindow = glutCreateWindow(PROGRAM);
  glutHideWindow();

  glutVersion = glutGet(0x01FC);
  version =     (char*)glGetString(GL_VERSION);
  vendor =      (char*)glGetString(GL_VENDOR);
  renderer =    (char*)glGetString(GL_RENDERER);
  extensions =  (char*)glGetString(GL_EXTENSIONS);

  printf("GLUT=%d\nVERSION=%s\nVENDOR=%s\nRENDERER=%s\nEXTENSIONS=%s\n",
    glutVersion,version,vendor,renderer,extensions);

  glutDestroyWindow(idWindow);
  return(0);
}

GLSL[edytuj]

// http://www.lighthouse3d.com/tutorials/glsl-tutorial/setup/
// 
#include <GL/glew.h> //  use GLEW to access OpenGL 3.3 functionality.
#include <GL/glut.h>
 
void main(int argc, char **argv) {
 
    glutInit(&argc, argv);
    ...
    glewInit();
 
    if (glewIsSupported("GL_VERSION_3_3"))
        printf("Ready for OpenGL 3.3\n");
    else {
        printf("OpenGL 3.3 not supported\n");
        exit(1);
    }
 
    setShaders();
    initGL();
 
        glutMainLoop();
}

Źródła[edytuj]

  1. Building OpenGL/GLUT Programs by Sugih Jamin
  2. GLUT description TUFTS UNIVERSITY
  3. FreeGLUT is a completely OpenSourced alternative to the OpenGL Utility Toolkit (GLUT) library
  4. Building OpenGL/GLUT Programs by Sugih Jamin
  5. Event-driven interaction with GLUT by Gustav Taxén
  6. glversion.c