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_VERTEX_SHADER
70 #define GL_VERTEX_SHADER 0x8B31
73 #ifndef GL_SHADING_LANGUAGE_VERSION
74 #define GL_SHADING_LANGUAGE_VERSION 0x8B8C
77 #ifndef GL_COMPILE_STATUS
78 #define GL_COMPILE_STATUS 0x8B81
81 #ifndef GL_LINK_STATUS
82 #define GL_LINK_STATUS 0x8B82
85 #ifndef GL_INFO_LOG_LENGTH
86 #define GL_INFO_LOG_LENGTH 0x8B84
89 typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
90 PFNGLGENBUFFERSPROC gl_GenBuffers;
92 typedef void (*PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
93 PFNGLBINDBUFFERPROC gl_BindBuffer;
95 typedef void (*PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size,
96 const GLvoid *data, GLenum usage);
97 PFNGLBUFFERDATAPROC gl_BufferData;
99 typedef GLuint (*PFNGLCREATESHADERPROC) (GLenum type);
100 PFNGLCREATESHADERPROC gl_CreateShader;
102 typedef void (*PFNGLSHADERSOURCEPROC)
103 (GLuint shader, GLsizei count, const GLchar **string, const GLint *length);
104 PFNGLSHADERSOURCEPROC gl_ShaderSource;
106 typedef void (*PFNGLCOMPILESHADERPROC) (GLuint shader);
107 PFNGLCOMPILESHADERPROC gl_CompileShader;
109 typedef GLuint (*PFNGLCREATEPROGRAMPROC) (void);
110 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
112 typedef void (*PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
113 PFNGLATTACHSHADERPROC gl_AttachShader;
115 typedef void (*PFNGLLINKPROGRAMPROC) (GLuint program);
116 PFNGLLINKPROGRAMPROC gl_LinkProgram;
118 typedef void (*PFNGLUSEPROGRAMPROC) (GLuint program);
119 PFNGLUSEPROGRAMPROC gl_UseProgram;
121 typedef void (*PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
122 PFNGLGETSHADERIVPROC gl_GetShaderiv;
124 typedef void (*PFNGLGETSHADERINFOLOGPROC)
125 (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
126 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
128 typedef void (*PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
129 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
131 typedef void (*PFNGLGETPROGRAMINFOLOGPROC)
132 (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
133 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
135 typedef GLint (*PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name);
136 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
138 typedef void (*PFNGLVERTEXATTRIBPOINTERPROC)
139 (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride,
140 const GLvoid *pointer);
141 PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
143 typedef void (*PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
144 PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
146 typedef GLint (*PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name);
147 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
149 typedef void (*PFNGLUNIFORMMATRIX4FVPROC)
150 (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
151 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
153 void initExtensionEntries(void)
155 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
156 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
157 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
158 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
159 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
160 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
161 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
162 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
163 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
164 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
165 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
166 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
167 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
168 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
169 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
170 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
171 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
172 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
173 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
176 /* vertex array data for a colored 2D triangle, consisting of RGB color values
177 and XY coordinates */
178 const GLfloat varray[] = {
179 1.0f, 0.0f, 0.0f, /* red */
180 5.0f, 5.0f, /* lower left */
182 0.0f, 1.0f, 0.0f, /* green */
183 25.0f, 5.0f, /* lower right */
185 0.0f, 0.0f, 1.0f, /* blue */
186 5.0f, 25.0f /* upper left */
189 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
191 numColorComponents = 3,
192 numVertexComponents = 2,
193 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
194 numElements = sizeof(varray) / stride
197 /* the name of the vertex buffer object */
198 GLuint vertexBufferName;
200 void initBuffer(void)
202 gl_GenBuffers (1, &vertexBufferName);
203 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
204 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
205 checkError ("initBuffer");
208 const GLchar *vertexShaderSource[] = {
210 "uniform mat4 fg_ProjectionMatrix;\n",
211 "in vec4 fg_Color;\n",
212 "in vec4 fg_Vertex;\n",
215 " gl_FrontColor = fg_Color;\n",
216 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
220 const GLchar *fragmentShaderSource[] = {
222 "out vec4 fg_FragColor;\n",
225 " fg_FragColor = gl_Color;\n",
229 void compileAndCheck(GLuint shader)
232 gl_CompileShader (shader);
233 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
234 if (status == GL_FALSE) {
237 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
238 infoLog = (GLchar*) malloc (infoLogLength);
239 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
240 fprintf (stderr, "compile log: %s\n", infoLog);
245 GLuint compileShaderSource(GLenum type, GLsizei count, const GLchar **string)
247 GLuint shader = gl_CreateShader (type);
248 gl_ShaderSource (shader, count, string, NULL);
249 compileAndCheck (shader);
253 void linkAndCheck(GLuint program)
256 gl_LinkProgram (program);
257 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
258 if (status == GL_FALSE) {
261 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
262 infoLog = (GLchar*) malloc (infoLogLength);
263 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
264 fprintf (stderr, "link log: %s\n", infoLog);
269 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
271 GLuint program = gl_CreateProgram ();
272 if (vertexShader != 0) {
273 gl_AttachShader (program, vertexShader);
275 if (fragmentShader != 0) {
276 gl_AttachShader (program, fragmentShader);
278 linkAndCheck (program);
282 GLuint fgProjectionMatrixIndex;
284 GLuint fgVertexIndex;
286 void initShader(void)
288 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(GLchar*);
289 GLuint vertexShader =
290 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
292 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(GLchar*);
293 GLuint fragmentShader =
294 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
296 GLuint program = createProgram (vertexShader, fragmentShader);
298 gl_UseProgram (program);
300 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
302 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
303 gl_EnableVertexAttribArray (fgColorIndex);
305 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
306 gl_EnableVertexAttribArray (fgVertexIndex);
308 checkError ("initShader");
311 void initRendering(void)
313 glClearColor (0.0, 0.0, 0.0, 0.0);
314 glShadeModel (GL_SMOOTH);
315 checkError ("initRendering");
320 initExtensionEntries ();
328 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
329 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
330 printf ("Version: %s\n", glGetString (GL_VERSION));
331 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
332 checkError ("dumpInfo");
335 const GLvoid *bufferObjectPtr (GLsizei index)
337 return (const GLvoid *) (((char *) NULL) + index);
340 GLfloat projectionMatrix[16];
344 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
346 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
347 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
348 stride, bufferObjectPtr (0));
349 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
350 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
351 glDrawArrays(GL_TRIANGLES, 0, numElements);
352 checkError ("triangle");
357 glClear (GL_COLOR_BUFFER_BIT);
360 checkError ("display");
363 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
364 GLfloat n, GLfloat f)
366 m[ 0] = 2.0f / (r - l);
372 m[ 5] = 2.0f / (t - b);
378 m[10] = -2.0f / (f - n);
381 m[12] = -(r + l) / (r - l);
382 m[13] = -(t + b) / (t - b);
383 m[14] = -(f + n) / (f - n);
387 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
389 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
392 void reshape (int w, int h)
394 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
396 loadOrtho2Df (projectionMatrix, 0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w);
398 loadOrtho2Df (projectionMatrix, 0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0);
400 checkError ("reshape");
403 void keyboard(unsigned char key, int x, int y)
412 int main(int argc, char** argv)
414 glutInit(&argc, argv);
415 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
416 /* add command line argument "classic" for a pre-3.0 context */
417 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
418 glutInitContextVersion (3, 0);
419 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
421 glutInitWindowSize (500, 500);
422 glutInitWindowPosition (100, 100);
423 glutCreateWindow (argv[0]);
426 glutDisplayFunc(display);
427 glutReshapeFunc(reshape);
428 glutKeyboardFunc (keyboard);