Skip to content

Instantly share code, notes, and snippets.

@thomaswilley
Last active April 8, 2020 09:26
Show Gist options
  • Save thomaswilley/ac0df1c1473f1454dd3a614c015b1d4c to your computer and use it in GitHub Desktop.
Save thomaswilley/ac0df1c1473f1454dd3a614c015b1d4c to your computer and use it in GitHub Desktop.
Zero to rotating cube in opengl / glut

Sources

  1. base for cube.c: https://www.opengl.org/archives/resources/code/samples/glut_examples/examples/cube.c
  2. more complex example: https://gist.github.com/hkulekci/2300262

Prereqs (install opengl & glut)

$ sudo apt-get install freeglut3-dev

Makefile

all:
	gcc cube.c -lGL -lGLU -lglut

clean:
	rm a.out

The rotating cube in C.

/* edited 201912 by @thomaswilley */

/* Copyright (c) Mark J. Kilgard, 1997. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

/* This program was requested by Patrick Earl; hopefully someone else
   will write the equivalent Direct3D immediate mode program. */

#include <GL/glut.h>

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
  {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
  {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */

float rot = 0.0f;
float resize_f = 1.0f;

void
drawBox(void)
{
  int i;

  for (i = 0; i < 6; i++) {
    glBegin(GL_QUADS);
    glNormal3fv(&n[i][0]);
    glVertex3fv(&v[faces[i][0]][0]);
    glVertex3fv(&v[faces[i][1]][0]);
    glVertex3fv(&v[faces[i][2]][0]);
    glVertex3fv(&v[faces[i][3]][0]);
    glEnd();
  }
}

void
display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();

  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
      0.0, 0.0, 0.0,      /* center is at (0,0,0) */
      0.0, 1.0, 0.0);      /* up is in positive Y direction */

  glRotatef(rot, 1.0, 1.0, 1.0);

  drawBox();

  glFlush();
  glutSwapBuffers();
}

void idle() {
  rot += 0.3f;
  glutPostRedisplay();
}

void
init(void)
{
  /* Setup cube vertex data. */
  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

  /* Enable a single OpenGL light. */
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);

  /* Use depth buffering for hidden surface elimination. */
  glEnable(GL_DEPTH_TEST);

    /* Adjust cube position to be asthetic angle. */
  glTranslatef(0.0, 0.0, -1.0);
}

void
reshape(int w, int h)
{
  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  glViewport(0, 0, w, h);

  gluPerspective(/* field of view in degree */ 40.0,
      /* aspect ratio */ resize_f * w / h,
      /* Z near */ resize_f,
      /* Z far */ 100 * resize_f);
  glMatrixMode(GL_MODELVIEW);
}

int
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("red 3D lighted cube");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutIdleFunc(idle);
  init();
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment