PDA

View Full Version : Example Q, FLTKt and OpenGL without using QOpenGL



giorgik
26th December 2012, 19:28
Hello to all. I found an example of a program that uses the libraries FLTK and OpenGL. These same examples can be used very well in Qt. Below I report the sample project.
OpenGLSphere.pro:


QT += core
QT -= gui

TARGET = OpenGLSphere
#CONFIG += console così non appare la finestra dos
CONFIG -= app_bundle

TEMPLATE = app

#------------------------------------------------
#
# Uso librerie FLTK 1.3.1
#
#------------------------------------------------
INCLUDEPATH += C:/MinGW/include \
C:/MinGW/include/FL/images

DEFINES += WIN32 USE_OPENGL32="1" LARGEFILE_SOURCE="1" LARGEFILE64_SOURCE="1"

LIBS += -mwindows \
-L C:/MinGW/lib \
-lfltk \
-lfltk_forms \
-lfltk_gl \
-lfltk_images \
-lfltk_jpeg \
-lfltk_png \
-lfltk_z \
-lole32 -luuid -lcomctl32

#------------------------------------------------
#
# Uso librerie Gl (OpenGL)
#
#------------------------------------------------
LIBS += -L C:/MinGW/lib \
#-lglaux \
-lopenglut \
-lglu32 \
-lopengl32 \
-lwinmm -lgdi32


SOURCES += main.cpp

HEADERS += \
simple.h

simple.h:


#ifndef SIMPLE_H
#define SIMPLE_H

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/glut.H>
#include <FL/gl.h>

#define WIDTH 640
#define HEIGHT 480

//
// Render a simple opengl shaded sphere with a single side light -- erco 11/28/08
//
// 1.1 Mods to use pure glut calls for subwindow -- erco 03/15/11
//
// You can build this example with: fltk-config --use-glut --compile sphere.cxx
//

// GLUT: RESHAPE THE VIEWPORT
void Reshape(int W, int H)
{
// (REFERENCE: SGI light.c DEMO)
GLfloat ratio = (float)W / (float)H;
glViewport(0, 0, (GLsizei)W, (GLsizei)H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5 * ratio, 1.5 * ratio, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// GLUT: REDRAW THE SCENE
void Redraw()
{
static int valid = 0;

if (!valid)
{
valid = 1;
Reshape(WIDTH, HEIGHT);

// (REFERENCE: SGI 'light.c' EXAMPLE)
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat light_position[] = { 5.0, 5.0, 0.0, 0.0 }; // XYZ
glClearColor(0.0, 0.0, 0.4, 0.0); // bg color
glShadeModel(GL_SMOOTH);

//
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 20.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

//
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glutSolidSphere(0.5, 30, 30);
glPopMatrix();
}

#endif // SIMPLE_H

main.cpp:


#include "simple.h"

int main(int argc, char *argv[])
{
Fl_Window win(WIDTH, HEIGHT, "sphere");
win.resizable(win);
win.show(argc, argv);

// Docs say to add glut subwindow /after/ calling win.show()
win.begin();

// glutInit(&argc, argv); // docs say not to call this if a subwindow
glutInitWindowSize(WIDTH - 20, HEIGHT - 20);
glutInitWindowPosition(10, 10); // place inside parent window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("sphere");
glutReshapeFunc(Reshape);
glutDisplayFunc(Redraw);
win.end();
return(Fl::run());
}

This code fails to compile in MinGW fine, but if I try to do it in the ide QtCreator 2.6.0 fails as it finds defined 2 functions:


glut_compatability.cxx:-1: error: undefined reference to `Fl::remove_idle(void (*)(void*), void*)'
glut_compatability.cxx:-1: error: undefined reference to `Fl::add_idle(void (*)(void*), void*)'

Can you help me solve this problem ?

amleto
26th December 2012, 20:27
you havent linked it properly. This is qt programming forum. Where is your qt programming question?

This code fails to compile in MinGW fine, but if I try to do it in the ide QtCreator 2.6.0 fails as it finds defined 2 functions:
What?
"fails to compile in mingw fine"
doesn't make sense.

but if I try to do it in the ide QtCreator 2.6.0 fails as it finds defined 2 functions
qt creator will be using mingw. The problem is the the linker DOESNT find symbols, not that "it finds defined 2 functions".

giorgik
26th December 2012, 20:48
sorry it was a mistake in translation from Italian to English.
Using MinGW with MSYS: OK, compiled
Using MinGW with QtCreator: KO all, not compiled

wysota
26th December 2012, 20:56
So why don't you continue using MinGW with MSYS if it works fine?

giorgik
26th December 2012, 22:29
My target is to use QtCreator as IDE and compiler MinGW. Below I command (command line) to successfully compile the file sphere.cpp


C:\Qt\progetti\FLTK\OpenGLSphere>g++ -I C:/MinGW/include -I C:/MinGW/include/FL/images -mwindows -DWIN32 -DUSE_OPENGL32 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -o 'sphere' 'sphere.cpp' -mwindows C:/MinGW/lib/libfltk_gl.a -lglu32 -lopengl32 C:/MinGW/lib/libfltk.a -lole32 -luuid -lcomctl32

sphere.cpp:


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/glut.H>
#include <FL/gl.h>
#define WIDTH 640
#define HEIGHT 480
//
// Render a simple opengl shaded sphere with a single side light -- erco 11/28/08
//
// 1.1 Mods to use pure glut calls for subwindow -- erco 03/15/11
//
// You can build this example with: fltk-config --use-glut --compile sphere.cxx
//

// GLUT: RESHAPE THE VIEWPORT
void Reshape(int W, int H) {
// (REFERENCE: SGI light.c DEMO)
GLfloat ratio = (float)W / (float)H;
glViewport(0, 0, (GLsizei)W, (GLsizei)H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5*ratio, 1.5*ratio, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// GLUT: REDRAW THE SCENE
void Redraw() {
static int valid = 0;
if (!valid) {
valid = 1;
Reshape(WIDTH, HEIGHT);
// (REFERENCE: SGI 'light.c' EXAMPLE)
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat light_position[] = { 5.0, 5.0, 0.0, 0.0 }; // XYZ
glClearColor(0.0, 0.0, 0.4, 0.0); // bg color
glShadeModel(GL_SMOOTH);
//
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 20.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glutSolidSphere(0.5, 30, 30);
glPopMatrix();
}
int main(int argc, char *argv[]) {
Fl_Window win(WIDTH, HEIGHT, "sphere");
win.resizable(win);
win.show(argc, argv);
// Docs say to add glut subwindow /after/ calling win.show()
win.begin();
// glutInit(&argc, argv); // docs say not to call this if a subwindow
glutInitWindowSize(WIDTH-20, HEIGHT-20);
glutInitWindowPosition(10,10); // place inside parent window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("sphere");
glutReshapeFunc(Reshape);
glutDisplayFunc(Redraw);
win.end();
return(Fl::run());
}

In the file sphere.pro as I set to get a successful compilation ?

wysota
26th December 2012, 22:45
If you mean that you want to change direct invokation of the compiler into managing your project by qmake then if you want to link an external library to your project, you have to inform qmake about it by modifying the LIBS variable. You can see the compiler invokation qmake does and you can compare it to yours to see the difference. I'm assuming this will be that you are using a static version of the Fltk library in your direct invokation which is not reflected in your qmake project.

giorgik
27th December 2012, 10:36
Yes, Fltk libraries are static and therefore should be linked to main.o I was hoping you'd tell me how is the corresponding write sphere.pro known command from the command line with MinGW ...:(
I'll have to experiment and go trial

wysota
27th December 2012, 11:43
It's exactly the same as your direct call. You need to add the same directive you have in your direct invokation to the LIBS variable in the project file (e.g. "LIBS+=C:/MinGW/lib/libfltk_gl.a", etc.)

giorgik
27th December 2012, 19:56
Yes,


#------------------------------------------------
#
# Uso librerie FLTK 1.3.1
#
#------------------------------------------------
INCLUDEPATH += C:/MinGW/include \
C:/MinGW/include/FL/images

LIBS += -mwindows

DEFINES += WIN32 USE_OPENGL32 LARGEFILE_SOURCE LARGEFILE64_SOURCE

LIBS += -mwindows C:/MinGW/lib/libfltk_gl.a -lglu32 -lopengl32 C:/MinGW/lib/libfltk.a -lole32 -luuid -lcomctl32


I have another question for you, but it is part of another topic "create in Qt command script fltk-config". Please give us a look ?