2015-12-10 19:20:37 +03:00
|
|
|
//
|
|
|
|
// Tiny OpenGL v3 demo program for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2022-03-04 17:40:29 +03:00
|
|
|
// Copyright 1998-2022 by Bill Spitzak and others.
|
2015-12-10 19:20:37 +03:00
|
|
|
//
|
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2015-12-10 19:20:37 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2015-12-10 19:20:37 +03:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2015-12-10 19:20:37 +03:00
|
|
|
//
|
|
|
|
|
2015-10-27 11:40:56 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <FL/Fl.H>
|
2018-02-01 00:17:17 +03:00
|
|
|
#include <FL/platform.H>
|
2015-10-27 11:40:56 +03:00
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
#include <FL/Fl_Gl_Window.H>
|
|
|
|
#include <FL/Fl_Light_Button.H>
|
|
|
|
#include <FL/Fl_Text_Display.H>
|
|
|
|
#include <FL/Fl_Text_Buffer.H>
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
# include <OpenGL/gl3.h> // defines OpenGL 3.0+ functions
|
|
|
|
#else
|
2018-02-09 17:39:42 +03:00
|
|
|
# if defined(_WIN32)
|
2015-10-27 11:40:56 +03:00
|
|
|
# define GLEW_STATIC 1
|
|
|
|
# endif
|
|
|
|
# include <GL/glew.h>
|
|
|
|
#endif
|
2022-09-29 10:39:21 +03:00
|
|
|
#include <FL/gl.h> // for gl_texture_reset()
|
2015-10-27 11:40:56 +03:00
|
|
|
|
|
|
|
void add_output(const char *format, ...);
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleGL3Window : public Fl_Gl_Window {
|
|
|
|
GLuint shaderProgram;
|
|
|
|
GLuint vertexArrayObject;
|
|
|
|
GLuint vertexBuffer;
|
|
|
|
GLint positionUniform;
|
|
|
|
GLint colourAttribute;
|
|
|
|
GLint positionAttribute;
|
|
|
|
int gl_version_major;
|
|
|
|
public:
|
|
|
|
SimpleGL3Window(int x, int y, int w, int h) : Fl_Gl_Window(x, y, w, h) {
|
|
|
|
mode(FL_RGB8 | FL_DOUBLE | FL_OPENGL3);
|
|
|
|
shaderProgram = 0;
|
2019-04-04 19:42:45 +03:00
|
|
|
gl_version_major = 0;
|
2015-10-27 11:40:56 +03:00
|
|
|
}
|
2022-12-30 21:14:36 +03:00
|
|
|
void draw(void) FL_OVERRIDE {
|
2022-09-29 10:39:21 +03:00
|
|
|
if (gl_version_major >= 3 && !shaderProgram) {
|
2015-10-27 11:40:56 +03:00
|
|
|
GLuint vs;
|
|
|
|
GLuint fs;
|
|
|
|
int Mslv, mslv; // major and minor version numbers of the shading language
|
|
|
|
sscanf((char*)glGetString(GL_SHADING_LANGUAGE_VERSION), "%d.%d", &Mslv, &mslv);
|
|
|
|
add_output("Shading Language Version=%d.%d\n",Mslv, mslv);
|
|
|
|
const char *vss_format="#version %d%d\n\
|
|
|
|
uniform vec2 p;\
|
|
|
|
in vec4 position;\
|
|
|
|
in vec4 colour;\
|
|
|
|
out vec4 colourV;\
|
|
|
|
void main (void)\
|
|
|
|
{\
|
|
|
|
colourV = colour;\
|
|
|
|
gl_Position = vec4(p, 0.0, 0.0) + position;\
|
|
|
|
}";
|
|
|
|
char vss_string[300]; const char *vss = vss_string;
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(vss_string, 300, vss_format, Mslv, mslv);
|
2015-10-27 11:40:56 +03:00
|
|
|
const char *fss_format="#version %d%d\n\
|
|
|
|
in vec4 colourV;\
|
|
|
|
out vec4 fragColour;\
|
|
|
|
void main(void)\
|
|
|
|
{\
|
|
|
|
fragColour = colourV;\
|
|
|
|
}";
|
|
|
|
char fss_string[200]; const char *fss = fss_string;
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(fss_string, 200, fss_format, Mslv, mslv);
|
2015-10-27 11:40:56 +03:00
|
|
|
GLint err; GLchar CLOG[1000]; GLsizei length;
|
|
|
|
vs = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
glShaderSource(vs, 1, &vss, NULL);
|
|
|
|
glCompileShader(vs);
|
|
|
|
glGetShaderiv(vs, GL_COMPILE_STATUS, &err);
|
|
|
|
if (err != GL_TRUE) {
|
2020-07-01 19:03:10 +03:00
|
|
|
glGetShaderInfoLog(vs, sizeof(CLOG), &length, CLOG);
|
|
|
|
add_output("vs ShaderInfoLog=%s\n",CLOG);
|
|
|
|
}
|
2015-10-27 11:40:56 +03:00
|
|
|
fs = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
glShaderSource(fs, 1, &fss, NULL);
|
|
|
|
glCompileShader(fs);
|
|
|
|
glGetShaderiv(fs, GL_COMPILE_STATUS, &err);
|
|
|
|
if (err != GL_TRUE) {
|
2020-07-01 19:03:10 +03:00
|
|
|
glGetShaderInfoLog(fs, sizeof(CLOG), &length, CLOG);
|
|
|
|
add_output("fs ShaderInfoLog=%s\n",CLOG);
|
|
|
|
}
|
2015-10-27 11:40:56 +03:00
|
|
|
// Attach the shaders
|
|
|
|
shaderProgram = glCreateProgram();
|
|
|
|
glAttachShader(shaderProgram, vs);
|
|
|
|
glAttachShader(shaderProgram, fs);
|
|
|
|
glBindFragDataLocation(shaderProgram, 0, "fragColour");
|
|
|
|
glLinkProgram(shaderProgram);
|
|
|
|
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &err);
|
|
|
|
if (err != GL_TRUE) {
|
|
|
|
glGetProgramInfoLog(shaderProgram, sizeof(CLOG), &length, CLOG);
|
|
|
|
add_output("link log=%s\n", CLOG);
|
|
|
|
}
|
|
|
|
// Get pointers to uniforms and attributes
|
|
|
|
positionUniform = glGetUniformLocation(shaderProgram, "p");
|
|
|
|
colourAttribute = glGetAttribLocation(shaderProgram, "colour");
|
|
|
|
positionAttribute = glGetAttribLocation(shaderProgram, "position");
|
|
|
|
glDeleteShader(vs);
|
|
|
|
glDeleteShader(fs);
|
|
|
|
// Upload vertices (1st four values in a row) and colours (following four values)
|
|
|
|
GLfloat vertexData[]= { -0.5,-0.5,0.0,1.0, 1.0,0.0,0.0,1.0,
|
|
|
|
-0.5, 0.5,0.0,1.0, 0.0,1.0,0.0,1.0,
|
|
|
|
0.5, 0.5,0.0,1.0, 0.0,0.0,1.0,1.0,
|
|
|
|
0.5,-0.5,0.0,1.0, 1.0,1.0,1.0,1.0};
|
|
|
|
glGenVertexArrays(1, &vertexArrayObject);
|
|
|
|
glBindVertexArray(vertexArrayObject);
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2015-10-27 11:40:56 +03:00
|
|
|
glGenBuffers(1, &vertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, 4*8*sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2015-10-27 11:40:56 +03:00
|
|
|
glEnableVertexAttribArray((GLuint)positionAttribute);
|
|
|
|
glEnableVertexAttribArray((GLuint)colourAttribute );
|
|
|
|
glVertexAttribPointer((GLuint)positionAttribute, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0);
|
|
|
|
glVertexAttribPointer((GLuint)colourAttribute , 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (char*)0+4*sizeof(GLfloat));
|
2022-09-27 15:12:39 +03:00
|
|
|
glUseProgram(shaderProgram);
|
2015-10-27 11:40:56 +03:00
|
|
|
}
|
|
|
|
else if ((!valid())) {
|
|
|
|
glViewport(0, 0, pixel_w(), pixel_h());
|
|
|
|
}
|
|
|
|
glClearColor(0.08, 0.8, 0.8, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2022-09-29 10:39:21 +03:00
|
|
|
if (shaderProgram) {
|
|
|
|
GLfloat p[]={0,0};
|
|
|
|
glUniform2fv(positionUniform, 1, (const GLfloat *)&p);
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
|
|
}
|
2022-09-25 17:39:40 +03:00
|
|
|
Fl_Gl_Window::draw(); // Draw FLTK child widgets.
|
2015-10-27 11:40:56 +03:00
|
|
|
}
|
2022-12-30 21:14:36 +03:00
|
|
|
int handle(int event) FL_OVERRIDE {
|
2015-10-27 11:40:56 +03:00
|
|
|
static int first = 1;
|
|
|
|
if (first && event == FL_SHOW && shown()) {
|
|
|
|
first = 0;
|
|
|
|
make_current();
|
|
|
|
#ifndef __APPLE__
|
|
|
|
GLenum err = glewInit(); // defines pters to functions of OpenGL V 1.2 and above
|
2022-03-04 17:40:29 +03:00
|
|
|
# ifdef FLTK_USE_WAYLAND
|
|
|
|
// glewInit returns GLEW_ERROR_NO_GLX_DISPLAY with Wayland
|
|
|
|
// see https://github.com/nigels-com/glew/issues/273
|
2022-09-22 09:07:46 +03:00
|
|
|
if (fl_wl_display() && err == GLEW_ERROR_NO_GLX_DISPLAY) err = GLEW_OK;
|
2022-03-04 17:40:29 +03:00
|
|
|
# endif
|
2015-10-27 11:40:56 +03:00
|
|
|
if (err) Fl::warning("glewInit() failed returning %u", err);
|
|
|
|
else add_output("Using GLEW %s\n", glewGetString(GLEW_VERSION));
|
|
|
|
#endif
|
|
|
|
const uchar *glv = glGetString(GL_VERSION);
|
|
|
|
add_output("GL_VERSION=%s\n", glv);
|
|
|
|
sscanf((const char *)glv, "%d", &gl_version_major);
|
2022-09-29 10:39:21 +03:00
|
|
|
if (gl_version_major < 3) {
|
|
|
|
add_output("\nThis platform does not support OpenGL V3 :\n"
|
|
|
|
"FLTK widgets will appear but the programmed "
|
|
|
|
"rendering pipeline will not run.\n");
|
2022-10-06 09:43:09 +03:00
|
|
|
mode(mode() & ~FL_OPENGL3);
|
2022-09-29 10:39:21 +03:00
|
|
|
}
|
2019-04-04 19:42:45 +03:00
|
|
|
redraw();
|
2015-10-27 11:40:56 +03:00
|
|
|
}
|
2020-07-01 19:03:10 +03:00
|
|
|
|
2022-09-25 17:39:40 +03:00
|
|
|
int retval = Fl_Gl_Window::handle(event);
|
|
|
|
if (retval) return retval;
|
2022-11-01 22:06:22 +03:00
|
|
|
|
2015-10-27 11:40:56 +03:00
|
|
|
if (event == FL_PUSH && gl_version_major >= 3) {
|
|
|
|
static float factor = 1.1;
|
|
|
|
GLfloat data[4];
|
|
|
|
glGetBufferSubData(GL_ARRAY_BUFFER, 0, 4*sizeof(GLfloat), data);
|
|
|
|
if (data[0] < -0.88 || data[0] > -0.5) factor = 1/factor;
|
|
|
|
data[0] *= factor;
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, 4*sizeof(GLfloat), data);
|
|
|
|
glGetBufferSubData(GL_ARRAY_BUFFER, 24*sizeof(GLfloat), 4*sizeof(GLfloat), data);
|
|
|
|
data[0] *= factor;
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 24*sizeof(GLfloat), 4*sizeof(GLfloat), data);
|
|
|
|
redraw();
|
2016-06-22 10:45:53 +03:00
|
|
|
add_output("push Fl_Gl_Window::pixels_per_unit()=%.1f\n", pixels_per_unit());
|
2015-10-27 11:40:56 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2022-09-25 17:39:40 +03:00
|
|
|
return retval;
|
2015-10-27 11:40:56 +03:00
|
|
|
}
|
2022-09-29 10:39:21 +03:00
|
|
|
void reset(void) { shaderProgram = 0; gl_texture_reset(); }
|
2015-10-27 11:40:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void toggle_double(Fl_Widget *wid, void *data) {
|
|
|
|
static bool doublebuff = true;
|
|
|
|
doublebuff = !doublebuff;
|
|
|
|
SimpleGL3Window *glwin = (SimpleGL3Window*)data;
|
|
|
|
int flags = glwin->mode();
|
|
|
|
if (doublebuff) flags |= FL_DOUBLE; else flags &= ~FL_DOUBLE;
|
|
|
|
glwin->reset();
|
2022-09-28 18:06:00 +03:00
|
|
|
glwin->hide();
|
|
|
|
glwin->mode(flags);
|
|
|
|
glwin->show();
|
2015-10-27 11:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Fl_Text_Display *output; // shared between output_win() and add_output()
|
|
|
|
|
|
|
|
void output_win(SimpleGL3Window *gl)
|
|
|
|
{
|
|
|
|
output = new Fl_Text_Display(300,0,500, 280);
|
|
|
|
Fl_Light_Button *lb = new Fl_Light_Button(300, 280, 500, 20, "Double-Buffered");
|
|
|
|
lb->callback(toggle_double);
|
|
|
|
lb->user_data(gl);
|
|
|
|
lb->value(1);
|
|
|
|
output->buffer(new Fl_Text_Buffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void add_output(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char line_buffer[10000];
|
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf(line_buffer, sizeof(line_buffer)-1, format, args);
|
|
|
|
va_end(args);
|
2015-12-01 19:17:37 +03:00
|
|
|
output->buffer()->append(line_buffer);
|
|
|
|
output->scroll(10000, 0);
|
2015-10-27 11:40:56 +03:00
|
|
|
output->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-06 09:43:09 +03:00
|
|
|
void button_cb(Fl_Widget *widget, void *) {
|
|
|
|
add_output("run %s callback\n", widget->label());
|
2022-09-25 17:39:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_widgets(Fl_Gl_Window *g) {
|
2022-09-28 18:06:00 +03:00
|
|
|
Fl::set_color(FL_FREE_COLOR, 255, 255, 255, 140); // partially transparent white
|
2022-09-25 17:39:40 +03:00
|
|
|
g->begin();
|
|
|
|
// Create here widgets to go above the GL3 scene
|
2022-10-06 09:43:09 +03:00
|
|
|
Fl_Button* b = new Fl_Button( 0, 0, 60, 30, "button");
|
2022-09-25 17:39:40 +03:00
|
|
|
b->color(FL_FREE_COLOR);
|
2022-10-06 09:43:09 +03:00
|
|
|
b->box(FL_DOWN_BOX );
|
2022-09-25 17:39:40 +03:00
|
|
|
b->callback(button_cb, NULL);
|
2022-10-06 09:43:09 +03:00
|
|
|
Fl_Button* b2 = new Fl_Button( 0, 170, 60, 30, "button2");
|
|
|
|
b2->color(FL_FREE_COLOR);
|
|
|
|
b2->box(FL_BORDER_BOX );
|
|
|
|
b2->callback(button_cb, NULL);
|
2022-09-25 17:39:40 +03:00
|
|
|
g->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-27 11:40:56 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Fl::use_high_res_GL(1);
|
|
|
|
Fl_Window *topwin = new Fl_Window(800, 300);
|
|
|
|
SimpleGL3Window *win = new SimpleGL3Window(0, 0, 300, 300);
|
|
|
|
win->end();
|
|
|
|
output_win(win);
|
2022-09-25 17:39:40 +03:00
|
|
|
add_widgets(win);
|
2015-10-27 11:40:56 +03:00
|
|
|
topwin->end();
|
|
|
|
topwin->resizable(win);
|
|
|
|
topwin->label("Click GL panel to reshape");
|
|
|
|
topwin->show(argc, argv);
|
|
|
|
Fl::run();
|
|
|
|
}
|
2015-12-10 19:20:37 +03:00
|
|
|
|