CONTENTS:
I. INTRODUCTION
II. EXAMPLE 1 - C++ CLASS DERIVED FROM FLTK WIDGET
III. INSTALLING OPENGL
IV. USING OPENGL WITH FLTK IN MICROSOFT VISUAL STUDIO
V. EXAMPLE 2 - OPENGL TEXT ON A ROTATING 3D OBJECT
YOU WILL LEARN HOW TO:
1. CODE A C++ CLASS DERIVED FROM A FLTK WIDGET.
2. INSTALL AND USE OPENGL WITH FLTK.
• I. INTRODUCTION
Welcome to the “C++ FLTK INSTALL OPENGL TUTORIAL”
This tutorial is a very brief overview of information presented by Dr. Bjarne Stroustrup in his book “Programming Principles and Practices Using C++,” Addison-Wesley 2009, ISBN 978-0321543721.
The web site for his book and the appropriate FLTX header files is:
http://www.stroustrup.com/Programming/
• II. EXAMPLE 1 - C++ CLASS DERIVED FROM FLTK WIDGET
Please copy and paste Example 1 into your IDE then build and debug.
//***********************************
//Exampe 1
//C++ CLASS DERIVED FROM FLTK WIDGET
//***********************************
#include <Fl/Fl.H>
//Required by all FLTK programs.
#include <Fl/Fl_window.H>
//Required for window class.
#include <FL/FL_Button.H>
//Required for button class.
#include <stdio.h>
//Required for display.
#include <stdlib.h>
class Passes_Userdata : public Fl_Window
{
private:
static void MyCallback(Fl_Widget*w, void* userdata)
{
fprintf(stderr, "Button Clicked! Userdata passed is %d\n", (int)userdata);
}
public:
int data;
Passes_Userdata(int w, int h, const char *name = 0) : Fl_Window(w, h, name)
{
data = 54321;
Fl_Button *but = new Fl_Button(5, 5, 75, 30, "Click Me");
but->callback(MyCallback, (void*)54321);
show();
}
};
int main()
{
Passes_Userdata passes_userdata(600,400,"FLTK is easy to learn and fun to use!");
return(Fl::run());
//Returns value of FLTK command loop,
//this function returns only when all
//windows are closed.
}
Example 1 creates a C++ class derived from a FLTK Fl widget.
The Fl widget we used was Fl_window.
We made everything public to make it easier to understand.
When we coded the constructor we followed the FLTK parameter order of width, height, and label name.
We declared the window button inside of the class constructor.
We made the callback for the button a private member of the class.
Defining the class will automatically show the window.
There are lots of free download websites you can use; this is one of the many for OpenGL95:
http://download-opengl-graphics.com/
Download and use WinZip or a similar package to extract the OpenGL95.zip file to your C:\ drive.
• IV. USING OPENGL WITH FLTK IN MICROSOFT VISUAL STUDIO
Use the following steps to use OpenGL with FLTK in Microsoft Visual Studio:
1. Open Visual Studio
Use the same Win32 console application empty project you have been using for all of your non-OpenGL FLTK applications.
2. From the Visual Studio main top menu choose Project and from the drop-down menu choose Properties.
To expand a sub-menu click the Linker folder in the left menu of the Properties dialog box. In the sub-menu click Input. On the right, in the additional dependencies text field you should see the following entries we added for FLTK:
At the end of this list add the following four libraries:
opengl32.lib glu32.lib fltkgld.lib glaux.lib
In the “Ignore Specific Library” text field you should already have the following text if you do not have it add it now:
libcd.lib
In the left menu of the Properties window click C\C++ to expand a sub-menu. Click the “Code Generation” sub-menu item. On the right, if it has not already been changed, change the “Runtime Library” drop-down to:
Multi-threaded Debug DLL (\MDd)
Click OK to close the Properties window.
• V. EXAMPLE 2 - OPENGL TEXT ON A ROTATING 3D OBJECT.
Please copy and paste Example 2 into your IDE. You have to use the same Project file we just setup. It will not work in other Project files until you set them up the same way we did the one above. Then, build and debug Example 2 to test to see if you can now code in OpenGL using FLTK.
//***********************************
//Example 2
//OPENGL TEXT ON A ROTATING
//3D OBJECT
//***********************************
#include <FL/Fl.H>
#include <FL/Fl_Gl_window.H>
#include <FL/gl.h>
#include <GL/glu.h>
#include <string.h>
#include <stdio.h>
//Tetrahedron points.
#define TOP 0, 1, 0
#define RIGHT 1, -1, 1
#define LEFT -1, -1, 1
#define BACK 0, -1, -1
class MyGlWindow : public Fl_Gl_Window {
float rotangle;
void draw() {
//Init viewport.
if (!valid()) {
valid(1);
// Initialize GL.
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//Position camera/viewport init.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w(),h());
gluPerspective(45.0, (float)w()/(float)h(), 1.0, 10.0);
glTranslatef(0.0, 0.0, -5.0);
//Position object.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rotangle, 1, 0, 1);
glRotatef(rotangle, 0, 1, 0);
glRotatef(rotangle, 1, 1, 1);
//Draw tetrahedron.
glColor3f(1.0, 0.0, 0.0); glBegin(GL_POLYGON); glVertex3f(TOP); glVertex3f(RIGHT); glVertex3f(LEFT); glEnd();
glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f(TOP); glVertex3f(BACK); glVertex3f(RIGHT); glEnd();
glColor3f(0.0, 0.0, 1.0); glBegin(GL_POLYGON); glVertex3f(TOP); glVertex3f(LEFT); glVertex3f(BACK); glEnd();
glColor3f(0.5, 0.5, 0.5); glBegin(GL_POLYGON); glVertex3f(RIGHT); glVertex3f(BACK); glVertex3f(LEFT); glEnd();
//Print tetrahedron's points on object.
//Disable depth buffer while drawing text,
//so text draws /over/ object.
glDisable(GL_DEPTH_TEST);
{
const char *p;
gl_font(1, 12);
glColor3f(1.0, 1.0, 1.0);
glRasterPos3f(TOP); p = "+ top"; gl_draw(p, strlen(p));
glRasterPos3f(LEFT); p = "+ left"; gl_draw(p, strlen(p));
glRasterPos3f(RIGHT); p = "+ right"; gl_draw(p, strlen(p));
glRasterPos3f(BACK); p = "+ back"; gl_draw(p, strlen(p));
}
glEnable(GL_DEPTH_TEST);
//Print rotangle value at fixed position
//at lower left.
char s[40];
sprintf(s, "ROT=%.2f", rotangle);
glLoadIdentity(); glRasterPos2f(-3,-2); gl_draw(s, strlen(s));
}
static void Timer_CB(void *userdata) {
MyGlWindow *o = (MyGlWindow*)userdata;
o->rotangle += 1.0;
o->redraw();
Fl::repeat_timeout(1.0/24.0, Timer_CB, userdata); //24fps
}
public:
//CONSTRUCTOR
MyGlWindow(int X,int Y,int W,int H,const char*L=0) : Fl_Gl_Window(X,Y,W,H,L) {
rotangle = 0;
Fl::add_timeout(3.0, Timer_CB, (void*)this); //Wait 3 secs before animation begins.
}
};
//MAIN
int main() {
Fl_Window win(500, 300);
MyGlWindow mygl(10, 10, win.w()-20, win.h()-20);
win.show();
return(Fl::run());
}
YOU HAVE LEARNED HOW TO:
1. CODE A C++ CLASS DERIVED FROM A FLTK WIDGET.
2. INSTALL AND USE OPENGL WITH FLTK.
Elcric Otto Circle
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment