/*! \file shapes.c
\ingroup demos
This program is a test harness for the various shapes
in OpenGLUT. It may also be useful to see which
parameters control what behavior in the OpenGLUT
objects.
Spinning wireframe and solid-shaded shapes are
displayed. Some parameters can be adjusted.
Keys:
- <tt>Esc </tt> Quit
- <tt>q Q </tt> Quit
- <tt>i I </tt> Show info
- <tt>p P </tt> Toggle perspective or orthographic projection
- <tt>r R </tt> Toggle fixed or animated rotation around model X-axis
- <tt>s S </tt> Toggle toggle fixed function or shader render path
- <tt>n N </tt> Toggle visualization of object's normal vectors
- <tt>= + </tt> Increase \a slices
- <tt>- _ </tt> Decreate \a slices
- <tt>, < </tt> Decreate \a stacks
- <tt>. > </tt> Increase \a stacks
- <tt>9 ( </tt> Decreate \a depth (Sierpinski Sponge)
- <tt>0 ) </tt> Increase \a depth (Sierpinski Sponge)
- <tt>up </tt> Increase "outer radius"
- <tt>down </tt> Decrease "outer radius"
- <tt>left </tt> Decrease "inner radius"
- <tt>right</tt> Increase "inner radius"
- <tt>PgUp </tt> Next shape-drawing function
- <tt>PgDn </tt> Prev shape-drawing function
\author Written by Nigel Stewart November 2003
\author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
OpenGLUT branched from freeglut in February, 2004.
\image html openglut_shapes.png OpenGLUT Geometric Shapes Demonstration
\include demos/shapes/shapes.c
*/
#include <GL/freeglut.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "glmatrix.h"
#ifdef _MSC_VER
/* DUMP MEMORY LEAKS */
#include <crtdbg.h>
#endif
/* report GL errors, if any, to stderr */
void checkError(const char *functionName)
{
GLenum error;
while (( error = glGetError() ) != GL_NO_ERROR) {
fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
}
}
/*
* OpenGL 2+ shader mode needs some function and macro definitions,
* avoiding a dependency on additional libraries like GLEW or the
* GL/glext.h header
*/
#ifndef GL_FRAGMENT_SHADER
#define GL_FRAGMENT_SHADER 0x8B30
#endif
#ifndef GL_VERTEX_SHADER
#define GL_VERTEX_SHADER 0x8B31
#endif
#ifndef GL_COMPILE_STATUS
#define GL_COMPILE_STATUS 0x8B81
#endif
#ifndef GL_LINK_STATUS
#define GL_LINK_STATUS 0x8B82
#endif
#ifndef GL_INFO_LOG_LENGTH
#define GL_INFO_LOG_LENGTH 0x8B84
#endif
typedef ptrdiff_t ourGLsizeiptr;
typedef char ourGLchar;
#ifndef APIENTRY
#define APIENTRY
#endif
#ifndef GL_VERSION_2_0
typedef GLuint (APIENTRY *PFNGLCREATESHADERPROC) (GLenum type);
typedef void (APIENTRY *PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const ourGLchar **string, const GLint *length);
typedef void (APIENTRY *PFNGLCOMPILESHADERPROC) (GLuint shader);
typedef GLuint (APIENTRY *PFNGLCREATEPROGRAMPROC) (void);
typedef void (APIENTRY *PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
typedef void (APIENTRY *PFNGLLINKPROGRAMPROC) (GLuint program);
typedef void (APIENTRY *PFNGLUSEPROGRAMPROC) (GLuint program);
typedef void (APIENTRY *PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
typedef void (APIENTRY *PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
typedef void (APIENTRY *PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
typedef void (APIENTRY *PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, ourGLchar *infoLog);
typedef GLint (APIENTRY *PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const ourGLchar *name);
typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
typedef void (APIENTRY *PFNGLUNIFORMMATRIX3FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
#endif
PFNGLCREATESHADERPROC gl_CreateShader;
PFNGLSHADERSOURCEPROC gl_ShaderSource;
PFNGLCOMPILESHADERPROC gl_CompileShader;
PFNGLCREATEPROGRAMPROC gl_CreateProgram;
PFNGLATTACHSHADERPROC gl_AttachShader;
PFNGLLINKPROGRAMPROC gl_LinkProgram;
PFNGLUSEPROGRAMPROC gl_UseProgram;
PFNGLGETSHADERIVPROC gl_GetShaderiv;
PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
PFNGLGETATTRIBLOCATIONPROC gl_GetAttribLocation;
PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
PFNGLUNIFORMMATRIX3FVPROC gl_UniformMatrix3fv;
void initExtensionEntries(void)
{
gl_CreateShader = (PFNGLCREATESHADERPROC) glutGetProcAddress ("glCreateShader");
gl_ShaderSource = (PFNGLSHADERSOURCEPROC) glutGetProcAddress ("glShaderSource");
gl_CompileShader = (PFNGLCOMPILESHADERPROC) glutGetProcAddress ("glCompileShader");
gl_CreateProgram = (PFNGLCREATEPROGRAMPROC) glutGetProcAddress ("glCreateProgram");
gl_AttachShader = (PFNGLATTACHSHADERPROC) glutGetProcAddress ("glAttachShader");
gl_LinkProgram = (PFNGLLINKPROGRAMPROC) glutGetProcAddress ("glLinkProgram");
gl_UseProgram = (PFNGLUSEPROGRAMPROC) glutGetProcAddress ("glUseProgram");
gl_GetShaderiv = (PFNGLGETSHADERIVPROC) glutGetProcAddress ("glGetShaderiv");
gl_GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC) glutGetProcAddress ("glGetShaderInfoLog");
gl_GetProgramiv = (PFNGLGETPROGRAMIVPROC) glutGetProcAddress ("glGetProgramiv");
gl_GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC) glutGetProcAddress ("glGetProgramInfoLog");
gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
gl_UniformMatrix3fv = (PFNGLUNIFORMMATRIX3FVPROC) glutGetProcAddress ("glUniformMatrix3fv");
if (!gl_CreateShader || !gl_ShaderSource || !gl_CompileShader || !gl_CreateProgram || !gl_AttachShader || !gl_LinkProgram || !gl_UseProgram || !gl_GetShaderiv || !gl_GetShaderInfoLog || !gl_GetProgramiv || !gl_GetProgramInfoLog || !gl_GetAttribLocation || !gl_GetUniformLocation || !gl_UniformMatrix4fv || !gl_UniformMatrix3fv)
{
fprintf (stderr, "glCreateShader, glShaderSource, glCompileShader, glCreateProgram, glAttachShader, glLinkProgram, glUseProgram, glGetShaderiv, glGetShaderInfoLog, glGetProgramiv, glGetProgramInfoLog, glGetAttribLocation, glGetUniformLocation, glUniformMatrix4fv or gl_UniformMatrix3fv not found");
exit(1);
}
}
const ourGLchar *vertexShaderSource[] = {
"/**",
" * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/GLSL_Programming/GLUT/Smooth_Specular_Highlights",
" * This file is in the public domain.",
" * Contributors: Sylvain Beucler",
" */",
"attribute vec3 fg_coord;",
"attribute vec3 fg_normal;",
"varying vec4 position; /* position of the vertex (and fragment) in world space */",
"varying vec3 varyingNormalDirection; /* surface normal vector in world space */",
"uniform mat4 m, p; /* don't need v, as always identity in our demo */",
"uniform mat3 m_3x3_inv_transp;",
" ",
"void main()",
"{",
" vec4 fg_coord4 = vec4(fg_coord, 1.0);",
" position = m * fg_coord4;",
" varyingNormalDirection = normalize(m_3x3_inv_transp * fg_normal);",
" ",
" mat4 mvp = p*m; /* normally p*v*m */",
" gl_Position = mvp * fg_coord4;",
"}"
};
const ourGLchar *fragmentShaderSource[] = {
"/**",
" * From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/GLSL_Prog
评论1