Monday, May 24, 2010

OpenGL minimalist program

Sometime I need a minimalist program to start with for doing some experiment in OpenGL using c++. I always loose time searching or writing such minimalist program. So I post one here.



#include <GL/glut.h>

int win_w = 800;
int win_h = 600;

void resize(int w, int h)
{
  win_w = w; win_h = h;
  glViewport(0,0, win_w, win_h);
}

void draw(void)
{
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(80, win_w / (float)win_h, 1.0, 100.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0,0,-2);
  glutSolidTeapot(1.0f);
  glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
  glutPostRedisplay ();
}

int main(int argc, char **argv)
{
  /* Set window size and location */
  glutInit(&argc, argv);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(0, 0);

  /* Select type of Display mode:
  double buffer & RGBA color */
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

  /*Initialize GLUT state */
  glutCreateWindow("OpenGL Tea Pot");
  glutDisplayFunc( draw );
  glutReshapeFunc( resize );
  glutKeyboardFunc ( keyboard );
  glutMainLoop();

  return 0;
}

And to compile it:

g++ main.cc -lGL -lGLU -lglut -o main

No comments:

Post a Comment