Initial version of a fully OpenGL-3.0-compliant of the famous smooth.c from the
[freeglut] / progs / demos / smooth_opengl3 / smooth_opengl3.c
diff --git a/progs/demos/smooth_opengl3/smooth_opengl3.c b/progs/demos/smooth_opengl3/smooth_opengl3.c
new file mode 100755 (executable)
index 0000000..9a7915f
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * 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.0-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.
+ *
+ */
+
+#include <GL/freeglut.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+void reportErrors(const char *message)
+{
+   GLenum error;
+   while (( error = glGetError() ) != GL_NO_ERROR) {
+      fprintf (stderr, "GL error 0x%X %s\n", error, message);
+   }
+}
+
+void init(void) 
+{
+   reportErrors ("at start of init");
+   glClearColor (0.0, 0.0, 0.0, 0.0);
+   glShadeModel (GL_SMOOTH);
+   reportErrors ("at end of init");
+}
+
+void triangle(void)
+{
+   reportErrors ("at start of triangle");
+   glBegin (GL_TRIANGLES);
+   glColor3f (1.0, 0.0, 0.0);
+   glVertex2f (5.0, 5.0);
+   glColor3f (0.0, 1.0, 0.0);
+   glVertex2f (25.0, 5.0);
+   glColor3f (0.0, 0.0, 1.0);
+   glVertex2f (5.0, 25.0);
+   glEnd();
+   reportErrors ("at end of triangle");
+}
+
+void display(void)
+{
+   reportErrors ("at start of display");
+   glClear (GL_COLOR_BUFFER_BIT);
+   triangle ();
+   glFlush ();
+   reportErrors ("at end of 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)
+{
+   GLfloat m[16];
+   reportErrors ("at start of reshape");
+   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
+   glMatrixMode (GL_PROJECTION);
+   if (w <= h) {
+      loadOrtho2Df (m, 0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w);
+   } else {
+      loadOrtho2Df (m, 0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0);
+   }
+   glLoadMatrixf (m);
+   glMatrixMode (GL_MODELVIEW);
+   reportErrors ("at end of 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);
+   glutInitContextVersion(3, 0);
+   // glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
+   glutInitWindowSize (500, 500); 
+   glutInitWindowPosition (100, 100);
+   glutCreateWindow (argv[0]);
+   init ();
+   glutDisplayFunc(display); 
+   glutReshapeFunc(reshape);
+   glutKeyboardFunc (keyboard);
+   glutMainLoop();
+   return 0;
+}