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_VERTEX_SHADER
62 #define GL_VERTEX_SHADER 0x8B31
65 #ifndef GL_SHADING_LANGUAGE_VERSION
66 #define GL_SHADING_LANGUAGE_VERSION 0x8B8C
69 #ifndef GL_COMPILE_STATUS
70 #define GL_COMPILE_STATUS 0x8B81
73 #ifndef GL_LINK_STATUS
74 #define GL_LINK_STATUS 0x8B82
77 #ifndef GL_INFO_LOG_LENGTH
78 #define GL_INFO_LOG_LENGTH 0x8B84
81 typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
82 PFNGLGENBUFFERSPROC gl_GenBuffers;
84 typedef void (*PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
85 PFNGLBINDBUFFERPROC gl_BindBuffer;
87 typedef void (*PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size,
88 const GLvoid *data, GLenum usage);
89 PFNGLBUFFERDATAPROC gl_BufferData;
91 typedef GLuint (*PFNGLCREATESHADERPROC) (GLenum type);
92 PFNGLCREATESHADERPROC gl_CreateShader;
94 typedef void (*PFNGLSHADERSOURCEPROC)
95 (GLuint shader, GLsizei count, const GLchar **string, const GLint *length);
96 PFNGLSHADERSOURCEPROC gl_ShaderSource;
98 typedef void (*PFNGLCOMPILESHADERPROC) (GLuint shader);
99 PFNGLCOMPILESHADERPROC gl_CompileShader;
101 typedef GLuint (*PFNGLCREATEPROGRAMPROC) (void);
102 PFNGLCREATEPROGRAMPROC gl_CreateProgram;
104 typedef void (*PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
105 PFNGLATTACHSHADERPROC gl_AttachShader;
107 typedef void (*PFNGLLINKPROGRAMPROC) (GLuint program);
108 PFNGLLINKPROGRAMPROC gl_LinkProgram;
110 typedef void (*PFNGLUSEPROGRAMPROC) (GLuint program);
111 PFNGLUSEPROGRAMPROC gl_UseProgram;
113 typedef void (*PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
114 PFNGLGETSHADERIVPROC gl_GetShaderiv;
116 typedef void (*PFNGLGETSHADERINFOLOGPROC)
117 (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
118 PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
120 typedef void (*PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
121 PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
123 typedef void (*PFNGLGETPROGRAMINFOLOGPROC)
124 (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
125 PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
127 void initExtensionEntries(void)
129 gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
130 gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
131 gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
132 gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
133 gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
134 gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
135 gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
136 gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
137 gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
138 gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
139 gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
140 gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
141 gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
142 gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
145 /* vertex array data for a colored 2D triangle, consisting of RGB color values
146 and XY coordinates */
147 const GLfloat varray[] = {
148 1.0f, 0.0f, 0.0f, /* red */
149 5.0f, 5.0f, /* lower left */
151 0.0f, 1.0f, 0.0f, /* green */
152 25.0f, 5.0f, /* lower right */
154 0.0f, 0.0f, 1.0f, /* blue */
155 5.0f, 25.0f /* upper left */
158 /* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
160 numColorComponents = 3,
161 numVertexComponents = 2,
162 stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
163 numElements = sizeof(varray) / stride
166 /* the name of the vertex buffer object */
167 GLuint vertexBufferName;
169 void initBuffer(void)
171 gl_GenBuffers (1, &vertexBufferName);
172 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
173 gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
174 glEnableClientState (GL_COLOR_ARRAY);
175 glEnableClientState (GL_VERTEX_ARRAY);
176 checkError ("initBuffer");
179 const GLchar *vertexShaderSource[] = {
183 " gl_FrontColor = gl_Color;\n",
184 " gl_Position = ftransform();\n",
188 const GLchar *fragmentShaderSource[] = {
192 " gl_FragColor = gl_Color;\n",
196 void compileAndCheck(GLuint shader)
199 gl_CompileShader (shader);
200 gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
201 if (status == GL_FALSE) {
204 gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
205 infoLog = (GLchar*) malloc (infoLogLength);
206 gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
207 fprintf (stderr, "compile log: %s\n", infoLog);
212 GLuint compileShaderSource(GLenum type, GLsizei count, const GLchar **string)
214 GLuint shader = gl_CreateShader (type);
215 gl_ShaderSource (shader, count, string, NULL);
216 compileAndCheck (shader);
220 void linkAndCheck(GLuint program)
223 gl_LinkProgram (program);
224 gl_GetProgramiv (program, GL_LINK_STATUS, &status);
225 if (status == GL_FALSE) {
228 gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
229 infoLog = (GLchar*) malloc (infoLogLength);
230 gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
231 fprintf (stderr, "link log: %s\n", infoLog);
236 GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
238 GLuint program = gl_CreateProgram ();
239 if (vertexShader != 0) {
240 gl_AttachShader (program, vertexShader);
242 if (fragmentShader != 0) {
243 gl_AttachShader (program, fragmentShader);
245 linkAndCheck (program);
249 void initShader(void)
251 const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(GLchar*);
252 GLuint vertexShader =
253 compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
255 const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(GLchar*);
256 GLuint fragmentShader =
257 compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
259 GLuint program = createProgram (vertexShader, fragmentShader);
260 gl_UseProgram (program);
261 checkError ("initShader");
264 void initRendering(void)
266 glClearColor (0.0, 0.0, 0.0, 0.0);
267 glShadeModel (GL_SMOOTH);
268 checkError ("initRendering");
273 initExtensionEntries ();
281 printf ("Vendor: %s\n", glGetString (GL_VENDOR));
282 printf ("Renderer: %s\n", glGetString (GL_RENDERER));
283 printf ("Version: %s\n", glGetString (GL_VERSION));
284 printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
285 checkError ("dumpInfo");
288 const GLvoid *bufferObjectPtr (GLsizei index)
290 return (const GLvoid *) (((char *) NULL) + index);
295 gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
296 glColorPointer (numColorComponents, GL_FLOAT, stride, bufferObjectPtr (0));
297 glVertexPointer(numVertexComponents, GL_FLOAT, stride,
298 bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
299 glDrawArrays(GL_TRIANGLES, 0, numElements);
300 checkError ("triangle");
305 glClear (GL_COLOR_BUFFER_BIT);
308 checkError ("display");
311 void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
312 GLfloat n, GLfloat f)
314 m[ 0] = 2.0f / (r - l);
320 m[ 5] = 2.0f / (t - b);
326 m[10] = -2.0f / (f - n);
329 m[12] = -(r + l) / (r - l);
330 m[13] = -(t + b) / (t - b);
331 m[14] = -(f + n) / (f - n);
335 void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
337 loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
340 void reshape (int w, int h)
343 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
344 glMatrixMode (GL_PROJECTION);
346 loadOrtho2Df (m, 0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w);
348 loadOrtho2Df (m, 0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0);
351 glMatrixMode (GL_MODELVIEW);
352 checkError ("reshape");
355 void keyboard(unsigned char key, int x, int y)
364 int main(int argc, char** argv)
366 glutInit(&argc, argv);
367 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
368 /* add command line argument "classic" for a pre-3.0 context */
369 if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
370 glutInitContextVersion (3, 0);
371 glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
373 glutInitWindowSize (500, 500);
374 glutInitWindowPosition (100, 100);
375 glutCreateWindow (argv[0]);
378 glutDisplayFunc(display);
379 glutReshapeFunc(reshape);
380 glutKeyboardFunc (keyboard);