Showing posts with label Microsoft Visual Studio. Show all posts
Showing posts with label Microsoft Visual Studio. Show all posts

Sunday, September 25, 2011

C++ FLTK OPENGL 3D ANIMATION TUTORIAL









REVISED: Friday, March 22, 2013



CONTENTS:
I.     INTRODUCTION
II.    INSTALLING OPENGL
III.   USING FLTK IN MICROSOFT VISUAL STUDIO
IV.   3D ANIMATION USING FLTK AND OPENGL

YOU WILL LEARN HOW TO:
1. INSTALL AND USE OPENGL WITH FLTK.
2. CODE 3D ANIMATION WITH FLTK OPENGL.

• I. INTRODUCTION


Welcome to the “C++ FLTK OPENGL 3D ANIMATION TUTORIAL"

• II. INSTALLING OPENGL


There are lots of free download websites you can Google; 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.

• III. USING 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:

fltkd.lib wsock32.lib comctl32.lib fltkjpegd.lib fltkimagesd.lib


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.

• IV. 3D ANIMATION USING FLTK AND OPENGL


Please copy and paste the following example 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 the example program to test to see if you can now code in OpenGL using FLTK.

//***********************************
//Texture Mapped Cube
//erco/loic 03/17/09
//***********************************
#include <cmath>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#include <stdio.h>
#include <math.h>
#include <FL/Fl.H>
#include <FL/Fl_window.H>
#include <FL/Fl_Gl_window.H>
#include <FL/gl.h>

//***********************************
//OpenGL spinning cube with
//checker texturemap
//***********************************
#define WIN_W 400      
//window width
#define WIN_H 400       
//window height
#define TEX_W 64        
//texturemap width
#define TEX_H 64        
//texturemap height
#define FPS (1.0/24.0)  
//frames per second playback

//***********************************
//OpenGL class to show
//texturemapped cube
//***********************************
class MyGlWindow : public Fl_Gl_Window {
    double spin;
    GLuint TexID;
//***********************************
//TIMER CALLBACK
//Handles rotation the object
//***********************************
    static void Timer_CB(void *userdata) {
        MyGlWindow *mygl = (MyGlWindow*)userdata;
        mygl->spin += 2.0;      
//spin
        mygl->redraw();
        Fl::repeat_timeout(FPS, Timer_CB, userdata);
    }
public:
//CTOR
    MyGlWindow(int x,int y,int w,int h,const char *l=0) : Fl_Gl_Window(x,y,w,h,l) {
        spin = 0.0;
        Fl::add_timeout(FPS, Timer_CB, (void*)this);       
//24fps timer
    }
//***********************************
//PERSPECTIVE VIEW
//Same as gluPerspective()..
//***********************************
    void Perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
        GLdouble xmin, xmax, ymin, ymax;
        ymax = zNear * tan(fovy * M_PI / 360.0);
        ymin = -ymax;
        xmin = ymin * aspect;
        xmax = ymax * aspect;
        glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
    }
//***********************************
//RESHAPED VIEWPORT
//OpenGL
//window changes size
//***********************************
    void ReshapeViewport() {
//***********************************
//Viewport
//***********************************
        glViewport(0, 0, w(), h());
//***********************************
//Projection
//***********************************
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GLfloat ratio = w() / h();
        Perspective(30.0, 1.0*ratio, 1.0, 30.0);
        glTranslatef(0.0, 0.0, -8.0);
//***********************************
//Model view
//***********************************
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
//***********************************
//OPENGL INITIALIZATION
//OpenGL do *only once* on startup.
//***********************************
    void GlInit() {
//***********************************
//Make sure we only do this once
//***********************************
        static int first_time = 1;
        if ( first_time ) {
            first_time = 0;
//***********************************
//Texture Map Init
//***********************************
            GLubyte img[TEX_W][TEX_H][3]; //after glTexImage2D(), array is no longer needed
            glGenTextures(1, &TexID);
            glBindTexture(GL_TEXTURE_2D, TexID);
//***********************************
//Texture Mapping Mode
//Uncomment one of the following lines:
//GL_DECAL or GL_MODULATE
//glTexEnvf(GL_TEXTURE_ENV,
//GL_TEXTURE_ENV_MODE,
//GL_DECAL);   
//use actual texture colors
//***********************************
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
//texture colors affected by poly's color

            for (int x=0; x<TEX_W; x++) {
                for (int y=0; y<TEX_H; y++) {
//***********************************
//Texture Pattern
//Uncomment one of the following lines:
//checkboard or basketweave
//GLubyte c = ((x&16)^(y&16)) ? ((x%16)
//<<4) : (((x%16)^15)<<4); //basket weave
//***********************************
                    GLubyte c = ((x&16)^(y&16)) ? 255 : 0;                        
//checkerboard
                    img[x][y][0] = c;
                    img[x][y][1] = c;
                    img[x][y][2] = c;
                }
            }
            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_W, TEX_H, 0, GL_RGB, GL_UNSIGNED_BYTE, &img[0][0][0]);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glEnable(GL_TEXTURE_2D);
//***********************************
//OpenGL settings
//***********************************
            glShadeModel(GL_FLAT);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LEQUAL);
        }
    }
//***********************************
//FLTK DRAW
//Called by FLTK to draw the scene.
//***********************************
    void draw() {
//***********************************
//Initialize/handle reshaped viewport
//***********************************
        if ( !valid() ) {
            valid(1);
            GlInit();
            ReshapeViewport();
        }
//***********************************
//Clear
//***********************************
        glClearColor(.5,.5,.5, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//***********************************
//Setup model matrix
//***********************************
        glLoadIdentity();
        glRotatef(spin, 0.5, 1.0, 0.0); 
//show all sides of cube

//***********************************
//Draw cube with texture assigned
//to each face
//***********************************
        glBegin(GL_QUADS);
//***********************************
//Front Face
//***********************************
        glColor3f(1.0, 0.0, 0.0); 
//red
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Back Face
//***********************************
        glColor3f(0.0, 1.0, 1.0); 
//cyn
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Top Face
//***********************************
        glColor3f(0.0, 1.0, 0.0); 
//grn
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Bottom Face
//***********************************
        glColor3f(1.0, 0.0, 1.0); 
//mag
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Right face
//***********************************
        glColor3f(0.0, 0.0, 1.0); 
//blu
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Left Face
//***********************************
        glColor3f(1.0, 1.0, 0.0); 
//yel
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); 
//Bottom Left Of The Texture and Quad
        glEnd();
//***********************************
//DEBUG: CHECK FOR ERRORS
//***********************************
        GLenum err = glGetError();
        if ( err != GL_NO_ERROR ) {
            fprintf(stderr, "GLGETERROR=%d\n", (int)err);
        }
    }
};
int main(int argc, char *argv[]) {
    MyGlWindow* mygl = new MyGlWindow(10, 10, WIN_W-20, WIN_H-20, "Texture Test");
    mygl->end();
    mygl->resizable(mygl);
    mygl->show();
    return(Fl::run());
}

YOU HAVE  LEARNED HOW TO:
1. INSTALL AND USE OPENGL WITH FLTK.
2. CODE 3D ANIMATION WITH FLTK OPENGL.

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"




C++ FLTK GRAPHICS TUTORIAL









REVISED: Friday, March 22, 2013




CONTENTS:
I.    INTRODUCTION
II.  FLTK
III. DOWNLOADING FLTK
IV. INSTALLING FLTK
V.  USING FLTK IN MICROSOFT VISUAL STUDIO
VI. TESTING FLTK

YOU WILL LEARN HOW TO:
1. Download FLTK.
2. Install FLTK.
3. Use FLTK in visual studio.
4. Test FLTK.

• I. INTRODUCTION


Welcome to the “C++ FLTK Graphics Tutorial.”

This tutorial describes how to install FLTK using Microsoft Visual Studio. If you use another system refer to the downloaded files main folder for directions for your system.

This tutorial assumes you have a basic understanding of classes, inheritance, pointers and dynamic memory allocation.

This tutorial also assumes you already have Microsoft Visual Studio C++ installed on your computer and you are familiar with its use.

• II. FLTK


Fast Light Tool Kit (FLTK) pronounced "full tick" is a cross-platform Graphical User Interface (GUI) library. FLTK is free open source software, it is fun and easy to learn, and has a short learning curve for new users.

• III. DOWNLOADING FLTK


Use the following steps to download FLTK:

A. Go to http://www.fltk.org/


B. Click download in the navigation menu.

C. Choose FLTK 1.1.9 in the menu.

D. Choose the zip file format download.


The file you will receive will be in a zip file format. Use WinZip or a similar program to unzip and extract the files into normal files on your C drive. When you are done you will have a folder on your C drive that looks like this:

C:\fltk-1.1.9

• IV. INSTALLING FLTK


Use the following steps to install FLTK:

A. Open the VISUALC folder.


Double click the FLTK setup file that looks like a green, orange, and yellow infinity symbol.

The FLTK setup file update will open your Microsoft Visual Studio C++ program and begin the FLTK installation.


B. When Microsoft Visual Studio C++ asks about updating old projects choose “Yes to All.”

When the updating of old projects is completed you will see “Ready” appear in the bottom left corner of your screen.

C. From the Microsoft Visual Studio C++ “Build Menu,” choose “Build Solution.”

When the “Build Solution” has successfully completed you will see 71 succeeded, 0 failed, 0 up to date, and 0 skipped.

If you do not receive this message, the Build failed and you will see error messages telling you what went wrong. If this happens post a question on the DIC website in the forums and either I or someone else will help you.

D. Close Visual Studio.

E. Open your C:\ drive directory.

From C:\fltk-1.1.9 open the lib folder. Copy all the seven .lib files, except for the README.LIB, into:

C:\Program Files\Microsoft Visual Studio\Vc\lib

F. From C:\fltk-1.1.9 copy the FL folder into:

C:\Program Files\ Microsoft Visual Studio\Vc\include

• V. USING FLTK IN MICROSOFT VISUAL STUDIO


Use the following steps to use FLTK in Microsoft Visual Studio:

A. Open Visual Studio


Create a Win32 console application empty project.

B. From 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 enter the following text:

fltkd.lib wsock32.lib comctl32.lib fltkjpegd.lib fltkimagesd.lib


In the “Ignore Specific Library” text field enter the following text:

libcd.lib


C. From Visual Studio create a new .cpp file.

From Visual Studio main top menu choose Project and from the drop-down menu choose Properties.

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, change the “Runtime Library” drop-down to:

Multi-threaded Debug DLL (\MDd)


Click OK to close the Properties window.

• VI. TESTING FLTK


Use the following steps to test FLTK:

A. From the .cpp file type the following code:

#include <Fl/Fl.h>
#include <Fl/Fl_Box.h>
#include <Fl/Fl_window.h>

int main()
{
     Fl_Window window(200,200, "Window title");
     Fl_Box box(0,0,200,200,"Hello World!");
     window.show();
     return Fl::run();
}

B. Build and Debug the .cpp file.


A successful run will create a blank FLTK window.

Future tutorials will explain how to use FLTK.

YOU HAVE LEARNED HOW TO:
1. Download FLTK.
2. Install FLTK.
3. Use FLTK in visual studio.
4. Test 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"