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 const GLubyte* sError = gluErrorString(error);
57 fprintf (stderr, "GL error 0x%X, %s, detected in %s\n", error, sError, functionName);
61 /* extension #defines, types and entries, avoiding a dependency on additional
62 libraries like GLEW or the GL/glext.h header */
63 #ifndef GL_ARRAY_BUFFER
64 #define GL_ARRAY_BUFFER 0x8892
67 #ifndef GL_STATIC_DRAW
68 #define GL_STATIC_DRAW 0x88E4
71 #ifndef GL_FRAGMENT_SHADER
72 #define GL_FRAGMENT_SHADER 0x8B30
75 #ifndef GL_VERTEX_SHADER
76 #define GL_VERTEX_SHADER 0x8B31
79 #ifndef GL_SHADING_LANGUAGE_VERSION
80 #define GL_SHADING_LANGUAGE_VERSION 0x8B8C
83 #ifndef GL_COMPILE_STATUS
84 #define GL_COMPILE_STATUS 0x8B81
87 #ifndef GL_LINK_STATUS
88 #define GL_LINK_STATUS 0x8B82
91 #ifndef GL_INFO_LOG_LENGTH
92 #define GL_INFO_LOG_LENGTH 0x8B84
95 typedef ptrdiff_t ourGLsizeiptr;
96 typedef char ourGLchar;
102 typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
103 typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
104 typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);
105 typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
106 typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
107 typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
108 typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
109 typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
110 typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
111 typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
112 typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
113 typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
114 typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
115 typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
116 typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
117 typedef void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
118 typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
119 typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
120 typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
122 PFNGLGENBUFFERSPROC gl_GenBuffers;
123 PFNGLBINDBUFFERPROC gl_BindBuffer;
124 PFNGLBUFFERDATAPROC gl_BufferData;
125 PFNGLCREATESHADERPROC gl_CreateShader;
126 PFNGLSHADERSOURCEPROC gl_ShaderSource;
127 PFNGLCOMPILESHADERPROC gl_CompileShader;
128 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
129 PFNGLATTACHSHADERPROC gl_AttachShader;
130 PFNGLLINKPROGRAMPROC gl_LinkProgram;
131 PFNGLUSEPROGRAMPROC gl_UseProgram;
132 PFNGLGETSHADERIVPROC gl_GetShaderiv;
133 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
134 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
135 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
136 PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
137 PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
138 PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
139 PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
140 PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
142 void initExtensionEntries(void)
144 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
145 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
146 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
147 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
148 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
149 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
150 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
151 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
152 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
153 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
154 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
155 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
156 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
157 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
158 gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
159 gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
160 gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
161 gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
162 gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
165 /* vertex array data for a colored 2D triangle, consisting of RGB color values
166 and XY coordinates */
167 const GLfloat varray[] = {
168 1.0f, 0.0f, 0.0f, /* red */
169 5.0f, 5.0f, /* lower left */
171 0.0f, 1.0f, 0.0f, /* green */
172 25.0f, 5.0f, /* lower right */
174 0.0f, 0.0f, 1.0f, /* blue */
175 5.0f, 25.0f /* upper left */
178 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
180 numColorComponents = 3,
181 numVertexComponents = 2,
182 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
183 numElements = sizeof(varray) / stride
186 /* the name of the vertex buffer object */
187 GLuint vertexBufferName;
189 void initBuffer(void)
191 gl_GenBuffers (1, &vertexBufferName);
192 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
193 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
194 checkError ("initBuffer");
197 const ourGLchar *vertexShaderSource[] = {
199 "uniform mat4 fg_ProjectionMatrix;\n",
200 "in vec4 fg_Color;\n",
201 "in vec4 fg_Vertex;\n",
202 "smooth out vec4 fg_SmoothColor;\n",
205 " fg_SmoothColor = fg_Color;\n",
206 " gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
210 const ourGLchar *fragmentShaderSource[] = {
212 "smooth in vec4 fg_SmoothColor;\n",
213 "out vec4 fg_FragColor;\n",
216 " fg_FragColor = fg_SmoothColor;\n",
220 void compileAndCheck(GLuint shader)
223 gl_CompileShader (shader);
224 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
225 if (status == GL_FALSE) {
228 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
229 infoLog = (ourGLchar*) malloc (infoLogLength);
230 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
231 fprintf (stderr, "compile log: %s\n", infoLog);
236 GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
238 GLuint shader = gl_CreateShader (type);
239 gl_ShaderSource (shader, count, string, NULL);
240 compileAndCheck (shader);
244 void linkAndCheck(GLuint program)
247 gl_LinkProgram (program);
248 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
249 if (status == GL_FALSE) {
252 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
253 infoLog = (ourGLchar*) malloc (infoLogLength);
254 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
255 fprintf (stderr, "link log: %s\n", infoLog);
260 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
262 GLuint program = gl_CreateProgram ();
263 if (vertexShader != 0) {
264 gl_AttachShader (program, vertexShader);
266 if (fragmentShader != 0) {
267 gl_AttachShader (program, fragmentShader);
269 linkAndCheck (program);
273 GLuint fgProjectionMatrixIndex;
275 GLuint fgVertexIndex;
277 void initShader(void)
279 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
280 GLuint vertexShader =
281 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
283 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
284 GLuint fragmentShader =
285 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
287 GLuint program = createProgram (vertexShader, fragmentShader);
289 gl_UseProgram (program);
291 fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
293 fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
294 gl_EnableVertexAttribArray (fgColorIndex);
296 fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
297 gl_EnableVertexAttribArray (fgVertexIndex);
299 checkError ("initShader");
302 void initRendering(void)
304 glClearColor (0.0, 0.0, 0.0, 0.0);
305 checkError ("initRendering");
310 initExtensionEntries ();
318 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
319 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
320 printf ("Version: %s\n", glGetString (GL_VERSION));
321 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
322 checkError ("dumpInfo");
325 const GLvoid *bufferObjectPtr (GLsizei index)
327 return (const GLvoid *) (((char *) NULL) + index);
330 GLfloat projectionMatrix[16];
334 gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
336 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
337 gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
338 stride, bufferObjectPtr (0));
339 gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
340 stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
341 glDrawArrays(GL_TRIANGLES, 0, numElements);
342 checkError ("triangle");
347 glClear (GL_COLOR_BUFFER_BIT);
350 checkError ("display");
353 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
354 GLfloat n, GLfloat f)
356 m[ 0] = 2.0f / (r - l);
362 m[ 5] = 2.0f / (t - b);
368 m[10] = -2.0f / (f - n);
371 m[12] = -(r + l) / (r - l);
372 m[13] = -(t + b) / (t - b);
373 m[14] = -(f + n) / (f - n);
377 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
379 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
382 void reshape (int w, int h)
384 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
386 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
388 loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
390 checkError ("reshape");
393 void keyboard(unsigned char key, int x, int y)
402 int main(int argc, char** argv)
404 glutInit(&argc, argv);
405 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
406 /* add command line argument "classic" for a pre-3.x context */
407 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
408 glutInitContextVersion (3, 1);
409 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
411 glutInitWindowSize (500, 500);
412 glutInitWindowPosition (100, 100);
413 glutCreateWindow (argv[0]);
416 glutDisplayFunc(display);
417 glutReshapeFunc(reshape);
418 glutKeyboardFunc (keyboard);