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) {
\r
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;
101 typedef void (APIENTRY *PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays);
\r
102 typedef void (APIENTRY *PFNGLBINDVERTEXARRAYPROC) (GLuint array);
103 typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
104 typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
105 typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);
106 typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
107 typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
108 typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
109 typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
110 typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
111 typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
112 typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
113 typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
114 typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
115 typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
116 typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
117 typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
118 typedef void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
119 typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
120 typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
121 typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
123 PFNGLGENVERTEXARRAYSPROC gl_GenVertexArrays;
\r
124 PFNGLBINDVERTEXARRAYPROC gl_BindVertexArray;
125 PFNGLGENBUFFERSPROC gl_GenBuffers;
126 PFNGLBINDBUFFERPROC gl_BindBuffer;
127 PFNGLBUFFERDATAPROC gl_BufferData;
128 PFNGLCREATESHADERPROC gl_CreateShader;
129 PFNGLSHADERSOURCEPROC gl_ShaderSource;
130 PFNGLCOMPILESHADERPROC gl_CompileShader;
131 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
132 PFNGLATTACHSHADERPROC gl_AttachShader;
133 PFNGLLINKPROGRAMPROC gl_LinkProgram;
134 PFNGLUSEPROGRAMPROC gl_UseProgram;
135 PFNGLGETSHADERIVPROC gl_GetShaderiv;
136 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
137 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
138 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
139 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
140 PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
141 PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
142 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
143 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
145 void initExtensionEntries(void)
147 gl_GenVertexArrays = (PFNGLGENVERTEXARRAYSPROC) glutGetProcAddress ("glGenVertexArrays");
\r
148 gl_BindVertexArray = (PFNGLBINDVERTEXARRAYPROC) glutGetProcAddress ("glBindVertexArray");
149 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
150 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
151 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
152 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
153 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
154 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
155 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
156 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
157 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
158 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
159 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
160 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
161 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
162 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
163 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
164 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
165 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
166 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
167 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
170 /* vertex array data for a colored 2D triangle, consisting of RGB color values
171 and XY coordinates */
172 const GLfloat varray[] = {
173 1.0f, 0.0f, 0.0f, /* red */
174 5.0f, 5.0f, /* lower left */
176 0.0f, 1.0f, 0.0f, /* green */
177 25.0f, 5.0f, /* lower right */
179 0.0f, 0.0f, 1.0f, /* blue */
180 5.0f, 25.0f /* upper left */
183 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
185 numColorComponents = 3,
186 numVertexComponents = 2,
187 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
188 numElements = sizeof(varray) / stride
191 /* the name of the vertex buffer object */
192 GLuint vertexBufferName;
193 GLuint vertexArrayName;
195 void initBuffer(void)
197 /* Need to setup a vertex array as otherwise invalid operation errors can
\r
198 * occur when accessing vertex buffer (OpenGL 3.3 has no default zero named
\r
201 gl_GenVertexArrays(1, &vertexArrayName);
\r
202 gl_BindVertexArray(vertexArrayName);
204 gl_GenBuffers (1, &vertexBufferName);
205 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
206 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
207 checkError ("initBuffer");
210 const ourGLchar *vertexShaderSource[] = {
212 "uniform mat4 fg_ProjectionMatrix;\n",
213 "in vec4 fg_Color;\n",
214 "in vec4 fg_Vertex;\n",
215 "smooth out vec4 fg_SmoothColor;\n",
218 " fg_SmoothColor = fg_Color;\n",
219 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
223 const ourGLchar *fragmentShaderSource[] = {
225 "smooth in vec4 fg_SmoothColor;\n",
226 "out vec4 fg_FragColor;\n",
229 " fg_FragColor = fg_SmoothColor;\n",
233 void compileAndCheck(GLuint shader)
236 gl_CompileShader (shader);
237 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
238 if (status == GL_FALSE) {
241 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
242 infoLog = (ourGLchar*) malloc (infoLogLength);
243 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
244 fprintf (stderr, "compile log: %s\n", infoLog);
249 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
251 GLuint shader = gl_CreateShader (type);
252 gl_ShaderSource (shader, count, string, NULL);
253 compileAndCheck (shader);
257 void linkAndCheck(GLuint program)
260 gl_LinkProgram (program);
261 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
262 if (status == GL_FALSE) {
265 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
266 infoLog = (ourGLchar*) malloc (infoLogLength);
267 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
268 fprintf (stderr, "link log: %s\n", infoLog);
273 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
275 GLuint program = gl_CreateProgram ();
276 if (vertexShader != 0) {
277 gl_AttachShader (program, vertexShader);
279 if (fragmentShader != 0) {
280 gl_AttachShader (program, fragmentShader);
282 linkAndCheck (program);
286 GLuint fgProjectionMatrixIndex;
288 GLuint fgVertexIndex;
290 void initShader(void)
292 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
293 GLuint vertexShader =
294 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
296 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
297 GLuint fragmentShader =
298 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
300 GLuint program = createProgram (vertexShader, fragmentShader);
302 gl_UseProgram (program);
304 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
306 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
307 gl_EnableVertexAttribArray (fgColorIndex);
309 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
310 gl_EnableVertexAttribArray (fgVertexIndex);
312 checkError ("initShader");
315 void initRendering(void)
317 glClearColor (0.0, 0.0, 0.0, 0.0);
318 checkError ("initRendering");
323 initExtensionEntries ();
331 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
332 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
333 printf ("Version: %s\n", glGetString (GL_VERSION));
334 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
335 checkError ("dumpInfo");
338 const GLvoid *bufferObjectPtr (GLsizei index)
340 return (const GLvoid *) (((char *) NULL) + index);
343 GLfloat projectionMatrix[16];
347 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
349 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
350 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
351 stride, bufferObjectPtr (0));
352 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
353 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
354 glDrawArrays(GL_TRIANGLES, 0, numElements);
355 checkError ("triangle");
360 glClear (GL_COLOR_BUFFER_BIT);
363 checkError ("display");
366 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
367 GLfloat n, GLfloat f)
369 m[ 0] = 2.0f / (r - l);
375 m[ 5] = 2.0f / (t - b);
381 m[10] = -2.0f / (f - n);
384 m[12] = -(r + l) / (r - l);
385 m[13] = -(t + b) / (t - b);
386 m[14] = -(f + n) / (f - n);
390 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
392 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
395 void reshape (int w, int h)
397 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
399 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
401 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
403 checkError ("reshape");
406 void keyboard(unsigned char key, int x, int y)
415 int main(int argc, char** argv)
417 glutInit(&argc, argv);
418 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
419 /* add command line argument "classic" for a pre-3.x context */
420 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
421 glutInitContextVersion (3, 1);
422 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
424 glutInitWindowSize (500, 500);
425 glutInitWindowPosition (100, 100);
426 glutCreateWindow (argv[0]);
429 glutDisplayFunc(display);
430 glutReshapeFunc(reshape);
431 glutKeyboardFunc (keyboard);