2009-02-14 22:38:53 +02:00
|
|
|
/*
|
|
|
|
* smooth_opengl3.c, based on smooth.c, which is (c) by SGI, see below.
|
|
|
|
* This program demonstrates smooth shading in a way which is fully
|
2009-03-30 19:14:49 +03:00
|
|
|
* OpenGL-3.1-compliant.
|
2009-02-14 22:38:53 +02:00
|
|
|
* A smooth shaded polygon is drawn in a 2-D projection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Original copyright notice from smooth.c:
|
|
|
|
*
|
|
|
|
* License Applicability. Except to the extent portions of this file are
|
|
|
|
* made subject to an alternative license as permitted in the SGI Free
|
|
|
|
* Software License B, Version 1.1 (the "License"), the contents of this
|
|
|
|
* file are subject only to the provisions of the License. You may not use
|
|
|
|
* this file except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
|
|
|
|
* Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
|
|
|
|
*
|
|
|
|
* http://oss.sgi.com/projects/FreeB
|
|
|
|
*
|
|
|
|
* Note that, as provided in the License, the Software is distributed on an
|
|
|
|
* "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
|
|
|
|
* DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
|
|
|
|
* CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
|
|
|
|
*
|
|
|
|
* Original Code. The Original Code is: OpenGL Sample Implementation,
|
|
|
|
* Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
|
|
|
|
* Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
|
|
|
|
* Copyright in any portions created by third parties is as indicated
|
|
|
|
* elsewhere herein. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Additional Notice Provisions: The application programming interfaces
|
|
|
|
* established by SGI in conjunction with the Original Code are The
|
|
|
|
* OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
|
|
|
|
* April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
|
|
|
|
* 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
|
|
|
|
* Window System(R) (Version 1.3), released October 19, 1998. This software
|
|
|
|
* was created using the OpenGL(R) version 1.2.1 Sample Implementation
|
|
|
|
* published by SGI, but has not been independently verified as being
|
|
|
|
* compliant with the OpenGL(R) version 1.2.1 Specification.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <GL/freeglut.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2009-03-27 18:20:40 +02:00
|
|
|
#include <stddef.h>
|
2009-02-15 19:53:48 +02:00
|
|
|
#include <string.h>
|
2009-02-14 22:38:53 +02:00
|
|
|
|
2009-02-15 02:22:55 +02:00
|
|
|
/* report GL errors, if any, to stderr */
|
2009-02-15 16:00:45 +02:00
|
|
|
void checkError(const char *functionName)
|
2009-02-14 22:38:53 +02:00
|
|
|
{
|
|
|
|
GLenum error;
|
2012-08-07 14:15:01 +03:00
|
|
|
while (( error = glGetError() ) != GL_NO_ERROR) {
|
2011-12-14 05:35:07 +02:00
|
|
|
fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
|
2009-02-14 22:38:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-15 19:53:48 +02:00
|
|
|
/* extension #defines, types and entries, avoiding a dependency on additional
|
|
|
|
libraries like GLEW or the GL/glext.h header */
|
2009-02-15 23:12:25 +02:00
|
|
|
#ifndef GL_ARRAY_BUFFER
|
|
|
|
#define GL_ARRAY_BUFFER 0x8892
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_STATIC_DRAW
|
|
|
|
#define GL_STATIC_DRAW 0x88E4
|
|
|
|
#endif
|
|
|
|
|
2009-02-28 20:31:35 +02:00
|
|
|
#ifndef GL_FRAGMENT_SHADER
|
|
|
|
#define GL_FRAGMENT_SHADER 0x8B30
|
|
|
|
#endif
|
|
|
|
|
2009-02-15 19:53:48 +02:00
|
|
|
#ifndef GL_VERTEX_SHADER
|
|
|
|
#define GL_VERTEX_SHADER 0x8B31
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_SHADING_LANGUAGE_VERSION
|
|
|
|
#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
|
|
|
|
#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
|
|
|
|
|
2009-02-28 20:31:35 +02:00
|
|
|
typedef ptrdiff_t ourGLsizeiptr;
|
|
|
|
typedef char ourGLchar;
|
2011-01-23 16:18:25 +02:00
|
|
|
|
2009-03-01 13:33:15 +02:00
|
|
|
#ifndef APIENTRY
|
|
|
|
#define APIENTRY
|
|
|
|
#endif
|
|
|
|
|
2012-08-07 14:15:01 +03:00
|
|
|
|
|
|
|
#ifndef GL_ARB_vertex_array_object
|
|
|
|
typedef void (APIENTRY *PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays);
|
2012-07-24 18:37:28 +03:00
|
|
|
typedef void (APIENTRY *PFNGLBINDVERTEXARRAYPROC) (GLuint array);
|
2012-08-07 14:15:01 +03:00
|
|
|
#endif
|
|
|
|
#ifndef GL_VERSION_1_5
|
2009-03-01 13:33:15 +02:00
|
|
|
typedef void (APIENTRY *PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
|
|
|
|
typedef void (APIENTRY *PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer);
|
|
|
|
typedef void (APIENTRY *PFNGLBUFFERDATAPROC) (GLenum target, ourGLsizeiptr size, const GLvoid *data, GLenum usage);
|
2012-08-07 14:15:01 +03:00
|
|
|
#endif
|
|
|
|
#ifndef GL_VERSION_2_0
|
2009-03-01 13:33:15 +02:00
|
|
|
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 void (APIENTRY *PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
|
|
|
|
typedef void (APIENTRY *PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
|
|
|
|
typedef GLint (APIENTRY *PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const ourGLchar *name);
|
|
|
|
typedef void (APIENTRY *PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
|
2012-08-07 14:15:01 +03:00
|
|
|
#endif
|
2011-01-23 16:18:25 +02:00
|
|
|
|
2012-08-07 14:15:01 +03:00
|
|
|
PFNGLGENVERTEXARRAYSPROC gl_GenVertexArrays;
|
2012-07-24 18:37:28 +03:00
|
|
|
PFNGLBINDVERTEXARRAYPROC gl_BindVertexArray;
|
2011-01-23 16:18:25 +02:00
|
|
|
PFNGLGENBUFFERSPROC gl_GenBuffers;
|
|
|
|
PFNGLBINDBUFFERPROC gl_BindBuffer;
|
|
|
|
PFNGLBUFFERDATAPROC gl_BufferData;
|
|
|
|
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;
|
|
|
|
PFNGLVERTEXATTRIBPOINTERPROC gl_VertexAttribPointer;
|
|
|
|
PFNGLENABLEVERTEXATTRIBARRAYPROC gl_EnableVertexAttribArray;
|
|
|
|
PFNGLGETUNIFORMLOCATIONPROC gl_GetUniformLocation;
|
|
|
|
PFNGLUNIFORMMATRIX4FVPROC gl_UniformMatrix4fv;
|
2009-02-15 23:12:25 +02:00
|
|
|
|
2012-08-07 14:15:01 +03:00
|
|
|
void initExtensionEntries(void)
|
2009-02-14 22:38:53 +02:00
|
|
|
{
|
2012-08-07 14:15:01 +03:00
|
|
|
gl_GenVertexArrays = (PFNGLGENVERTEXARRAYSPROC) glutGetProcAddress ("glGenVertexArrays");
|
2012-07-24 18:37:28 +03:00
|
|
|
gl_BindVertexArray = (PFNGLBINDVERTEXARRAYPROC) glutGetProcAddress ("glBindVertexArray");
|
2013-01-27 13:56:32 +02:00
|
|
|
if (!gl_GenVertexArrays || !gl_BindVertexArray)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "glGenVertexArrays or glBindVertexArray not found");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-02-15 16:00:45 +02:00
|
|
|
gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
|
|
|
|
gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
|
|
|
|
gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
|
2013-01-27 13:56:32 +02:00
|
|
|
if (!gl_GenBuffers || !gl_BindBuffer || !gl_BufferData)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "glGenBuffers, glBindBuffer or glBufferData not found");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-02-15 19:53:48 +02:00
|
|
|
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");
|
2009-02-15 23:12:25 +02:00
|
|
|
gl_GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC) glutGetProcAddress ("glGetAttribLocation");
|
|
|
|
gl_VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC) glutGetProcAddress ("glVertexAttribPointer");
|
|
|
|
gl_EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC) glutGetProcAddress ("glEnableVertexAttribArray");
|
|
|
|
gl_GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC) glutGetProcAddress ("glGetUniformLocation");
|
|
|
|
gl_UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC) glutGetProcAddress ("glUniformMatrix4fv");
|
2013-01-27 13:56:32 +02:00
|
|
|
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_VertexAttribPointer || !gl_EnableVertexAttribArray || !gl_GetUniformLocation || !gl_UniformMatrix4fv)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "glCreateShader, glShaderSource, glCompileShader, glCreateProgram, glAttachShader, glLinkProgram, glUseProgram, glGetShaderiv, glGetShaderInfoLog, glGetProgramiv, glGetProgramInfoLog, glGetAttribLocation, glVertexAttribPointer, glEnableVertexAttribArray, glGetUniformLocation or glUniformMatrix4fv not found");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-02-15 02:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vertex array data for a colored 2D triangle, consisting of RGB color values
|
|
|
|
and XY coordinates */
|
|
|
|
const GLfloat varray[] = {
|
2009-02-15 16:00:45 +02:00
|
|
|
1.0f, 0.0f, 0.0f, /* red */
|
|
|
|
5.0f, 5.0f, /* lower left */
|
2009-02-15 02:22:55 +02:00
|
|
|
|
2009-02-15 16:00:45 +02:00
|
|
|
0.0f, 1.0f, 0.0f, /* green */
|
|
|
|
25.0f, 5.0f, /* lower right */
|
2009-02-15 02:22:55 +02:00
|
|
|
|
2009-02-15 16:00:45 +02:00
|
|
|
0.0f, 0.0f, 1.0f, /* blue */
|
|
|
|
5.0f, 25.0f /* upper left */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ISO C somehow enforces this silly use of 'enum' for compile-time constants */
|
|
|
|
enum {
|
|
|
|
numColorComponents = 3,
|
|
|
|
numVertexComponents = 2,
|
|
|
|
stride = sizeof(GLfloat) * (numColorComponents + numVertexComponents),
|
|
|
|
numElements = sizeof(varray) / stride
|
2009-02-15 02:22:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* the name of the vertex buffer object */
|
2009-02-15 16:00:45 +02:00
|
|
|
GLuint vertexBufferName;
|
2012-07-24 18:37:28 +03:00
|
|
|
GLuint vertexArrayName;
|
2009-02-15 02:22:55 +02:00
|
|
|
|
|
|
|
void initBuffer(void)
|
|
|
|
{
|
2012-08-07 14:15:01 +03:00
|
|
|
/* Need to setup a vertex array as otherwise invalid operation errors can
|
|
|
|
* occur when accessing vertex buffer (OpenGL 3.3 has no default zero named
|
|
|
|
* vertex array)
|
|
|
|
*/
|
|
|
|
gl_GenVertexArrays(1, &vertexArrayName);
|
2012-07-24 18:37:28 +03:00
|
|
|
gl_BindVertexArray(vertexArrayName);
|
|
|
|
|
2009-02-15 16:00:45 +02:00
|
|
|
gl_GenBuffers (1, &vertexBufferName);
|
|
|
|
gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
|
2009-02-15 02:22:55 +02:00
|
|
|
gl_BufferData (GL_ARRAY_BUFFER, sizeof(varray), varray, GL_STATIC_DRAW);
|
2009-02-15 16:00:45 +02:00
|
|
|
checkError ("initBuffer");
|
2009-02-15 02:22:55 +02:00
|
|
|
}
|
|
|
|
|
2009-02-28 20:31:35 +02:00
|
|
|
const ourGLchar *vertexShaderSource[] = {
|
2009-03-30 19:14:49 +03:00
|
|
|
"#version 140\n",
|
2009-02-15 23:12:25 +02:00
|
|
|
"uniform mat4 fg_ProjectionMatrix;\n",
|
|
|
|
"in vec4 fg_Color;\n",
|
|
|
|
"in vec4 fg_Vertex;\n",
|
2009-03-30 19:14:49 +03:00
|
|
|
"smooth out vec4 fg_SmoothColor;\n",
|
|
|
|
"void main()\n",
|
2009-02-15 19:53:48 +02:00
|
|
|
"{\n",
|
2009-03-30 19:14:49 +03:00
|
|
|
" fg_SmoothColor = fg_Color;\n",
|
2009-02-15 23:12:25 +02:00
|
|
|
" gl_Position = fg_ProjectionMatrix * fg_Vertex;\n",
|
2009-02-15 19:53:48 +02:00
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
2009-02-28 20:31:35 +02:00
|
|
|
const ourGLchar *fragmentShaderSource[] = {
|
2009-03-30 19:14:49 +03:00
|
|
|
"#version 140\n",
|
|
|
|
"smooth in vec4 fg_SmoothColor;\n",
|
2009-02-15 23:12:25 +02:00
|
|
|
"out vec4 fg_FragColor;\n",
|
2009-02-15 19:53:48 +02:00
|
|
|
"void main(void)\n",
|
|
|
|
"{\n",
|
2009-03-30 19:14:49 +03:00
|
|
|
" fg_FragColor = fg_SmoothColor;\n",
|
2009-02-15 19:53:48 +02:00
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
void compileAndCheck(GLuint shader)
|
|
|
|
{
|
|
|
|
GLint status;
|
|
|
|
gl_CompileShader (shader);
|
|
|
|
gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLint infoLogLength;
|
2009-02-28 20:31:35 +02:00
|
|
|
ourGLchar *infoLog;
|
2009-02-15 19:53:48 +02:00
|
|
|
gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
2009-02-28 20:31:35 +02:00
|
|
|
infoLog = (ourGLchar*) malloc (infoLogLength);
|
2009-02-15 19:53:48 +02:00
|
|
|
gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
|
|
|
|
fprintf (stderr, "compile log: %s\n", infoLog);
|
|
|
|
free (infoLog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 20:31:35 +02:00
|
|
|
GLuint compileShaderSource(GLenum type, GLsizei count, const ourGLchar **string)
|
2009-02-15 19:53:48 +02:00
|
|
|
{
|
|
|
|
GLuint shader = gl_CreateShader (type);
|
|
|
|
gl_ShaderSource (shader, count, string, NULL);
|
|
|
|
compileAndCheck (shader);
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linkAndCheck(GLuint program)
|
|
|
|
{
|
|
|
|
GLint status;
|
|
|
|
gl_LinkProgram (program);
|
|
|
|
gl_GetProgramiv (program, GL_LINK_STATUS, &status);
|
|
|
|
if (status == GL_FALSE) {
|
|
|
|
GLint infoLogLength;
|
2009-02-28 20:31:35 +02:00
|
|
|
ourGLchar *infoLog;
|
2009-02-15 19:53:48 +02:00
|
|
|
gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
2009-02-28 20:31:35 +02:00
|
|
|
infoLog = (ourGLchar*) malloc (infoLogLength);
|
2009-02-15 19:53:48 +02:00
|
|
|
gl_GetProgramInfoLog (program, infoLogLength, NULL, infoLog);
|
|
|
|
fprintf (stderr, "link log: %s\n", infoLog);
|
|
|
|
free (infoLog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint createProgram(GLuint vertexShader, GLuint fragmentShader)
|
|
|
|
{
|
|
|
|
GLuint program = gl_CreateProgram ();
|
|
|
|
if (vertexShader != 0) {
|
2009-02-15 23:12:25 +02:00
|
|
|
gl_AttachShader (program, vertexShader);
|
2009-02-15 19:53:48 +02:00
|
|
|
}
|
|
|
|
if (fragmentShader != 0) {
|
2009-02-15 23:12:25 +02:00
|
|
|
gl_AttachShader (program, fragmentShader);
|
2009-02-15 19:53:48 +02:00
|
|
|
}
|
|
|
|
linkAndCheck (program);
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:25 +02:00
|
|
|
GLuint fgProjectionMatrixIndex;
|
|
|
|
GLuint fgColorIndex;
|
|
|
|
GLuint fgVertexIndex;
|
|
|
|
|
2009-02-15 19:53:48 +02:00
|
|
|
void initShader(void)
|
|
|
|
{
|
2009-02-28 20:31:35 +02:00
|
|
|
const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(ourGLchar*);
|
2009-02-15 19:53:48 +02:00
|
|
|
GLuint vertexShader =
|
|
|
|
compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
|
|
|
|
|
2009-02-28 20:31:35 +02:00
|
|
|
const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(ourGLchar*);
|
2009-02-15 19:53:48 +02:00
|
|
|
GLuint fragmentShader =
|
|
|
|
compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
|
|
|
|
|
|
|
|
GLuint program = createProgram (vertexShader, fragmentShader);
|
2009-02-15 23:12:25 +02:00
|
|
|
|
2009-02-15 19:53:48 +02:00
|
|
|
gl_UseProgram (program);
|
2009-02-15 23:12:25 +02:00
|
|
|
|
|
|
|
fgProjectionMatrixIndex = gl_GetUniformLocation(program, "fg_ProjectionMatrix");
|
|
|
|
|
|
|
|
fgColorIndex = gl_GetAttribLocation(program, "fg_Color");
|
|
|
|
gl_EnableVertexAttribArray (fgColorIndex);
|
|
|
|
|
|
|
|
fgVertexIndex = gl_GetAttribLocation(program, "fg_Vertex");
|
|
|
|
gl_EnableVertexAttribArray (fgVertexIndex);
|
|
|
|
|
2009-02-15 19:53:48 +02:00
|
|
|
checkError ("initShader");
|
|
|
|
}
|
|
|
|
|
2009-02-15 02:22:55 +02:00
|
|
|
void initRendering(void)
|
|
|
|
{
|
2009-02-14 22:38:53 +02:00
|
|
|
glClearColor (0.0, 0.0, 0.0, 0.0);
|
2009-02-15 16:00:45 +02:00
|
|
|
checkError ("initRendering");
|
2009-02-15 02:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init(void)
|
|
|
|
{
|
2009-02-15 19:53:48 +02:00
|
|
|
initExtensionEntries ();
|
|
|
|
initBuffer ();
|
|
|
|
initShader ();
|
|
|
|
initRendering ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpInfo(void)
|
|
|
|
{
|
|
|
|
printf ("Vendor: %s\n", glGetString (GL_VENDOR));
|
|
|
|
printf ("Renderer: %s\n", glGetString (GL_RENDERER));
|
|
|
|
printf ("Version: %s\n", glGetString (GL_VERSION));
|
|
|
|
printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
|
|
|
|
checkError ("dumpInfo");
|
2009-02-14 22:38:53 +02:00
|
|
|
}
|
|
|
|
|
2009-02-15 16:00:45 +02:00
|
|
|
const GLvoid *bufferObjectPtr (GLsizei index)
|
|
|
|
{
|
|
|
|
return (const GLvoid *) (((char *) NULL) + index);
|
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:25 +02:00
|
|
|
GLfloat projectionMatrix[16];
|
|
|
|
|
2009-02-14 22:38:53 +02:00
|
|
|
void triangle(void)
|
|
|
|
{
|
2009-02-15 23:12:25 +02:00
|
|
|
gl_UniformMatrix4fv (fgProjectionMatrixIndex, 1, GL_FALSE, projectionMatrix);
|
|
|
|
|
2009-02-15 16:00:45 +02:00
|
|
|
gl_BindBuffer (GL_ARRAY_BUFFER, vertexBufferName);
|
2009-02-15 23:12:25 +02:00
|
|
|
gl_VertexAttribPointer (fgColorIndex, numColorComponents, GL_FLOAT, GL_FALSE,
|
|
|
|
stride, bufferObjectPtr (0));
|
|
|
|
gl_VertexAttribPointer (fgVertexIndex, numVertexComponents, GL_FLOAT, GL_FALSE,
|
|
|
|
stride, bufferObjectPtr (sizeof(GLfloat) * numColorComponents));
|
2009-02-15 02:22:55 +02:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, numElements);
|
2009-02-15 16:00:45 +02:00
|
|
|
checkError ("triangle");
|
2009-02-14 22:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void display(void)
|
|
|
|
{
|
|
|
|
glClear (GL_COLOR_BUFFER_BIT);
|
|
|
|
triangle ();
|
|
|
|
glFlush ();
|
2009-02-15 16:00:45 +02:00
|
|
|
checkError ("display");
|
2009-02-14 22:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loadOrthof(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t,
|
|
|
|
GLfloat n, GLfloat f)
|
|
|
|
{
|
|
|
|
m[ 0] = 2.0f / (r - l);
|
|
|
|
m[ 1] = 0.0f;
|
|
|
|
m[ 2] = 0.0f;
|
|
|
|
m[ 3] = 0.0f;
|
|
|
|
|
|
|
|
m[ 4] = 0.0f;
|
|
|
|
m[ 5] = 2.0f / (t - b);
|
|
|
|
m[ 6] = 0.0f;
|
|
|
|
m[ 7] = 0.0f;
|
|
|
|
|
|
|
|
m[ 8] = 0.0f;
|
|
|
|
m[ 9] = 0.0f;
|
|
|
|
m[10] = -2.0f / (f - n);
|
|
|
|
m[11] = 0.0f;
|
|
|
|
|
|
|
|
m[12] = -(r + l) / (r - l);
|
|
|
|
m[13] = -(t + b) / (t - b);
|
|
|
|
m[14] = -(f + n) / (f - n);
|
|
|
|
m[15] = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadOrtho2Df(GLfloat *m, GLfloat l, GLfloat r, GLfloat b, GLfloat t)
|
|
|
|
{
|
|
|
|
loadOrthof (m, l, r, b, t, -1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reshape (int w, int h)
|
|
|
|
{
|
|
|
|
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
|
|
|
|
if (w <= h) {
|
2009-03-27 18:20:40 +02:00
|
|
|
loadOrtho2Df (projectionMatrix, 0.0f, 30.0f, 0.0f, 30.0f * (GLfloat) h/(GLfloat) w);
|
2009-02-14 22:38:53 +02:00
|
|
|
} else {
|
2009-03-27 18:20:40 +02:00
|
|
|
loadOrtho2Df (projectionMatrix, 0.0f, 30.0f * (GLfloat) w/(GLfloat) h, 0.0f, 30.0f);
|
2009-02-14 22:38:53 +02:00
|
|
|
}
|
2009-02-15 16:00:45 +02:00
|
|
|
checkError ("reshape");
|
2009-02-14 22:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void keyboard(unsigned char key, int x, int y)
|
|
|
|
{
|
|
|
|
switch (key) {
|
|
|
|
case 27:
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 06:50:51 +03:00
|
|
|
void samplemenu(int menuID)
|
|
|
|
{}
|
|
|
|
|
2009-02-14 22:38:53 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2014-06-01 06:50:51 +03:00
|
|
|
int menuA;
|
2009-02-14 22:38:53 +02:00
|
|
|
glutInit(&argc, argv);
|
|
|
|
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
|
2009-03-30 19:14:49 +03:00
|
|
|
/* add command line argument "classic" for a pre-3.x context */
|
2009-02-15 16:00:45 +02:00
|
|
|
if ((argc != 2) || (strcmp (argv[1], "classic") != 0)) {
|
2009-03-30 19:14:49 +03:00
|
|
|
glutInitContextVersion (3, 1);
|
2009-02-15 16:00:45 +02:00
|
|
|
glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
|
|
|
|
}
|
2009-02-14 22:38:53 +02:00
|
|
|
glutInitWindowSize (500, 500);
|
|
|
|
glutInitWindowPosition (100, 100);
|
|
|
|
glutCreateWindow (argv[0]);
|
2009-02-15 19:53:48 +02:00
|
|
|
dumpInfo ();
|
2009-02-14 22:38:53 +02:00
|
|
|
init ();
|
|
|
|
glutDisplayFunc(display);
|
|
|
|
glutReshapeFunc(reshape);
|
|
|
|
glutKeyboardFunc (keyboard);
|
2014-06-01 06:50:51 +03:00
|
|
|
|
|
|
|
/* Add a menu. They have their own context and should thus work with forward compatible main windows too. */
|
|
|
|
menuA = glutCreateMenu(samplemenu);
|
|
|
|
glutAddMenuEntry("Sub menu A1 (01)",1);
|
|
|
|
glutAddMenuEntry("Sub menu A2 (02)",2);
|
|
|
|
glutAddMenuEntry("Sub menu A3 (03)",3);
|
|
|
|
glutSetMenu(menuA);
|
|
|
|
glutAttachMenu(GLUT_RIGHT_BUTTON);
|
|
|
|
|
2009-02-14 22:38:53 +02:00
|
|
|
glutMainLoop();
|
|
|
|
return 0;
|
|
|
|
}
|