Initial version of a fully OpenGL-3.0-compliant of the famous smooth.c from the
[freeglut] / progs / demos / smooth_opengl3 / smooth_opengl3.c
1 /*
2  * smooth_opengl3.c, based on smooth.c, which is (c) by SGI, see below.
3  * This program demonstrates smooth shading in a way which is fully
4  * OpenGL-3.0-compliant.
5  * A smooth shaded polygon is drawn in a 2-D projection.
6  */
7
8 /*
9  * Original copyright notice from smooth.c:
10  *
11  * License Applicability. Except to the extent portions of this file are
12  * made subject to an alternative license as permitted in the SGI Free
13  * Software License B, Version 1.1 (the "License"), the contents of this
14  * file are subject only to the provisions of the License. You may not use
15  * this file except in compliance with the License. You may obtain a copy
16  * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
17  * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
18  * 
19  * http://oss.sgi.com/projects/FreeB
20  * 
21  * Note that, as provided in the License, the Software is distributed on an
22  * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
23  * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
24  * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
25  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
26  * 
27  * Original Code. The Original Code is: OpenGL Sample Implementation,
28  * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
29  * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
30  * Copyright in any portions created by third parties is as indicated
31  * elsewhere herein. All Rights Reserved.
32  * 
33  * Additional Notice Provisions: The application programming interfaces
34  * established by SGI in conjunction with the Original Code are The
35  * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
36  * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
37  * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
38  * Window System(R) (Version 1.3), released October 19, 1998. This software
39  * was created using the OpenGL(R) version 1.2.1 Sample Implementation
40  * published by SGI, but has not been independently verified as being
41  * compliant with the OpenGL(R) version 1.2.1 Specification.
42  *
43  */
44
45 #include <GL/freeglut.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48
49 void reportErrors(const char *message)
50 {
51    GLenum error;
52    while (( error = glGetError() ) != GL_NO_ERROR) {
53       fprintf (stderr, "GL error 0x%X %s\n", error, message);
54    }
55 }
56
57 void init(void) 
58 {
59    reportErrors ("at start of init");
60    glClearColor (0.0, 0.0, 0.0, 0.0);
61    glShadeModel (GL_SMOOTH);
62    reportErrors ("at end of init");
63 }
64
65 void triangle(void)
66 {
67    reportErrors ("at start of triangle");
68    glBegin (GL_TRIANGLES);
69    glColor3f (1.0, 0.0, 0.0);
70    glVertex2f (5.0, 5.0);
71    glColor3f (0.0, 1.0, 0.0);
72    glVertex2f (25.0, 5.0);
73    glColor3f (0.0, 0.0, 1.0);
74    glVertex2f (5.0, 25.0);
75    glEnd();
76    reportErrors ("at end of triangle");
77 }
78
79 void display(void)
80 {
81    reportErrors ("at start of display");
82    glClear (GL_COLOR_BUFFER_BIT);
83    triangle ();
84    glFlush ();
85    reportErrors ("at end of display");
86 }
87
88 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
89                 GLfloat n, GLfloat f)
90 {
91    m[ 0] = 2.0f / (r - l);
92    m[ 1] = 0.0f;
93    m[ 2] = 0.0f;
94    m[ 3] = 0.0f;
95
96    m[ 4] = 0.0f;
97    m[ 5] = 2.0f / (t - b);
98    m[ 6] = 0.0f;
99    m[ 7] = 0.0f;
100
101    m[ 8] = 0.0f;
102    m[ 9] = 0.0f;
103    m[10] = -2.0f / (f - n);
104    m[11] = 0.0f;
105
106    m[12] = -(r + l) / (r - l);
107    m[13] = -(t + b) / (t - b);
108    m[14] = -(f + n) / (f - n);
109    m[15] = 1.0f;
110 }
111
112 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
113 {
114    loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
115 }
116
117 void reshape (int w, int h)
118 {
119    GLfloat m[16];
120    reportErrors ("at start of reshape");
121    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
122    glMatrixMode (GL_PROJECTION);
123    if (w <= h) {
124       loadOrtho2Df (m, 0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w);
125    } else {
126       loadOrtho2Df (m, 0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0);
127    }
128    glLoadMatrixf (m);
129    glMatrixMode (GL_MODELVIEW);
130    reportErrors ("at end of reshape");
131 }
132
133 void keyboard(unsigned char key, int x, int y)
134 {
135    switch (key) {
136       case 27:
137          exit(0);
138          break;
139    }
140 }
141
142 int main(int argc, char** argv)
143 {
144    glutInit(&argc, argv);
145    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
146    glutInitContextVersion(3, 0);
147    // glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
148    glutInitWindowSize (500, 500); 
149    glutInitWindowPosition (100, 100);
150    glutCreateWindow (argv[0]);
151    init ();
152    glutDisplayFunc(display); 
153    glutReshapeFunc(reshape);
154    glutKeyboardFunc (keyboard);
155    glutMainLoop();
156    return 0;
157 }