how to start

hey guys
i am very new to opengl
but would like to learn a lot on it
But i have no idea where to start
so i searched and found out that i have to include the glut.h header in the code
i am going to use c++ with GNU’s g++ compiler in ubuntu
so i downloaded the header but i dont know where to paste the header in my hard disk so that i can use it in my program
please help

Maybe here: http://opengl.org/wiki/Getting_started

Welcome to the fun world of Linux and openGL. That is great you want to learn. You should not have to manually copy any header files anywhere. Linux has synaptic to make installing everything a simple and easy hands-off task.

I suggest using glut to help your learning curve. In synaptic you can make sure it is installed by checking “freeglut3”.

As a test of your gcc tools, you should be able to run from a terminal “gcc --version” and see a response something like “gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3”. If that works then you are ready to try coding if not take a look at How to install GCC in Ubuntu Linux.

Since you are still in a terminal, type


gedit main.cpp &

and paste in the following code


//this shows one way to get FPS using an inprecise clock
//or to throttle the drawing rate to a fixed FPS with glutTimerFunc
//g++ main.cpp -lGL -lglut

#include <cstdlib>
#include <iostream>

//linux openGL headers
#include <GL/gl.h>
#include <GL/glut.h>

GLint gFramesPerSecond = 0;

void FPS(void) {
  static GLint Frames = 0;         // frames averaged over 1000mS
  static GLuint Clock;             // [milliSeconds]
  static GLuint PreviousClock = 0; // [milliSeconds]
  static GLuint NextClock = 0;     // [milliSeconds]

  ++Frames;
  Clock = glutGet(GLUT_ELAPSED_TIME); //has limited resolution, so average over 1000mS
  if ( Clock < NextClock ) return;

  gFramesPerSecond = Frames/1; // store the averaged number of frames per second

  PreviousClock = Clock;
  NextClock = Clock+1000; // 1000mS=1S in the future
  Frames=0;
}

void timer(int value)
{
  const int desiredFPS=120;
  glutTimerFunc(1000/desiredFPS, timer, ++value);

  //put your specific idle code here
  //... this code will run at desiredFPS
  char spinner[] = {'|','/','-','~','\\'};
  printf("%c",spinner[value%sizeof(spinner)/sizeof(char)]);
  //end your specific idle code here

  FPS(); //only call once per frame loop to measure FPS 
  glutPostRedisplay();
}

void display() {
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glClear(GL_COLOR_BUFFER_BIT);

  // Set the drawing color (RGB: WHITE)
  printf("FPS %d\r",gFramesPerSecond); fflush(stdout);

  glColor3f(1.0,1.0,1.0);

  glBegin(GL_LINE_STRIP); {
     glVertex3f(0.25,0.25,0.0);
     glVertex3f(0.75,0.25,0.0);
     glVertex3f(0.75,0.75,0.0);
     glVertex3f(0.25,0.75,0.0);
     glVertex3f(0.25,0.25,0.0);
  }
  glEnd(); 

  glutSwapBuffers();
}

void init() {
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0); 
}

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

int main(int argc, char** argv) {
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   glutCreateWindow("FPS test");

   glutTimerFunc(0,timer,0);
   glutDisplayFunc(display);
   glutKeyboardFunc(keyboard);

   init();

   glutMainLoop();
   return 0;
}

save it and then in the same terminal you opened earlier, compile your code using the command


g++ main.cpp -lGL -lglut

which will create an executable named a.out. Try it by typing in the terminal


./a.out

which will pop up a openGL window with a rather boring box being drawn and in the terminal you will see a constantly updating FPS reading. Note you can throttle this up and down by adjusting the “desiredFPS=120” in the timer function. Press the <escape> key to exit and return back to the terminal.

Have fun on your learning journey. I look forward to seeing your future openGL creations :slight_smile: