Added a commandline option for 'classic' contexts. Aesthetic changes.
[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 /* report GL errors, if any, to stderr */
50 void checkError(const char *functionName)
51 {
52    GLenum error;
53    while (( error = glGetError() ) != GL_NO_ERROR) {
54       fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
55    }
56 }
57
58 /* extension types and entries, avoiding a dependency on additional libraries
59    like GLEW or the GL/glext.h header */
60 typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
61 PFNGLGENBUFFERSPROC gl_GenBuffers;
62
63 typedef void (*PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
64 PFNGLBINDBUFFERPROC gl_BindBuffer;
65
66 typedef void (*PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size,
67                                      const GLvoid *data, GLenum usage);
68 PFNGLBUFFERDATAPROC gl_BufferData;
69
70 void initExtensionEntries(void) 
71 {
72    gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
73    gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
74    gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
75 }
76
77 /* vertex array data for a colored 2D triangle, consisting of RGB color values
78    and XY coordinates */
79 const GLfloat varray[] = {
80    1.0f, 0.0f, 0.0f, /* red */
81    5.0f, 5.0f,       /* lower left */
82
83    0.0f, 1.0f, 0.0f, /* green */
84    25.0f, 5.0f,      /* lower right */
85
86    0.0f, 0.0f, 1.0f, /* blue */
87    5.0f, 25.0f       /* upper left */
88 };
89
90 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
91 enum {
92   numColorComponents = 3,
93   numVertexComponents = 2,
94   stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
95   numElements = sizeof(varray) / stride
96 };
97
98 /* the name of the vertex buffer object */
99 GLuint vertexBufferName;
100
101 void initBuffer(void)
102 {
103    gl_GenBuffers (1, &vertexBufferName);
104    gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
105    gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
106    glEnableClientState (GL_COLOR_ARRAY);
107    glEnableClientState (GL_VERTEX_ARRAY);
108    checkError ("initBuffer");
109 }
110
111 void initRendering(void)
112 {
113    glClearColor (0.0, 0.0, 0.0, 0.0);
114    glShadeModel (GL_SMOOTH);
115    checkError ("initRendering");
116 }
117
118 void init(void) 
119 {
120    initExtensionEntries();
121    initBuffer();
122    initRendering();
123 }
124
125 const GLvoid *bufferObjectPtr (GLsizei index)
126 {
127    return (const GLvoid *) (((char *) NULL) + index);
128 }
129
130 void triangle(void)
131 {
132    gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
133    glColorPointer (numColorComponents, GL_FLOAT, stride, bufferObjectPtr (0));
134    glVertexPointer(numVertexComponents, GL_FLOAT, stride,
135                    bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
136    glDrawArrays(GL_TRIANGLES, 0, numElements);
137    checkError ("triangle");
138 }
139
140 void display(void)
141 {
142    glClear (GL_COLOR_BUFFER_BIT);
143    triangle ();
144    glFlush ();
145    checkError ("display");
146 }
147
148 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
149                 GLfloat n, GLfloat f)
150 {
151    m[ 0] = 2.0f / (r - l);
152    m[ 1] = 0.0f;
153    m[ 2] = 0.0f;
154    m[ 3] = 0.0f;
155
156    m[ 4] = 0.0f;
157    m[ 5] = 2.0f / (t - b);
158    m[ 6] = 0.0f;
159    m[ 7] = 0.0f;
160
161    m[ 8] = 0.0f;
162    m[ 9] = 0.0f;
163    m[10] = -2.0f / (f - n);
164    m[11] = 0.0f;
165
166    m[12] = -(r + l) / (r - l);
167    m[13] = -(t + b) / (t - b);
168    m[14] = -(f + n) / (f - n);
169    m[15] = 1.0f;
170 }
171
172 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
173 {
174    loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
175 }
176
177 void reshape (int w, int h)
178 {
179    GLfloat m[16];
180    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
181    glMatrixMode (GL_PROJECTION);
182    if (w <= h) {
183       loadOrtho2Df (m, 0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w);
184    } else {
185       loadOrtho2Df (m, 0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0);
186    }
187    glLoadMatrixf (m);
188    glMatrixMode (GL_MODELVIEW);
189    checkError ("reshape");
190 }
191
192 void keyboard(unsigned char key, int x, int y)
193 {
194    switch (key) {
195       case 27:
196          exit(0);
197          break;
198    }
199 }
200
201 int main(int argc, char** argv)
202 {
203    glutInit(&argc, argv);
204    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
205    /* add command line argument "classic" for a pre-3.0 context */
206    if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
207       glutInitContextVersion (3, 0);
208       glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
209    }
210    glutInitWindowSize (500, 500); 
211    glutInitWindowPosition (100, 100);
212    glutCreateWindow (argv[0]);
213    init ();
214    glutDisplayFunc(display); 
215    glutReshapeFunc(reshape);
216    glutKeyboardFunc (keyboard);
217    glutMainLoop();
218    return 0;
219 }