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

Saturday, June 12, 2010

GL_TEXTURE_2D

GL_TEXTURE_2D

In this part, we continue with a convenient and powerful method for 2D OpenGL, 2D texture.
The strength of using OpenGL 2D texture over glDrawPixels is that linear filtering can be selected.
Moreover, the task is systematic and not tedious.

I, however, do not know if using RGBA is better in terms of performance or not, but I used it in this example.
Thus, we have to modify a way to create a texture image.  The A element can be set to 255, as shown below.

texImage[nColorBytes*(h*m_nWidth + w) + 3] = (GLubyte) 255;

To initialize 2D texture with linear filtering, the following code can be used to make a texImage an OpenGL texture.

void GLTexture2D::initializeGL()
{
    // Basic initialization
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    // Texture initialization
    glGenTextures(1, &textureName);
    glBindTexture(GL_TEXTURE_2D, textureName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    // GL_NEAREST is another choice
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_nWidth, m_nHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
}



To draw a 2D image as a texture, we have to map image coordinates to texture coordinates when we draw GL_QUADS.
In the following function, we map the texture created in initializeGL to rectangular coordinates -1.0 to 1.0.

void GLTexture2D::paintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glBindTexture(GL_TEXTURE_2D, textureName);

    glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0);    glVertex3f(-1.0, -1.0, 0.0);
        glTexCoord2f(0.0, 1.0);    glVertex3f(-1.0,  1.0, 0.0);
        glTexCoord2f(1.0, 1.0);    glVertex3f( 1.0,  1.0, 0.0);
        glTexCoord2f(1.0, 0.0);    glVertex3f( 1.0, -1.0, 0.0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}


===================================

An example project for Eclipse CDT / Qt on Linux is stored at a public folder here.
The project file is valid for Eclipse CDT on Linux with Qt integration (Ubuntu 9.04 Juanty), but the main part should be applicable to most C++ environment.


Pinyo Taeprasartsit
Octobor 2009

Tuesday, May 25, 2010

Eclise + MinGW + FreeGlut (2)

Eclipse CDT + MinGW + FreeGlut (2)

This tutorial starts with building freeglut to create a simple working application with Eclipse and MinGW in both 32-bit and 64-bit versions.  This tutorial refers to a few other tutorials so that readers can jump to the part they want to know conveniently.  Follow the instructions below is relatively a simple task.

  1. The first task is to obtain Eclipse CDT.  This tutorial is based on Eclipse CDT Galieo (SR2 Build ID:2010 02 18-1602).  You can download it from this link, but the most recent version on the Eclipse server may be your preferred choice.

  2. Obtain your preferred MinGW version.  I prepared 32-bit MinGW 4.5.0 and 64-bit MinGW 4.6.0 (2010-April-16 Experimental) packages for you to download.  Note that the include paths within the packages are different.  This is the default settings of the two packages.  Eclipse, however, knows the 32-bit folder paths well, but does not aware of the include paths for the 64-bit version.  Probably, this is because the 64-bit version is still experimental.

  3. Download freeglut.  This tutorial uses freeglut 2.6.0.  If you want to use a pre-compiled binary package, try the following links.
    1. freeglut 32-bit, compiled with 32-bit MinGW 4.5.0 (O2 optimization).  libfreeglut32.a and freeglut32.dll are for dynamic linking, while libfreeglut32_static.a is for static linking.  Header files are included.  I built this package myself.  Feel free to contact me if you have any problem (use the comment in my blog)

    2. freeglut 64-bit, compiled with 64-bit MinGW 4.6.0 (2010-April-16 Experimental) (O2 optimization).  libfreeglut64.a and freeglut64.dll are for dynamic linking, while libfreeglut64_static.a is for static linking.  Header files are included.  I built this package myself.  Feel free to contact me if you have any problem (use the comment in my blog)

    3. From other source

  4. Build freeglut (If you get the binary package from Step 3, ignore this step).  Instructions are available in my post and in another web site.

  5. Putting things together.  If you use MinGW and freeglut 32-bit, this step is quite simple, as Eclipse knows MinGW 32-bit settings well, provided that you save MinGW at C:\MinGW. If you choose a 64-bit path, follow a common instruction for building and running 64-bit applications with Eclipse + MinGW 64-bit in my post.
    1. Put freeglut library in a proper place.
    2. Set compiler and linker flags.  This can be done by going to 'Project->Properties'.  Then, in C/C++ Build, go to 'Settings'.  The flags for static and dynamic links are different.  I assume that you are going to build a C++ application.
      1. For static link:
        In 'GCC C++ Compiler', go to 'Preprocessor' and add a defined symbol FREEGLUT_STATIC.  Also, make sure that your compiler see your freeglut and OpenGL headers.

        In 'MinGW C++ Linker', go to 'Libraries' and add the following libraries (-l): winmm, gid32, opengl32, and freeglut64_static.  Note that the name for freeglut64_static must be consistent with your actual library file name.  Also, it is important to omit the prefix 'lib' and suffix '.a'.  It is crucial, however, that the library file name must come with the prefix and suffix.  Next, add library search path (-L).  This is where you have placed your freeglut library.  For example, in my case, I add a path "C:\MinGW_x64\x86_64-w64-mingw32\lib\freeglut" (with double quotes).

      2. For dynamic link:
        There is no need to add any thing to the preprocessor section, but make sure that your compiler see your freeglut and OpenGL headers.

        Now, go to 'MinGW C++ Linker' and its library section.  Then, do exactly the same thing as discussed for the static link, except that you have to change freeglut64_static to the one for your dynamic link.

    3. Build application.  Try starting with a simple one.  A good example is given from other www.transmissionzero.co.uk.

    4. Run your application.  This seems to be trivial at first, but it can be a bit tricky because you also have to set your MinGW library path.  In addition, if you dynamically link to freeglut, you need to set up its library path.  Whether or not you are building an 64-bit application, please see Section 'Change Run-Time Setting' in my previous post.  It shows you how to set run-time library paths in Eclipse and Windows command to set a library path in case that you are going to run your application outside Eclipse.  If you feel not understand how to do it, you might want to check some figures in my even older post (Figures 6 and 7).

Pinyo Taeprasartsit
(May 2010)

This document can be viewed at my blog and my Google docs.  I welcome questions, comments, and suggestions.  Please leave a message in my blog if you have any.  Thank you.



Build FreeGlut with MinGW

Build FreeGlut with MinGW

(updated Jan 21, 2011: new FreeGlut 64-bit pre-compiled binary is now available. This solves a naming issue caused by an earlier version of MinGW. Thank you James for pointing out the issue.)

FreeGlut 2.6.0 comes with Visual Studio 2008 solution files. If you use Visual Studio, you can probably build the package easily. If you, however, want to use MinGW to build FreeGlut (either or both 32-bit and 64-bit versions), you may not know where to start. Thus, I wrote this document, so that you can build FreeGlut yourself. If you prefer, there are several binary packages ready to use in some web sites. Example of these web sites are http://www.transmissionzero.co.uk/software/freeglut-devel/ and this web site itself.

32-Bit FreeGlut

Use MSYS to build 32-bit freeglut shared library [ref: http://netsuperbrain.com/blog/posts/freeglut-windows-hopengl-hglut/]
  1. gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
  2. gcc -shared -o freeglut32.dll *.o -Wl,--enable-stdcall-fixup,--out-implib,libfreeglut32.a -lopengl32 -lglu32 -lgdi32 -lwinmm

Use MSYS to build 32-bit freeglut static library
  1. gcc -O2 -c -DFREEGLUT_STATIC *.c -I../include (same as above, except the flag is changed from FREEGLUT_EXPORTS to FREEGLUT_STATIC)
  2. ar rcs libfreeglut32_static.a *.o [ref: http://www.adp-gmbh.ch/cpp/gcc/create_lib.html]

If you want to use a pre-compiled binary package, instead of building it yourself, get a pre-compiled binary package here.

64-Bit FreeGlut

This tutorial show a way to build 64-bit FreeGlut by using the Windows command prompt (cmd.exe). Almost every thing is the same as using MSYS for the 32-bit version. In this case, I assume that the current directory is the freeglut source directory and MinGW x64 is at C:\MinGW_x64\bin\.

First of all, set PATH to MinGW in the command prompt:
set PATH=%PATH%;C:\mingw_x64\bin

For a shared library (note that actual prefix gcc-related file names may be different from distribution to distribution)
  1. x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
  2. x86_64-w64-mingw32-gcc -shared -o freeglut64.dll *.o -Wl,--enable-stdcall-fixup,--out-implib,libfreeglut64.a -lopengl32 -lglu32 -lgdi32 -lwinmm

For a static library
  1. x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_STATIC *.c -I../include (same as above, except the flag is changed from FREEGLUT_EXPORTS to FREEGLUT_STATIC)
  2. x86_64-w64-mingw32-ar rcs libfreeglut64_static.a *.o

If you want to use a pre-compiled binary package, instead of building it yourself, get a pre-compiled binary package here. This package was built with sezero's build MinGW GCC 4.4.5 20101001.

Pinyo Taeprasartsit
(May 2010, Jan 2011)

This document can be viewed at my blog and my Google docs. Please leave a message in my blog if you have any questions, comments, and suggestions. Thank you.

Sunday, May 25, 2008

Eclipse CDT + MinGW + FreeGLUT

Eclipse CDT + MinGW + FreeGLUT

[Note: if this document seems out-of-date to you, you might want to see the updated and related documents in the bottom.]

(May 24, 2008)
Today, I tried to use Eclipse CDT + MinGW + FreeGLUT to study OpenGL to build my skill for current research and future career development.  This note is created to show how I could get everything done and perhaps, it can be useful for other people.

I started with installing MinGW version 3.4.5 on Windows XP service pack 2.  During installation, we need to install only g++ to get C/C++ compiler.  We should not include the MinGW make tool if we want to make things easier when we work with source base derived from the Unix world.  In such a case, we should use MSYS (Minimal SYStem), instead [1].  However, if you want to use Eclipse CDT's make system, you have no need to worry about this.  It is interesting to note that MinGW already contains GL header files right from its package.  This makes thing very simple for those who want to quickly set things up.

Side Note: MSYS is a part of MinGW project and can be downloaded from the same download area of MinGW at Sourceforge.  

Next, I installed Eclipse CDT.  It is worth noting that we can have multiple Eclipse copies in a single machine.  We can even have each copy of Eclipse associated with one task (project or solution).  If we do not have any specific reason to share this Eclipse CDT copy with other tasks, we can just download a new copy to get only Eclipse CDT.  This can reduce complexity and confusion in the tool as well.  The figure below shows Eclipse features I used.  You can see that it is very lean because it contains only relevant features for C/C++ development.


Figure 1: Eclipse CDT features

Now, we are close to the last step: getting FreeGLUT up and running with Eclipse CDT and MinGW.  I suggest you obtain a compiled DLL from 'Temp Variable's Blog' [2].  The compiled package also comes with necessary header files, so we don't need to go else where, including the official FreeGLUT web site.  If you need to build Free Glut from scratch or need another version, please see the 'update' note at the bottom of this page.  Next, we are ready to create an Eclipse project with a very simple OpenGL program.  This is just for testing the software configuration.  To avoid confusion, I break the process down to steps:

1. Create an Eclipse project with MinGW compiler

Figure 2: When we choose to create a C or C++ project, we need to choose a compiler / toolchain we want to use.

2. Once the project is created, we probably see the default list of included files related to MinGW in the Eclipse project view.  Check if its main include folder has GL header files.  If not, we probably did something wrong.

Figure 3: Default MinGW include paths, including GL header files

3. We have to place FreeGLUT header files in an appropriate place.  We may choose to put them in the GL folder shown above, in an Eclipse project folder, or in a shared folder (so that you can have only one FreeGLUT header copy in your system).  If we choose the last choice (I chose this one too), we have to add an include directory containing FreeGLUT header files.  To do this, right click on the project item in Eclipse and select Properties.  Then, add an include directory as shown in Figure 4.

Figure 4: 'Adding include directories' can be done in C/C++ Build -> Settings -> GCC C++ Compiler -> Directories

4. Now, we need to refer to the OpenGL layer in Windows, opengl32.  This OpenGL layer will connect to a video driver in a machine.  We don't need to worry if we change a video card.  Things will be the same as long as the new video card driver supports OpenGL commands we employ.  This OpenGL layer is, in fact, a library used by a linker.  Hence, we need to set up the GCC C++ linker.  It can be done from almost the same spot shown in Figure 4.  Just scroll down from the 'GCC C++ Compiler' option to 'MinGW C++ Linker'.  Then, add two libraries: opengl32 and freeglut.  Of course, if your OpenGL project is more complicated, you probably need to add more libraries. We don't need to specify any library search path for opengl32 since they are in a system folder.  For freeglut, however, we need to specify library search path for it if it is not in a system folder.  Note that Eclipse CDT will not search for any library in its project folder.  We need to explicitly specify it for any non-system library search path.

By the way, it is interesting to note that, even if we are working on Windows 64-bit version, we still use opengl32, NOT opengl64.  This may sound odd, but it makes things easier since programs from a 32-bit world can be run seamlessly on Windows 64-bit version without any recompilation.  And for programmers, they don't need to change OpenGL configuration much; Windows will handle it by connecting to an appropriate video driver.

Figure 5: Adding more libraries to MinGW C++ Linker

5. Create the main file with a very simple usage of OpenGL.  Once again, I suggest you use the same example as in 'Temp Variable's Blog'.  The file seems to be in a book by E. Angel, Interactive Computer Graphics, A Top-Down Approach with OpenGL, Third Edition, Addison-Wesley Longman, 2003 [3].  The direct link to the file is http://www.box.net/shared/u2o7k31ogc

6. Build a project (press Ctrl + B or click the hammer button).

7. Try running your application.  You may find that there is nothing happens.  Your program just terminated.  When you try to debug it, you will see only an error message that "there is an error occurred", but no other clue.  This problem arises from a missing DLL.  It is important to know that the executable file we get will not try to search for libraries specified in the Eclipse settings.  Thus, we need to put the DLL in the executable folder  (or in a system one if you prefer).  Make sure that you have DLL copies in both Debug and Release folders.  Anyway, if you encounter 'silent errors' like this in Eclipse CDT, I recommend you run an executable file from the Windows command prompt; Windows may give you more information than Eclipse CDT.

Note: if you are new to Eclipse CDT, you may wonder how to run a program in a Debug or Release mode as you wish.  From the beginning, it will run solely in a Debug mode, even when you set the active configuration to Release.  To change the default running mode, you have to do it in the 'Run Dialog'.  It can be found from the menu bar on the top of Eclipse as shown in Figure 6.  When you go there, you may find two configurations you can choose, but still have no idea which one is Release (I had this problem before).  You can differentiate it by its location.  From the main tab, you can see a project name and 'C/C++ Application'.  The latter will tell you about an application location as shown in Figure 7.  Notice the release folder name before an executable file name.


Figure 6: 'Run Dialog' can be accessed from the menu bar.  Note: click on the arrow beside the green Run button, not the Run button itself.
Figure 7: Main tap in the 'Run Dialog' can show us the location of an executable.  We can use the location to figure out what mode it is in

References:

1. Eclipse CDT: Before You Begin, http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.user/concepts/cdt_c_before_you_begin.htm
2. Temp Variable's Blog: Installing freeglut on Visual Studio 2008, http://tempvariable.blogspot.com/2008/02/installing-freeglut-on-visual-studio.html
3. E. Angel, Interactive Computer Graphics, A Top-Down Approach with OpenGL, Third Edition, Addison-Wesley Longman, 2003. (a portion of source code in the book is obtained from http://www.box.net/shared/u2o7k31ogc)


Pinyo Taeprasartsit
(May 2008, May 2010, Oct 2011)

This document can be viewed at my blog and my Google docs.  Questions, comments, and suggestions are welcomed.  Please leave a message in my blog if you have any.  Thank you.

=======

Update: [May 2010] I now have a few other documents that closely related to this one.  They discuss how to compile FreeGlut for both 32 and 64-bit applications and using more recent versions of MinGW, including the 64-bit version.  Check them out if you are interested:
  1. Eclipse CDT with MinGW x64.  If you want to use MinGW 64-bit with Eclipse, this is a document for you.
  2. Build FreeGlut 2.6.0 with MinGW in both 32 and 64 bit.  Pre-compiled binary packages are also available.  This package is newer than the one discussed in this document and come with both static and dynamic library.  Very handy if you need one.  
  3. Eclipse CDT + MinGW + FreeGlut (2).  This adds information about 64-bit version of MinGW and FreeGlut.  It shows you how to use both static and dynamic library versions of FreeGlut.  Also, if you need to easily download MinGW 4.5.0 32-bit without bothering with wget and bash as mentioned in MinGW web site.  The package provided in this document will help you with that.  This should be helpful until MinGW comes with a more polished installer.

This document can be viewed at my blog and my Google docs.  Questions, comments, and suggestions are welcomed.  Please leave a message in my blog if you have any.  Thank you.