Page 1 of 1

C++ 3D ANIMATION FLTK OPENGL TUTORIAL C++ 3D ANIMATION FLTK OPENGL TUTORIAL Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 08 September 2009 - 12:51 AM

C++ 3D ANIMATION FLTK OPENGL TUTORIAL


YOU WILL LEARN HOW TO:

1. INSTALL AND USE OPENGL WITH FLTK.

2. CODE 3D ANIMATION WITH FLTK OPENGL.

• I. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ 3D Animation FLTK OpenGL Tutorial

• II. INSTALLING OPENGL

There are lots of free download websites you can Google; this is one of the many for OpenGL95:

http://www.freedownl...L_Download.html

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.

• V. 3D ANIMATION USING FLTK AND OPENGL

Please copy and paste the 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 stuff to do whenever
	//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 stuff to 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);
			//********************************************
			//Misc 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());
}



If the example does not work post your questions in the DIC forum and either I or someone else will help you.

Is This A Good Question/Topic? 3
  • +

Replies To: C++ 3D ANIMATION FLTK OPENGL TUTORIAL

#2 kxng66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-April 10

Posted 03 February 2011 - 02:28 PM

Hello,

I am struggling in setting up the fltk opengl in VC++. I followed your instruction, it still generate many linking errors. Can you send me your sample project in VC++ 6.0 as my starting point? Otherwise, can you walk me step by step in setting it up?

I can run fltk in VC++ 6.0 but once I added fl_gl_window object in my code, problem occurs.

Thanks in advance,

Giau
Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 01 August 2011 - 09:18 AM

Hello kxng66,

Thank you very much for your comments:

View Postkxng66, on 03 February 2011 - 05:28 PM, said:

Hello,

I am struggling in setting up the fltk opengl in VC++. I followed your instruction, it still generate many linking errors. Can you send me your sample project in VC++ 6.0 as my starting point? Otherwise, can you walk me step by step in setting it up?

I can run fltk in VC++ 6.0 but once I added fl_gl_window object in my code, problem occurs.

Thanks in advance,

Giau


If the example still does not work, please post your questions in the DIC forum and either I or someone else will help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1