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.1-compliant.
5 * A smooth shaded polygon is drawn in a 2-D projection.
9 * Original copyright notice from smooth.c:
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:
19 * http://oss.sgi.com/projects/FreeB
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.
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.
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.
45 #include <GL/freeglut.h>
51 /* report GL errors, if any, to stderr */
52 void checkError(const char *functionName)
55 while (( error = glGetError() ) != GL_NO_ERROR) {
56 fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
60 /* extension #defines, types and entries, avoiding a dependency on additional
61 libraries like GLEW or the GL/glext.h header */
62 #ifndef GL_ARRAY_BUFFER
63 #define GL_ARRAY_BUFFER 0x8892
66 #ifndef GL_STATIC_DRAW
67 #define GL_STATIC_DRAW 0x88E4
70 #ifndef GL_FRAGMENT_SHADER
71 #define GL_FRAGMENT_SHADER 0x8B30
74 #ifndef GL_VERTEX_SHADER
75 #define GL_VERTEX_SHADER 0x8B31
78 #ifndef GL_SHADING_LANGUAGE_VERSION
79 #define GL_SHADING_LANGUAGE_VERSION 0x8B8C
82 #ifndef GL_COMPILE_STATUS
83 #define GL_COMPILE_STATUS 0x8B81
86 #ifndef GL_LINK_STATUS
87 #define GL_LINK_STATUS 0x8B82
90 #ifndef GL_INFO_LOG_LENGTH
91 #define GL_INFO_LOG_LENGTH 0x8B84
94 typedef ptrdiff_t ourGLsizeiptr;
95 typedef char ourGLchar;
102 #ifndef GL_ARB_vertex_array_object
103 typedef void (APIENTRY *PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays);
104 typedef void (APIENTRY *PFNGLBINDVERTEXARRAYPROC) (GLuint array);
106 #ifndef GL_VERSION_1_5
107 typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
108 typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
109 typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);
111 #ifndef GL_VERSION_2_0
112 typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
113 typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
114 typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
115 typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
116 typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
117 typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
118 typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
119 typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
120 typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
121 typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
122 typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
123 typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
124 typedef void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
125 typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
126 typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
127 typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
130 PFNGLGENVERTEXARRAYSPROC gl_GenVertexArrays;
131 PFNGLBINDVERTEXARRAYPROC gl_BindVertexArray;
132 PFNGLGENBUFFERSPROC gl_GenBuffers;
133 PFNGLBINDBUFFERPROC gl_BindBuffer;
134 PFNGLBUFFERDATAPROC gl_BufferData;
135 PFNGLCREATESHADERPROC gl_CreateShader;
136 PFNGLSHADERSOURCEPROC gl_ShaderSource;
137 PFNGLCOMPILESHADERPROC gl_CompileShader;
138 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
139 PFNGLATTACHSHADERPROC gl_AttachShader;
140 PFNGLLINKPROGRAMPROC gl_LinkProgram;
141 PFNGLUSEPROGRAMPROC gl_UseProgram;
142 PFNGLGETSHADERIVPROC gl_GetShaderiv;
143 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
144 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
145 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
146 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
147 PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
148 PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
149 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
150 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
152 void initExtensionEntries(void)
154 gl_GenVertexArrays = (PFNGLGENVERTEXARRAYSPROC) glutGetProcAddress ("glGenVertexArrays");
155 gl_BindVertexArray = (PFNGLBINDVERTEXARRAYPROC) glutGetProcAddress ("glBindVertexArray");
156 if (!gl_GenVertexArrays || !gl_BindVertexArray)
158 fprintf (stderr, "glGenVertexArrays or glBindVertexArray not found");
161 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
162 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
163 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
164 if (!gl_GenBuffers || !gl_BindBuffer || !gl_BufferData)
166 fprintf (stderr, "glGenBuffers, glBindBuffer or glBufferData not found");
169 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
170 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
171 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
172 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
173 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
174 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
175 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
176 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
177 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
178 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
179 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
180 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
181 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
182 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
183 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
184 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
185 if (!gl_CreateShader || !gl_ShaderSource || !gl_CompileShader || !gl_CreateProgram || !gl_AttachShader || !gl_LinkProgram || !gl_UseProgram || !gl_GetShaderiv || !gl_GetShaderInfoLog || !gl_GetProgramiv || !gl_GetProgramInfoLog || !gl_GetAttribLocation || !gl_VertexAttribPointer || !gl_EnableVertexAttribArray || !gl_GetUniformLocation || !gl_UniformMatrix4fv)
187 fprintf (stderr, "glCreateShader, glShaderSource, glCompileShader, glCreateProgram, glAttachShader, glLinkProgram, glUseProgram, glGetShaderiv, glGetShaderInfoLog, glGetProgramiv, glGetProgramInfoLog, glGetAttribLocation, glVertexAttribPointer, glEnableVertexAttribArray, glGetUniformLocation or glUniformMatrix4fv not found");
192 /* vertex array data for a colored 2D triangle, consisting of RGB color values
193 and XY coordinates */
194 const GLfloat varray[] = {
195 1.0f, 0.0f, 0.0f, /* red */
196 5.0f, 5.0f, /* lower left */
198 0.0f, 1.0f, 0.0f, /* green */
199 25.0f, 5.0f, /* lower right */
201 0.0f, 0.0f, 1.0f, /* blue */
202 5.0f, 25.0f /* upper left */
205 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
207 numColorComponents = 3,
208 numVertexComponents = 2,
209 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
210 numElements = sizeof(varray) / stride
213 /* the name of the vertex buffer object */
214 GLuint vertexBufferName;
215 GLuint vertexArrayName;
217 void initBuffer(void)
219 /* Need to setup a vertex array as otherwise invalid operation errors can
220 * occur when accessing vertex buffer (OpenGL 3.3 has no default zero named
223 gl_GenVertexArrays(1, &vertexArrayName);
224 gl_BindVertexArray(vertexArrayName);
226 gl_GenBuffers (1, &vertexBufferName);
227 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
228 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
229 checkError ("initBuffer");
232 const ourGLchar *vertexShaderSource[] = {
234 "uniform mat4 fg_ProjectionMatrix;\n",
235 "in vec4 fg_Color;\n",
236 "in vec4 fg_Vertex;\n",
237 "smooth out vec4 fg_SmoothColor;\n",
240 " fg_SmoothColor = fg_Color;\n",
241 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
245 const ourGLchar *fragmentShaderSource[] = {
247 "smooth in vec4 fg_SmoothColor;\n",
248 "out vec4 fg_FragColor;\n",
251 " fg_FragColor = fg_SmoothColor;\n",
255 void compileAndCheck(GLuint shader)
258 gl_CompileShader (shader);
259 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
260 if (status == GL_FALSE) {
263 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
264 infoLog = (ourGLchar*) malloc (infoLogLength);
265 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
266 fprintf (stderr, "compile log: %s\n", infoLog);
271 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
273 GLuint shader = gl_CreateShader (type);
274 gl_ShaderSource (shader, count, string, NULL);
275 compileAndCheck (shader);
279 void linkAndCheck(GLuint program)
282 gl_LinkProgram (program);
283 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
284 if (status == GL_FALSE) {
287 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
288 infoLog = (ourGLchar*) malloc (infoLogLength);
289 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
290 fprintf (stderr, "link log: %s\n", infoLog);
295 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
297 GLuint program = gl_CreateProgram ();
298 if (vertexShader != 0) {
299 gl_AttachShader (program, vertexShader);
301 if (fragmentShader != 0) {
302 gl_AttachShader (program, fragmentShader);
304 linkAndCheck (program);
308 GLuint fgProjectionMatrixIndex;
310 GLuint fgVertexIndex;
312 void initShader(void)
314 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
315 GLuint vertexShader =
316 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
318 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
319 GLuint fragmentShader =
320 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
322 GLuint program = createProgram (vertexShader, fragmentShader);
324 gl_UseProgram (program);
326 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
328 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
329 gl_EnableVertexAttribArray (fgColorIndex);
331 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
332 gl_EnableVertexAttribArray (fgVertexIndex);
334 checkError ("initShader");
337 void initRendering(void)
339 glClearColor (0.0, 0.0, 0.0, 0.0);
340 checkError ("initRendering");
345 initExtensionEntries ();
353 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
354 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
355 printf ("Version: %s\n", glGetString (GL_VERSION));
356 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
357 checkError ("dumpInfo");
360 const GLvoid *bufferObjectPtr (GLsizei index)
362 return (const GLvoid *) (((char *) NULL) + index);
365 GLfloat projectionMatrix[16];
369 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
371 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
372 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
373 stride, bufferObjectPtr (0));
374 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
375 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
376 glDrawArrays(GL_TRIANGLES, 0, numElements);
377 checkError ("triangle");
382 glClear (GL_COLOR_BUFFER_BIT);
385 checkError ("display");
388 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
389 GLfloat n, GLfloat f)
391 m[ 0] = 2.0f / (r - l);
397 m[ 5] = 2.0f / (t - b);
403 m[10] = -2.0f / (f - n);
406 m[12] = -(r + l) / (r - l);
407 m[13] = -(t + b) / (t - b);
408 m[14] = -(f + n) / (f - n);
412 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
414 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
417 void reshape (int w, int h)
419 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
421 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
423 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
425 checkError ("reshape");
428 void keyboard(unsigned char key, int x, int y)
437 int main(int argc, char** argv)
439 glutInit(&argc, argv);
440 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
441 /* add command line argument "classic" for a pre-3.x context */
442 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
443 glutInitContextVersion (3, 1);
444 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
446 glutInitWindowSize (500, 500);
447 glutInitWindowPosition (100, 100);
448 glutCreateWindow (argv[0]);
451 glutDisplayFunc(display);
452 glutReshapeFunc(reshape);
453 glutKeyboardFunc (keyboard);