Showing posts with label OpenGL. Show all posts
Showing posts with label OpenGL. Show all posts

Sunday, January 10, 2010

How to Create Makefile from Imakefile

Here we'll describe how to create Makefile using Imakefile.

Create the Imakefile for example with following contents
TARGET = program.out
OBJS = main.o
SRCS = main.cpp

CC = g++
CCOPTIONS = -g

MESAINCDIR = /usr/include
MESALIBDIR = /usr/lib

GLLIB = -L$(MESALIBDIR) -lglut -lGLU -lGL
XLIB = -L/usr/lib -lXmu -lXi -lXext -lX11 -lm -lpthread
INCLUDES = -I$(MESAINCDIR)

LOCAL_LIBRARIES = $(GLLIB) $(XLIB)

ComplexProgramTarget($(TARGET))

Use xmkmf command to create makefile from Imakefile.
$ xmkmf

xmkmf script actually uses imake command to create Imakefile. Hence following command would also work.

$ imake -DUseInstalled -I/usr/share/X11/config

xmkmf command does not necessarily require a parameter. It looks for a file named Imakefile inside current dirctory to create makefile.

If imake package is not installed bash will tell you that Command not found. For Fedora Core Systems login as root and type the following command.

# yum -y install imake

For Ubuntu the command will be something like this.

$ sudo apt-get install imake

Now imake or xmkmf command will work.

Now let's test it compiling and running a program. Create your source file main.cpp for example with following contents.

/* Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* File for "Basic Shapes" lesson of the OpenGL tutorial on
 * www.videotutorialsrock.com
 */



#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
     int x, int y) {    //The current mouse coordinates
 switch (key) {
  case 27: //Escape key
   exit(0); //Exit the program
 }
}

//Initializes 3D rendering
void initRendering() {
 //Makes 3D drawing work when something is in front of something else
 glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
 //Tell OpenGL how to convert from coordinates to pixel values
 glViewport(0, 0, w, h);
 
 glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
 
 //Set the camera perspective
 glLoadIdentity(); //Reset the camera
 gluPerspective(45.0,                  //The camera angle
       (double)w / (double)h, //The width-to-height ratio
       1.0,                   //The near z clipping coordinate
       200.0);                //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
 //Clear information from last draw
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
 glLoadIdentity(); //Reset the drawing perspective
 
 glBegin(GL_QUADS); //Begin quadrilateral coordinates
 
 //Trapezoid
 glVertex3f(-0.7f, -1.5f, -5.0f);
 glVertex3f(0.7f, -1.5f, -5.0f);
 glVertex3f(0.4f, -0.5f, -5.0f);
 glVertex3f(-0.4f, -0.5f, -5.0f);
 
 glEnd(); //End quadrilateral coordinates
 
 glBegin(GL_TRIANGLES); //Begin triangle coordinates
 
 //Pentagon
 glVertex3f(0.5f, 0.5f, -5.0f);
 glVertex3f(1.5f, 0.5f, -5.0f);
 glVertex3f(0.5f, 1.0f, -5.0f);
 
 glVertex3f(0.5f, 1.0f, -5.0f);
 glVertex3f(1.5f, 0.5f, -5.0f);
 glVertex3f(1.5f, 1.0f, -5.0f);
 
 glVertex3f(0.5f, 1.0f, -5.0f);
 glVertex3f(1.5f, 1.0f, -5.0f);
 glVertex3f(1.0f, 1.5f, -5.0f);
 
 //Triangle
 glVertex3f(-0.5f, 0.5f, -5.0f);
 glVertex3f(-1.0f, 1.5f, -5.0f);
 glVertex3f(-1.5f, 0.5f, -5.0f);
 
 glEnd(); //End triangle coordinates
 
 glutSwapBuffers(); //Send the 3D scene to the screen
}

int main(int argc, char** argv) {
 //Initialize GLUT
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(400, 400); //Set the window size
 
 //Create the window
 glutCreateWindow("Basic Shapes - videotutorialsrock.com");
 initRendering(); //Initialize rendering
 
 //Set handler functions for drawing, keypresses, and window resizes
 glutDisplayFunc(drawScene);
 glutKeyboardFunc(handleKeypress);
 glutReshapeFunc(handleResize);
 
 glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
 return 0; //This line is never reached
}

Now apply make command to compile and build the program. And then as in our Imakefile output file name was program.out type ./program.out to display output.

$ make
c++ -m32 -O2 -fno-strength-reduce -fno-strict-aliasing   -I/usr/include \
-I/usr/include -Dlinux -D__i386__ -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE\ 
-D_XOPEN_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_LARGEFILE_SOURCE \
-D_FILE_OFFSET_BITS=64 -c -o main.o main.cpp
rm -f program.out
g++ -o program.out -O2 -fno-strength-reduce -fno-strict-aliasing -g -L/usr/lib\ 
main.o -L/usr/lib -lglut -lGLU -lGL -L/usr/lib -lXmu -lXi -lXext -lX11 -lm -lpthread
make: *** No rule to make target `program.out.man', needed by `program.out._man'.
Stop.

It is not a must that you have to use an Imakefile to compile sources. You can use simple makefiles.

For example a file named makefile with following contents will serve the same purpose.

CC = g++
CFLAGS = -Wall
PROG = basicshapes

SRCS = main.cpp

ifeq ($(shell uname),Darwin)
 LIBS = -framework OpenGL -framework GLUT
else
 LIBS = -lGL -lGLU -lglut
endif

all: $(PROG)

$(PROG): $(SRCS)
 $(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)

clean:
 rm -f $(PROG)

An easy tutorial on makefile here.
       http://mrbook.org/tutorials/make

Monday, January 4, 2010

OpenGL on Windows & Common Instructions

Download glut for Windows here here.

Common instructions for setting up opengl with Visual Studio
Many different sites have posts about this. However, I'm going to give you hints. In Pro versions SDKs are installed by default. If you are using Visual Studio Express you need to install Windows Platform SDK.

Now extract the glut archive you just downloaded from this page. Copy glut.h and paste where GL.h and GLU.h resides.


[Click image to enlarge]

The directory is PlatformSDKInstallDIR\Windows\VERSION\Include\gl
Copy dll file to System 32 folder
Copy lib file to to PlatformSDKInstallDIR\Windows\VERSION\Lib

For Visual Studio 2008 Pro files go like this.
      glut.h -> C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\gl
      glut32.dll-> C:\Windows\System32
      glut32.lib -> C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib

Testing OpenGL Code
  • Create new win32 -> win32 project.
  • Click Solution Explorer. Right click Source Files. Click Add - > New Item. Specify a name. Select .cpp as template.
  • Download this code file. Extract and copy the contents of the cpp file and paste on the empty cpp file of the IDE
  • Select Project properties additional dependency: opengl32.lib glut32.lib glu32.lib


    [Click image to enlarge]

  • To build press Alt + Shft + B. If build is successful press Ctrl + F5 to run the program.


    [Click image to enlarge]
If your opengl code does not have a WinMAIN function, instead you have a main function then the type of project to be created should be General -> Empty Project to avoid linker errors. Output of the program will start with a console. In both cases you need to remember about selecting additional dependency: opengl32.lib glut32.lib glu32.lib in project properties.

Example of such code here.

Related Links:

Sunday, January 3, 2010

How to Setup OpenGL (installing glut) on Linux and Compile Programs


Fedora Core Instructions

I am assuming that you have the primary idea about what OpenGL is and what it does. Hence, without wasting any more time let's see how to set it up or install on Linux Operating System.

First check for files gl.h and glut.h

# cd /usr/include/GL
# ls
glext.h      glu.h         glx.h         glxmd.h      internal
gl.h         glu_mangle.h  glxint.h      glxproto.h
gl_mangle.h  glxext.h      glx_mangle.h  glxtokens.h

We have gl.h but no glut.h Hence we have to install development packages. If you don't have gl.h then you have to install mesa graphics libraries or nvidia libraries if you are using nvidia graphics card.

As freeglut devel package provides glut.h we are going to install it.

# yum -y install freeglut-devel
Loaded plugins: presto, refresh-packagekit
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package freeglut-devel.i686 0:2.6.0-1.fc12 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch         Version              Repository       Size
================================================================================
Installing:
 freeglut-devel         i686         2.6.0-1.fc12         updates         112 k

Transaction Summary
================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 112 k
Downloading Packages:
Setting up and reading Presto delta metadata
updates/prestodelta                                      | 322 kB     00:30     
Processing delta metadata
Package(s) data still to download: 112 k
freeglut-devel-2.6.0-1.fc12.i686.rpm                     | 112 kB     00:11     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : freeglut-devel-2.6.0-1.fc12.i686                         1/1 

Installed:
  freeglut-devel.i686 0:2.6.0-1.fc12                                            

Complete!
# ls
freeglut_ext.h  glext.h      glu.h         glxext.h  glx_mangle.h  glxtokens.h
freeglut.h      gl.h         glu_mangle.h  glx.h     glxmd.h       internal
freeglut_std.h  gl_mangle.h  glut.h        glxint.h  glxproto.h

Installation of OpenGL in a Machine without Internet Connection

Download freeglut-devel according to your distribution using a PC that has internet connection.
      freeglut-devel for Fedora Core 8
      freeglut-devel for Fedora Core 9
      freeglut-devel for Fedora Core 10
      freeglut-devel for Fedora Core 11
      freeglut-devel for Fedora Core 12

You can take the file to your PC using a memory device like USB Stick (pen drive)etc. Copy the file in the machine in which you want to install. Login as root(you can type su and provide root password after hitting enter). cd to the directory where you copied the file. Use this syntax to install them (rpm).

# rpm -Uvh filename.rpm

For Fedora Core 12 it will be like this

# rpm -Uvh freeglut-devel-2.6.0-1.fc12.i686.rpm

Now you'll find glut.h inside /usr/include/GL and that's all about installation.

# cd /usr/include/GL
# ls
glext.h      glu.h         glx.h         glxmd.h      internal
gl.h         glu_mangle.h  glxint.h      glxproto.h
gl_mangle.h  glxext.h      glx_mangle.h  glxtokens.h

Now we have glut.h along with other files. If everything is okay and hardware acceleration is enabled then glxinfo command will show "direct rendering: Yes"

# glxinfo
name of display: :0.0
display: :0  screen: 0
direct rendering: Yes
server glx vendor string: SGI
server glx version string: 1.4
server glx extensions:
  GLX_ARB_multisample, GLX_EXT_import_context, GLX_EXT_texture_from_pixmap,
......................................................................

Ubuntu Instructions
For ubuntu I would recommend you to follow this Video tutorial. This is a good one.
       http://www.videotutorialsrock.com/opengl_tutorial/get_opengl_setup_linux/video.php

Writing Opengl Programs and Compiling on Linux

Create a new directory.

$ mkdir og

Change directory.

$ cd og
Use a text-editor to create a make file inside the directory with following contents. For example, you can use command like this, (Or you can download files from the bottom of the post)

$ gedit Makefile&

Contents

CC = g++
CFLAGS = -Wall
PROG = basicshapes

SRCS = main.cpp

ifeq ($(shell uname),Darwin)
 LIBS = -framework OpenGL -framework GLUT
else
 LIBS = -lGL -lGLU -lglut
endif

all: $(PROG)

$(PROG): $(SRCS)
 $(CC) $(CFLAGS) -o $(PROG) $(SRCS) $(LIBS)

clean:
 rm -f $(PROG)

In the same way create a file named "main.cpp" with following contents

/* Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* File for "Basic Shapes" lesson of the OpenGL tutorial on
 * www.videotutorialsrock.com
 */

#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <glut/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
                              int x, int y) {    //The current mouse coordinates
      switch (key) {
            case 27: //Escape key
            exit(0); //Exit the program
      }
}

//Initializes 3D rendering
void initRendering() {
      //Makes 3D drawing work when something is in front of something else
      glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
      //Tell OpenGL how to convert from coordinates to pixel values
      glViewport(0, 0, w, h);
     
      glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
     
      //Set the camera perspective
      glLoadIdentity();      //Reset the camera
      gluPerspective(45.0,                  //The camera angle
                  (double)w / (double)h, //The width-to-height ratio
                  1.0,             //The near z clipping coordinate
                  200.0);          //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
      //Clear information from last draw
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
      glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
      glLoadIdentity(); //Reset the drawing perspective
     
      glBegin(GL_QUADS); //Begin quadrilateral coordinates
     
      //Trapezoid
      glVertex3f(-0.7f, -1.5f, -5.0f);
      glVertex3f(0.7f, -1.5f, -5.0f);
      glVertex3f(0.4f, -0.5f, -5.0f);
      glVertex3f(-0.4f, -0.5f, -5.0f);

      
      glEnd(); //End quadrilateral coordinates
     
      glBegin(GL_TRIANGLES); //Begin triangle coordinates
     
      //Pentagon
      glVertex3f(0.5f, 0.5f, -5.0f);
      glVertex3f(1.5f, 0.5f, -5.0f);
      glVertex3f(0.5f, 1.0f, -5.0f);
     
      glVertex3f(0.5f, 1.0f, -5.0f);
      glVertex3f(1.5f, 0.5f, -5.0f);
      glVertex3f(1.5f, 1.0f, -5.0f);
     
      glVertex3f(0.5f, 1.0f, -5.0f);
      glVertex3f(1.5f, 1.0f, -5.0f);
      glVertex3f(1.0f, 1.5f, -5.0f);
     
      //Triangle
      glVertex3f(-0.5f, 0.5f, -5.0f);
      glVertex3f(-1.0f, 1.5f, -5.0f);
      glVertex3f(-1.5f, 0.5f, -5.0f);
     
      glEnd(); //End triangle coordinates
     
      glutSwapBuffers(); //Send the 3D scene to the screen
}

int main(int argc, char** argv) {
      //Initialize GLUT
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(400, 400); //Set the window size
     
      //Create the window
      glutCreateWindow("Basic Shapes - videotutorialsrock.com");
      initRendering(); //Initialize rendering
     
      //Set handler functions for drawing, keypresses, and window resizes
      glutDisplayFunc(drawScene);
      glutKeyboardFunc(handleKeypress);
      glutReshapeFunc(handleResize);
     
      glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
      return 0; //This line is never reached
}


Now compile (simple make command will do this) and run the program

# make
g++ -Wall -o basicshapes main.cpp -lGL -lGLU -lglut
$ ls
basicshapes  main.cpp  Makefile
$ ./basicshapes &

A new window will appear like this one


[Click to enlarge image]


Write in comments if you face any exception.

Download attached files.
          BasicShape.zip