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;
101 typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
102 typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
103 typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);
104 typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
105 typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
106 typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
107 typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
108 typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
109 typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
110 typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
111 typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
112 typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
113 typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
114 typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
115 typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
116 typedef void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
117 typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
118 typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
119 typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
121 PFNGLGENBUFFERSPROC gl_GenBuffers;
122 PFNGLBINDBUFFERPROC gl_BindBuffer;
123 PFNGLBUFFERDATAPROC gl_BufferData;
124 PFNGLCREATESHADERPROC gl_CreateShader;
125 PFNGLSHADERSOURCEPROC gl_ShaderSource;
126 PFNGLCOMPILESHADERPROC gl_CompileShader;
127 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
128 PFNGLATTACHSHADERPROC gl_AttachShader;
129 PFNGLLINKPROGRAMPROC gl_LinkProgram;
130 PFNGLUSEPROGRAMPROC gl_UseProgram;
131 PFNGLGETSHADERIVPROC gl_GetShaderiv;
132 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
133 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
134 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
135 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
136 PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
137 PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
138 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
139 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
141 void initExtensionEntries(void)
143 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
144 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
145 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
146 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
147 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
148 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
149 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
150 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
151 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
152 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
153 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
154 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
155 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
156 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
157 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
158 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
159 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
160 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
161 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
164 /* vertex array data for a colored 2D triangle, consisting of RGB color values
165 and XY coordinates */
166 const GLfloat varray[] = {
167 1.0f, 0.0f, 0.0f, /* red */
168 5.0f, 5.0f, /* lower left */
170 0.0f, 1.0f, 0.0f, /* green */
171 25.0f, 5.0f, /* lower right */
173 0.0f, 0.0f, 1.0f, /* blue */
174 5.0f, 25.0f /* upper left */
177 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
179 numColorComponents = 3,
180 numVertexComponents = 2,
181 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
182 numElements = sizeof(varray) / stride
185 /* the name of the vertex buffer object */
186 GLuint vertexBufferName;
188 void initBuffer(void)
190 gl_GenBuffers (1, &vertexBufferName);
191 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
192 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
193 checkError ("initBuffer");
196 const ourGLchar *vertexShaderSource[] = {
198 "uniform mat4 fg_ProjectionMatrix;\n",
199 "in vec4 fg_Color;\n",
200 "in vec4 fg_Vertex;\n",
201 "smooth out vec4 fg_SmoothColor;\n",
204 " fg_SmoothColor = fg_Color;\n",
205 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
209 const ourGLchar *fragmentShaderSource[] = {
211 "smooth in vec4 fg_SmoothColor;\n",
212 "out vec4 fg_FragColor;\n",
215 " fg_FragColor = fg_SmoothColor;\n",
219 void compileAndCheck(GLuint shader)
222 gl_CompileShader (shader);
223 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
224 if (status == GL_FALSE) {
227 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
228 infoLog = (ourGLchar*) malloc (infoLogLength);
229 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
230 fprintf (stderr, "compile log: %s\n", infoLog);
235 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
237 GLuint shader = gl_CreateShader (type);
238 gl_ShaderSource (shader, count, string, NULL);
239 compileAndCheck (shader);
243 void linkAndCheck(GLuint program)
246 gl_LinkProgram (program);
247 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
248 if (status == GL_FALSE) {
251 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
252 infoLog = (ourGLchar*) malloc (infoLogLength);
253 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
254 fprintf (stderr, "link log: %s\n", infoLog);
259 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
261 GLuint program = gl_CreateProgram ();
262 if (vertexShader != 0) {
263 gl_AttachShader (program, vertexShader);
265 if (fragmentShader != 0) {
266 gl_AttachShader (program, fragmentShader);
268 linkAndCheck (program);
272 GLuint fgProjectionMatrixIndex;
274 GLuint fgVertexIndex;
276 void initShader(void)
278 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
279 GLuint vertexShader =
280 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
282 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
283 GLuint fragmentShader =
284 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
286 GLuint program = createProgram (vertexShader, fragmentShader);
288 gl_UseProgram (program);
290 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
292 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
293 gl_EnableVertexAttribArray (fgColorIndex);
295 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
296 gl_EnableVertexAttribArray (fgVertexIndex);
298 checkError ("initShader");
301 void initRendering(void)
303 glClearColor (0.0, 0.0, 0.0, 0.0);
304 checkError ("initRendering");
309 initExtensionEntries ();
317 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
318 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
319 printf ("Version: %s\n", glGetString (GL_VERSION));
320 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
321 checkError ("dumpInfo");
324 const GLvoid *bufferObjectPtr (GLsizei index)
326 return (const GLvoid *) (((char *) NULL) + index);
329 GLfloat projectionMatrix[16];
333 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
335 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
336 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
337 stride, bufferObjectPtr (0));
338 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
339 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
340 glDrawArrays(GL_TRIANGLES, 0, numElements);
341 checkError ("triangle");
346 glClear (GL_COLOR_BUFFER_BIT);
349 checkError ("display");
352 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
353 GLfloat n, GLfloat f)
355 m[ 0] = 2.0f / (r - l);
361 m[ 5] = 2.0f / (t - b);
367 m[10] = -2.0f / (f - n);
370 m[12] = -(r + l) / (r - l);
371 m[13] = -(t + b) / (t - b);
372 m[14] = -(f + n) / (f - n);
376 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
378 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
381 void reshape (int w, int h)
383 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
385 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
387 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
389 checkError ("reshape");
392 void keyboard(unsigned char key, int x, int y)
401 int main(int argc, char** argv)
403 glutInit(&argc, argv);
404 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
405 /* add command line argument "classic" for a pre-3.x context */
406 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
407 glutInitContextVersion (3, 1);
408 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
410 glutInitWindowSize (500, 500);
411 glutInitWindowPosition (100, 100);
412 glutCreateWindow (argv[0]);
415 glutDisplayFunc(display);
416 glutReshapeFunc(reshape);
417 glutKeyboardFunc (keyboard);