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 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
157 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
158 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
159 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
160 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
161 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
162 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
163 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
164 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
165 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
166 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
167 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
168 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
169 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
170 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
171 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
172 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
173 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
174 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
177 /* vertex array data for a colored 2D triangle, consisting of RGB color values
178 and XY coordinates */
179 const GLfloat varray[] = {
180 1.0f, 0.0f, 0.0f, /* red */
181 5.0f, 5.0f, /* lower left */
183 0.0f, 1.0f, 0.0f, /* green */
184 25.0f, 5.0f, /* lower right */
186 0.0f, 0.0f, 1.0f, /* blue */
187 5.0f, 25.0f /* upper left */
190 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
192 numColorComponents = 3,
193 numVertexComponents = 2,
194 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
195 numElements = sizeof(varray) / stride
198 /* the name of the vertex buffer object */
199 GLuint vertexBufferName;
200 GLuint vertexArrayName;
202 void initBuffer(void)
204 /* Need to setup a vertex array as otherwise invalid operation errors can
205 * occur when accessing vertex buffer (OpenGL 3.3 has no default zero named
208 gl_GenVertexArrays(1, &vertexArrayName);
209 gl_BindVertexArray(vertexArrayName);
211 gl_GenBuffers (1, &vertexBufferName);
212 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
213 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
214 checkError ("initBuffer");
217 const ourGLchar *vertexShaderSource[] = {
219 "uniform mat4 fg_ProjectionMatrix;\n",
220 "in vec4 fg_Color;\n",
221 "in vec4 fg_Vertex;\n",
222 "smooth out vec4 fg_SmoothColor;\n",
225 " fg_SmoothColor = fg_Color;\n",
226 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
230 const ourGLchar *fragmentShaderSource[] = {
232 "smooth in vec4 fg_SmoothColor;\n",
233 "out vec4 fg_FragColor;\n",
236 " fg_FragColor = fg_SmoothColor;\n",
240 void compileAndCheck(GLuint shader)
243 gl_CompileShader (shader);
244 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
245 if (status == GL_FALSE) {
248 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
249 infoLog = (ourGLchar*) malloc (infoLogLength);
250 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
251 fprintf (stderr, "compile log: %s\n", infoLog);
256 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
258 GLuint shader = gl_CreateShader (type);
259 gl_ShaderSource (shader, count, string, NULL);
260 compileAndCheck (shader);
264 void linkAndCheck(GLuint program)
267 gl_LinkProgram (program);
268 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
269 if (status == GL_FALSE) {
272 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
273 infoLog = (ourGLchar*) malloc (infoLogLength);
274 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
275 fprintf (stderr, "link log: %s\n", infoLog);
280 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
282 GLuint program = gl_CreateProgram ();
283 if (vertexShader != 0) {
284 gl_AttachShader (program, vertexShader);
286 if (fragmentShader != 0) {
287 gl_AttachShader (program, fragmentShader);
289 linkAndCheck (program);
293 GLuint fgProjectionMatrixIndex;
295 GLuint fgVertexIndex;
297 void initShader(void)
299 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
300 GLuint vertexShader =
301 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
303 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
304 GLuint fragmentShader =
305 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
307 GLuint program = createProgram (vertexShader, fragmentShader);
309 gl_UseProgram (program);
311 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
313 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
314 gl_EnableVertexAttribArray (fgColorIndex);
316 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
317 gl_EnableVertexAttribArray (fgVertexIndex);
319 checkError ("initShader");
322 void initRendering(void)
324 glClearColor (0.0, 0.0, 0.0, 0.0);
325 checkError ("initRendering");
330 initExtensionEntries ();
338 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
339 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
340 printf ("Version: %s\n", glGetString (GL_VERSION));
341 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
342 checkError ("dumpInfo");
345 const GLvoid *bufferObjectPtr (GLsizei index)
347 return (const GLvoid *) (((char *) NULL) + index);
350 GLfloat projectionMatrix[16];
354 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
356 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
357 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
358 stride, bufferObjectPtr (0));
359 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
360 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
361 glDrawArrays(GL_TRIANGLES, 0, numElements);
362 checkError ("triangle");
367 glClear (GL_COLOR_BUFFER_BIT);
370 checkError ("display");
373 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
374 GLfloat n, GLfloat f)
376 m[ 0] = 2.0f / (r - l);
382 m[ 5] = 2.0f / (t - b);
388 m[10] = -2.0f / (f - n);
391 m[12] = -(r + l) / (r - l);
392 m[13] = -(t + b) / (t - b);
393 m[14] = -(f + n) / (f - n);
397 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
399 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
402 void reshape (int w, int h)
404 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
406 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
408 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
410 checkError ("reshape");
413 void keyboard(unsigned char key, int x, int y)
422 int main(int argc, char** argv)
424 glutInit(&argc, argv);
425 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
426 /* add command line argument "classic" for a pre-3.x context */
427 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
428 glutInitContextVersion (3, 1);
429 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
431 glutInitWindowSize (500, 500);
432 glutInitWindowPosition (100, 100);
433 glutCreateWindow (argv[0]);
436 glutDisplayFunc(display);
437 glutReshapeFunc(reshape);
438 glutKeyboardFunc (keyboard);