Use GLSL shaders (still 1.20, though). Dump GL info.
git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@777 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
parent
2af3dd1eae
commit
3d7236a470
@ -45,6 +45,7 @@
|
|||||||
#include <GL/freeglut.h>
|
#include <GL/freeglut.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
/* report GL errors, if any, to stderr */
|
/* report GL errors, if any, to stderr */
|
||||||
void checkError(const char *functionName)
|
void checkError(const char *functionName)
|
||||||
@ -55,8 +56,28 @@ void checkError(const char *functionName)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* extension types and entries, avoiding a dependency on additional libraries
|
/* extension #defines, types and entries, avoiding a dependency on additional
|
||||||
like GLEW or the GL/glext.h header */
|
libraries like GLEW or the GL/glext.h header */
|
||||||
|
#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
|
||||||
|
|
||||||
typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
|
typedef void (*PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
|
||||||
PFNGLGENBUFFERSPROC gl_GenBuffers;
|
PFNGLGENBUFFERSPROC gl_GenBuffers;
|
||||||
|
|
||||||
@ -67,11 +88,58 @@ typedef void (*PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size,
|
|||||||
const GLvoid *data, GLenum usage);
|
const GLvoid *data, GLenum usage);
|
||||||
PFNGLBUFFERDATAPROC gl_BufferData;
|
PFNGLBUFFERDATAPROC gl_BufferData;
|
||||||
|
|
||||||
|
typedef GLuint (*PFNGLCREATESHADERPROC) (GLenum type);
|
||||||
|
PFNGLCREATESHADERPROC gl_CreateShader;
|
||||||
|
|
||||||
|
typedef void (*PFNGLSHADERSOURCEPROC)
|
||||||
|
(GLuint shader, GLsizei count, const GLchar **string, const GLint *length);
|
||||||
|
PFNGLSHADERSOURCEPROC gl_ShaderSource;
|
||||||
|
|
||||||
|
typedef void (*PFNGLCOMPILESHADERPROC) (GLuint shader);
|
||||||
|
PFNGLCOMPILESHADERPROC gl_CompileShader;
|
||||||
|
|
||||||
|
typedef GLuint (*PFNGLCREATEPROGRAMPROC) (void);
|
||||||
|
PFNGLCREATEPROGRAMPROC gl_CreateProgram;
|
||||||
|
|
||||||
|
typedef void (*PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader);
|
||||||
|
PFNGLATTACHSHADERPROC gl_AttachShader;
|
||||||
|
|
||||||
|
typedef void (*PFNGLLINKPROGRAMPROC) (GLuint program);
|
||||||
|
PFNGLLINKPROGRAMPROC gl_LinkProgram;
|
||||||
|
|
||||||
|
typedef void (*PFNGLUSEPROGRAMPROC) (GLuint program);
|
||||||
|
PFNGLUSEPROGRAMPROC gl_UseProgram;
|
||||||
|
|
||||||
|
typedef void (*PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
|
||||||
|
PFNGLGETSHADERIVPROC gl_GetShaderiv;
|
||||||
|
|
||||||
|
typedef void (*PFNGLGETSHADERINFOLOGPROC)
|
||||||
|
(GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
|
||||||
|
PFNGLGETSHADERINFOLOGPROC gl_GetShaderInfoLog;
|
||||||
|
|
||||||
|
typedef void (*PFNGLGETPROGRAMIVPROC) (GLenum target, GLenum pname, GLint *params);
|
||||||
|
PFNGLGETPROGRAMIVPROC gl_GetProgramiv;
|
||||||
|
|
||||||
|
typedef void (*PFNGLGETPROGRAMINFOLOGPROC)
|
||||||
|
(GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
|
||||||
|
PFNGLGETPROGRAMINFOLOGPROC gl_GetProgramInfoLog;
|
||||||
|
|
||||||
void initExtensionEntries(void)
|
void initExtensionEntries(void)
|
||||||
{
|
{
|
||||||
gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
|
gl_GenBuffers = (PFNGLGENBUFFERSPROC) glutGetProcAddress ("glGenBuffers");
|
||||||
gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
|
gl_BindBuffer = (PFNGLBINDBUFFERPROC) glutGetProcAddress ("glBindBuffer");
|
||||||
gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
|
gl_BufferData = (PFNGLBUFFERDATAPROC) glutGetProcAddress ("glBufferData");
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* vertex array data for a colored 2D triangle, consisting of RGB color values
|
/* vertex array data for a colored 2D triangle, consisting of RGB color values
|
||||||
@ -108,6 +176,91 @@ void initBuffer(void)
|
|||||||
checkError ("initBuffer");
|
checkError ("initBuffer");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GLchar *vertexShaderSource[] = {
|
||||||
|
"#version 120\n",
|
||||||
|
"void main(void)\n",
|
||||||
|
"{\n",
|
||||||
|
" gl_FrontColor = gl_Color;\n",
|
||||||
|
" gl_Position = ftransform();\n",
|
||||||
|
"}\n"
|
||||||
|
};
|
||||||
|
|
||||||
|
const GLchar *fragmentShaderSource[] = {
|
||||||
|
"#version 120\n",
|
||||||
|
"void main(void)\n",
|
||||||
|
"{\n",
|
||||||
|
" gl_FragColor = gl_Color;\n",
|
||||||
|
"}\n"
|
||||||
|
};
|
||||||
|
|
||||||
|
void compileAndCheck(GLuint shader)
|
||||||
|
{
|
||||||
|
GLint status;
|
||||||
|
gl_CompileShader (shader);
|
||||||
|
gl_GetShaderiv (shader, GL_COMPILE_STATUS, &status);
|
||||||
|
if (status == GL_FALSE) {
|
||||||
|
GLint infoLogLength;
|
||||||
|
GLchar *infoLog;
|
||||||
|
gl_GetShaderiv (shader, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||||
|
infoLog = (GLchar*) malloc (infoLogLength);
|
||||||
|
gl_GetShaderInfoLog (shader, infoLogLength, NULL, infoLog);
|
||||||
|
fprintf (stderr, "compile log: %s\n", infoLog);
|
||||||
|
free (infoLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint compileShaderSource(GLenum type, GLsizei count, const GLchar **string)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
GLchar *infoLog;
|
||||||
|
gl_GetProgramiv (program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||||
|
infoLog = (GLchar*) malloc (infoLogLength);
|
||||||
|
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) {
|
||||||
|
gl_AttachShader (program, vertexShader);
|
||||||
|
}
|
||||||
|
if (fragmentShader != 0) {
|
||||||
|
gl_AttachShader (program, fragmentShader);
|
||||||
|
}
|
||||||
|
linkAndCheck (program);
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
|
||||||
|
void initShader(void)
|
||||||
|
{
|
||||||
|
const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(GLchar*);
|
||||||
|
GLuint vertexShader =
|
||||||
|
compileShaderSource (GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource);
|
||||||
|
|
||||||
|
const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(GLchar*);
|
||||||
|
GLuint fragmentShader =
|
||||||
|
compileShaderSource (GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource);
|
||||||
|
|
||||||
|
GLuint program = createProgram (vertexShader, fragmentShader);
|
||||||
|
gl_UseProgram (program);
|
||||||
|
checkError ("initShader");
|
||||||
|
}
|
||||||
|
|
||||||
void initRendering(void)
|
void initRendering(void)
|
||||||
{
|
{
|
||||||
glClearColor (0.0, 0.0, 0.0, 0.0);
|
glClearColor (0.0, 0.0, 0.0, 0.0);
|
||||||
@ -119,9 +272,19 @@ void init(void)
|
|||||||
{
|
{
|
||||||
initExtensionEntries ();
|
initExtensionEntries ();
|
||||||
initBuffer ();
|
initBuffer ();
|
||||||
|
initShader ();
|
||||||
initRendering ();
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
const GLvoid *bufferObjectPtr (GLsizei index)
|
const GLvoid *bufferObjectPtr (GLsizei index)
|
||||||
{
|
{
|
||||||
return (const GLvoid *) (((char *) NULL) + index);
|
return (const GLvoid *) (((char *) NULL) + index);
|
||||||
@ -210,6 +373,7 @@ int main(int argc, char** argv)
|
|||||||
glutInitWindowSize (500, 500);
|
glutInitWindowSize (500, 500);
|
||||||
glutInitWindowPosition (100, 100);
|
glutInitWindowPosition (100, 100);
|
||||||
glutCreateWindow (argv[0]);
|
glutCreateWindow (argv[0]);
|
||||||
|
dumpInfo ();
|
||||||
init ();
|
init ();
|
||||||
glutDisplayFunc(display);
|
glutDisplayFunc(display);
|
||||||
glutReshapeFunc(reshape);
|
glutReshapeFunc(reshape);
|
||||||
|
Reference in New Issue
Block a user