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.
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>
50 /* report GL errors, if any, to stderr */
51 void checkError(const char *functionName)
54 while (( error = glGetError() ) != GL_NO_ERROR) {
55 fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
59 /* extension #defines, types and entries, avoiding a dependency on additional
60 libraries like GLEW or the GL/glext.h header */
61 #ifndef GL_ARRAY_BUFFER
62 #define GL_ARRAY_BUFFER 0x8892
65 #ifndef GL_STATIC_DRAW
66 #define GL_STATIC_DRAW 0x88E4
69 #ifndef GL_FRAGMENT_SHADER
70 #define GL_FRAGMENT_SHADER 0x8B30
73 #ifndef GL_VERTEX_SHADER
74 #define GL_VERTEX_SHADER 0x8B31
77 #ifndef GL_SHADING_LANGUAGE_VERSION
78 #define GL_SHADING_LANGUAGE_VERSION 0x8B8C
81 #ifndef GL_COMPILE_STATUS
82 #define GL_COMPILE_STATUS 0x8B81
85 #ifndef GL_LINK_STATUS
86 #define GL_LINK_STATUS 0x8B82
89 #ifndef GL_INFO_LOG_LENGTH
90 #define GL_INFO_LOG_LENGTH 0x8B84
93 typedef ptrdiff_t ourGLsizeiptr;
94 typedef char ourGLchar;
96 typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
97 PFNGLGENBUFFERSPROC gl_GenBuffers;
99 typedef void (*PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
100 PFNGLBINDBUFFERPROC gl_BindBuffer;
102 typedef void (*PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size,
103 const GLvoid *data, GLenum usage);
104 PFNGLBUFFERDATAPROC gl_BufferData;
106 typedef GLuint (*PFNGLCREATESHADERPROC) (GLenum type);
107 PFNGLCREATESHADERPROC gl_CreateShader;
109 typedef void (*PFNGLSHADERSOURCEPROC)
110 (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
111 PFNGLSHADERSOURCEPROC gl_ShaderSource;
113 typedef void (*PFNGLCOMPILESHADERPROC) (GLuint shader);
114 PFNGLCOMPILESHADERPROC gl_CompileShader;
116 typedef GLuint (*PFNGLCREATEPROGRAMPROC) (void);
117 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
119 typedef void (*PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
120 PFNGLATTACHSHADERPROC gl_AttachShader;
122 typedef void (*PFNGLLINKPROGRAMPROC) (GLuint program);
123 PFNGLLINKPROGRAMPROC gl_LinkProgram;
125 typedef void (*PFNGLUSEPROGRAMPROC) (GLuint program);
126 PFNGLUSEPROGRAMPROC gl_UseProgram;
128 typedef void (*PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
129 PFNGLGETSHADERIVPROC gl_GetShaderiv;
131 typedef void (*PFNGLGETSHADERINFOLOGPROC)
132 (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
133 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
135 typedef void (*PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
136 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
138 typedef void (*PFNGLGETPROGRAMINFOLOGPROC)
139 (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
140 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
142 typedef GLint (*PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
143 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
145 typedef void (*PFNGLVERTEXATTRIBPOINTERPROC)
146 (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride,
147 const GLvoid *pointer);
148 PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
150 typedef void (*PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
151 PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
153 typedef GLint (*PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
154 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
156 typedef void (*PFNGLUNIFORMMATRIX4FVPROC)
157 (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
158 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
160 void initExtensionEntries(void)
162 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
163 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
164 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
165 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
166 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
167 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
168 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
169 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
170 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
171 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
172 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
173 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
174 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
175 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
176 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
177 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
178 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
179 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
180 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
183 /* vertex array data for a colored 2D triangle, consisting of RGB color values
184 and XY coordinates */
185 const GLfloat varray[] = {
186 1.0f, 0.0f, 0.0f, /* red */
187 5.0f, 5.0f, /* lower left */
189 0.0f, 1.0f, 0.0f, /* green */
190 25.0f, 5.0f, /* lower right */
192 0.0f, 0.0f, 1.0f, /* blue */
193 5.0f, 25.0f /* upper left */
196 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
198 numColorComponents = 3,
199 numVertexComponents = 2,
200 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
201 numElements = sizeof(varray) / stride
204 /* the name of the vertex buffer object */
205 GLuint vertexBufferName;
207 void initBuffer(void)
209 gl_GenBuffers (1, &vertexBufferName);
210 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
211 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
212 checkError ("initBuffer");
215 const ourGLchar *vertexShaderSource[] = {
217 "uniform mat4 fg_ProjectionMatrix;\n",
218 "in vec4 fg_Color;\n",
219 "in vec4 fg_Vertex;\n",
222 " gl_FrontColor = fg_Color;\n",
223 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
227 const ourGLchar *fragmentShaderSource[] = {
229 "out vec4 fg_FragColor;\n",
232 " fg_FragColor = gl_Color;\n",
236 void compileAndCheck(GLuint shader)
239 gl_CompileShader (shader);
240 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
241 if (status == GL_FALSE) {
244 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
245 infoLog = (ourGLchar*) malloc (infoLogLength);
246 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
247 fprintf (stderr, "compile log: %s\n", infoLog);
252 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
254 GLuint shader = gl_CreateShader (type);
255 gl_ShaderSource (shader, count, string, NULL);
256 compileAndCheck (shader);
260 void linkAndCheck(GLuint program)
263 gl_LinkProgram (program);
264 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
265 if (status == GL_FALSE) {
268 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
269 infoLog = (ourGLchar*) malloc (infoLogLength);
270 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
271 fprintf (stderr, "link log: %s\n", infoLog);
276 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
278 GLuint program = gl_CreateProgram ();
279 if (vertexShader != 0) {
280 gl_AttachShader (program, vertexShader);
282 if (fragmentShader != 0) {
283 gl_AttachShader (program, fragmentShader);
285 linkAndCheck (program);
289 GLuint fgProjectionMatrixIndex;
291 GLuint fgVertexIndex;
293 void initShader(void)
295 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
296 GLuint vertexShader =
297 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
299 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
300 GLuint fragmentShader =
301 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
303 GLuint program = createProgram (vertexShader, fragmentShader);
305 gl_UseProgram (program);
307 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
309 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
310 gl_EnableVertexAttribArray (fgColorIndex);
312 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
313 gl_EnableVertexAttribArray (fgVertexIndex);
315 checkError ("initShader");
318 void initRendering(void)
320 glClearColor (0.0, 0.0, 0.0, 0.0);
321 glShadeModel (GL_SMOOTH);
322 checkError ("initRendering");
327 initExtensionEntries ();
335 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
336 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
337 printf ("Version: %s\n", glGetString (GL_VERSION));
338 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
339 checkError ("dumpInfo");
342 const GLvoid *bufferObjectPtr (GLsizei index)
344 return (const GLvoid *) (((char *) NULL) + index);
347 GLfloat projectionMatrix[16];
351 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
353 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
354 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
355 stride, bufferObjectPtr (0));
356 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
357 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
358 glDrawArrays(GL_TRIANGLES, 0, numElements);
359 checkError ("triangle");
364 glClear (GL_COLOR_BUFFER_BIT);
367 checkError ("display");
370 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
371 GLfloat n, GLfloat f)
373 m[ 0] = 2.0f / (r - l);
379 m[ 5] = 2.0f / (t - b);
385 m[10] = -2.0f / (f - n);
388 m[12] = -(r + l) / (r - l);
389 m[13] = -(t + b) / (t - b);
390 m[14] = -(f + n) / (f - n);
394 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
396 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
399 void reshape (int w, int h)
401 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
403 loadOrtho2Df (projectionMatrix, 0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w);
405 loadOrtho2Df (projectionMatrix, 0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0);
407 checkError ("reshape");
410 void keyboard(unsigned char key, int x, int y)
419 int main(int argc, char** argv)
421 glutInit(&argc, argv);
422 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
423 /* add command line argument "classic" for a pre-3.0 context */
424 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
425 glutInitContextVersion (3, 0);
426 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
428 glutInitWindowSize (500, 500);
429 glutInitWindowPosition (100, 100);
430 glutCreateWindow (argv[0]);
433 glutDisplayFunc(display);
434 glutReshapeFunc(reshape);
435 glutKeyboardFunc (keyboard);