diff options
| author | Manolo Gouy <Manolo> | 2015-10-27 08:40:56 +0000 |
|---|---|---|
| committer | Manolo Gouy <Manolo> | 2015-10-27 08:40:56 +0000 |
| commit | 7e025aac22338ae573174d67e0c03e085c16630f (patch) | |
| tree | ceed51c15e935fc4a404bd4bb217f01aa55cdc79 /examples/OpenGL3-glut-test.cxx | |
| parent | 43e4f8a6615892a5169f4e713a0d7a2a2e2d86e1 (diff) | |
Added support for OpenGL V3 and higher.
On the X11/MSWindows platforms, this requires external installation of the GLEW library.
This fixes STR#3198 and STR#3257.
Added two new examples programs.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10876 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'examples/OpenGL3-glut-test.cxx')
| -rw-r--r-- | examples/OpenGL3-glut-test.cxx | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/examples/OpenGL3-glut-test.cxx b/examples/OpenGL3-glut-test.cxx new file mode 100644 index 000000000..f455eaf56 --- /dev/null +++ b/examples/OpenGL3-glut-test.cxx @@ -0,0 +1,199 @@ +#include <stdio.h> +#if defined(__APPLE__) +# include <OpenGL/gl3.h> // defines OpenGL 3.0+ functions +#else +# if defined(WIN32) +# define GLEW_STATIC 1 +# endif +# include <GL/glew.h> +#endif +#include <FL/glut.H> + + +// Globals +// Real programs don't use globals :-D +// Data would normally be read from files +GLfloat vertices[] = { -1.0f,0.0f,0.0f, + 0.0f,1.0f,0.0f, + 0.0f,0.0f,0.0f }; +GLfloat colours[] = { 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f }; +GLfloat vertices2[] = { 0.0f,0.0f,0.0f, + 0.0f,-1.0f,0.0f, + 1.0f,0.0f,0.0f }; + +// two vertex array objects, one for each object drawn +unsigned int vertexArrayObjID[2]; +// three vertex buffer objects in this example +unsigned int vertexBufferObjID[3]; + + +void printShaderInfoLog(GLint shader) +{ + int infoLogLen = 0; + GLchar *infoLog; + + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen); + if (infoLogLen > 0) + { + infoLog = new GLchar[infoLogLen]; + // error check for fail to allocate memory omitted + glGetShaderInfoLog(shader,infoLogLen, NULL, infoLog); + fprintf(stderr, "InfoLog:\n%s\n", infoLog); + delete [] infoLog; + } +} + + +void init(void) +{ + // Would load objects from file here - but using globals in this example + + // Allocate Vertex Array Objects + glGenVertexArrays(2, &vertexArrayObjID[0]); + // Setup first Vertex Array Object + glBindVertexArray(vertexArrayObjID[0]); + glGenBuffers(2, vertexBufferObjID); + + // VBO for vertex data + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[0]); + glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertices, GL_STATIC_DRAW); + glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(0); + + // VBO for colour data + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[1]); + glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), colours, GL_STATIC_DRAW); + glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(1); + + // Setup second Vertex Array Object + glBindVertexArray(vertexArrayObjID[1]); + glGenBuffers(1, &vertexBufferObjID[2]); + + // VBO for vertex data + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[2]); + glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertices2, GL_STATIC_DRAW); + glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(0); + + glBindVertexArray(0); +} + + +void initShaders(void) +{ + GLuint p, f, v; + glClearColor (1.0, 1.0, 1.0, 0.0); + + v = glCreateShader(GL_VERTEX_SHADER); + f = glCreateShader(GL_FRAGMENT_SHADER); + +#ifdef __APPLE__ +#define SHADING_LANG_VERS "140" +#else +#define SHADING_LANG_VERS "130" +#endif + // load shaders + const char *vv = "#version "SHADING_LANG_VERS"\n\ + in vec3 in_Position;\ + in vec3 in_Color;\ + out vec3 ex_Color;\ + void main(void)\ + {\ + ex_Color = in_Color;\ + gl_Position = vec4(in_Position, 1.0);\ + }"; + + const char *ff = "#version "SHADING_LANG_VERS"\n\ + precision highp float;\ + in vec3 ex_Color;\ + out vec4 out_Color;\ + void main(void)\ + {\ + out_Color = vec4(ex_Color,1.0);\ + }"; + + glShaderSource(v, 1, &vv,NULL); + glShaderSource(f, 1, &ff,NULL); + + GLint compiled; + + glCompileShader(v); + glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); + if (!compiled) + { + fprintf(stderr, "Vertex shader not compiled.\n"); + printShaderInfoLog(v); + } + + glCompileShader(f); + glGetShaderiv(f, GL_COMPILE_STATUS, &compiled); + if (!compiled) + { + fprintf(stderr, "Fragment shader not compiled.\n"); + printShaderInfoLog(f); + } + + p = glCreateProgram(); + + glAttachShader(p,v); + glAttachShader(p,f); + glBindAttribLocation(p,0, "in_Position"); + glBindAttribLocation(p,1, "in_Color"); + + glLinkProgram(p); + glGetProgramiv(p, GL_LINK_STATUS, &compiled); + if (compiled != GL_TRUE) { + GLchar *infoLog; GLint length; + glGetProgramiv(p, GL_INFO_LOG_LENGTH, &length); + infoLog = new GLchar[length]; + glGetProgramInfoLog(p, length, NULL, infoLog); + fprintf(stderr, "Link log=%s\n", infoLog); + delete[] infoLog; + } + glUseProgram(p); +} + + +void display(void) +{ + // clear the screen + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBindVertexArray(vertexArrayObjID[0]); // First VAO + glDrawArrays(GL_TRIANGLES, 0, 3); // draw first object + + glBindVertexArray(vertexArrayObjID[1]); // select second VAO + glVertexAttrib3f((GLuint)1, 1.0, 0.0, 0.0); // set constant color attribute + glDrawArrays(GL_TRIANGLES, 0, 3); // draw second object + } + + +int main (int argc, char* argv[]) +{ + Fl::use_high_res_GL(true); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | FL_OPENGL3); + glutInitWindowSize(400,400); + glutCreateWindow("Triangle Test"); +#ifndef __APPLE__ + GLenum err = glewInit(); // defines pters to functions of OpenGL V 1.2 and above + if (err) Fl::error("glewInit() failed returning %u", err); + fprintf(stderr, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); +#endif + int gl_version_major; + const char *glv = (const char*)glGetString(GL_VERSION); + fprintf(stderr, "OpenGL version %s supported\n", glv); + sscanf(glv, "%d", &gl_version_major); + if (gl_version_major < 3) { + fprintf(stderr, "\nThis platform does not support OpenGL V3\n\n"); + exit(1); + } + initShaders(); + init(); + glutDisplayFunc(display); + glutMainLoop(); + return 0; +} |
