CS409/CS809 Tutorial 12: Loading PNG Textures

 

Note: PNG loading only works correctly on Windows; not on Mac or Linux.  I didn't write the PNG-loading code, and I don't know how to fix it.

Setting up an OpenGL Project with the Obj Library of Files

1.      Start Microsoft Visual Studio 2015.  Select File / New / Project….  Under the list on the left, choose Installed / Templates / Visual C++. Then choose a Win32 Console Application.  Change the name to Tutorial12 or some suitable name and change the location to a hard drive located on the computer (typically C or D).  You will need to remember this location for steps 3 and 5.  Make sure the “Create folder for project” checkbox is not checked and that the “Add to source control” is not checked.  Click OK.

·         If the Chose Location window keeps popping up again, you aren’t allowed to save files at the location you chose.  Select a different location, such as the desktop.

·         Do not put your program on one of the external drives like H.  The connection is slow, and your program may not compile correctly.

2.      Click Next> or Application Settings (they go to the same place).  Check the “Empty Project” box.  Make sure the “Security development lifecycle check” is not checked.  Click Finish.

3.      Go to http://www2.cs.uregina.ca/~anima/409/Terms/201810/Tutorials/index.html and download the Tutorial12.zip and ObjLibrary.zip files to your project directory.

4.      Unzip the 2 zip files.  You will have a folder called ObjLibrary and 14 other files.

5.      In Visual C++, go to Project/Add Existing Item… and select main12.cpp (and holding [CTRL], continue to select), Sleep.h, Sleep.cpp, and GetGlut.h.

6.      Ensure that Solution Explorer is visible (View / Solution Explorer). In the Solution Explorer, select Tutorial12 (the project name) and right click it.  Choose Add / New Filter.  A new folder icon should appear in the Solution Explorer.  Name it ObjLibrary.  (This creates an imaginary folder in the project, but it would not create a Windows folder on the computer).

7.      In the Solution Explorer, right click on the ObjLibrary folder icon and select Add / Existing item.  Go into the ObjLibrary folder on Windows and select all files.  The files should appear in the Solution Explorer inside the ObjLibrary folder.  You should get 26 files.

8.      Compile and run the original program.  You should see 2 windows: a text console and a graphical window labeled “Loading PNG Textures”.  There is a spiky orange ball in the middle of this second window.  Exit the windows.  Closing the text window will close the graphical window, and usually vice versa.

·         You may see a pop-up window that says "This project is out of date:".  If so, click "Yes".  This tells Visual Studio to compile your program before trying to run it.

Adding PNG Textures

9.      Go to http://www2.cs.uregina.ca/~anima/409/Terms/201810/Tutorials/index.html and download the ObjLibraryPng.zip file to your project directory.

10.  Unzip the zip file.  You should get 2 more folders named zlib and png, and 2 files named glpng.c and glpng.h. These should be on the same folder level as the ObjLibrary folder.

·         The zlib folder contains the files you need to open ZIP files.

·         The png folder contains the files you need to load PNG images (which are modified ZIP files internally).

·         The glpng.c and glpng.h files handle getting the PNG image into OpenGL.

11.  Add 2 more Visual Studio "folders" (filters) and name them zlib and png.  Add the contents of the corresponding real folders to them.  Also add the glpng.c and glpng.h files to your project.

12.  Compile your project.  You will get 7 warnings that functions (sprintf, getenv, sscanf, and fopen) are unsafe, which you can ignore.  Everything else should compile correctly on Windows.  On Mac or Linux, the glpng.c file will not compile.

13.  Open the ObjLibrary/ObjSettings.h file and uncomment the line that says:
#define OBJ_LIBRARY_LOAD_PNG_TEXTURES
You will have to remove read-only-ness from the file. You can do this by hand, or Visual Studio will offer to do it for you when you try to save the file.

14.  Recompile everything.  In Visual Studio, go to Build/Rebuild Solution.  If you don't do this, your program will not compile.

15.  Test your program.  It should run the same as before.

16.  Change the initDisplay function to load the OBJ model named Spiky2.obj instead of Spiky.obj:
  spiky.load("Spiky2.obj");

17.  Compile and run the program.  The spiky ball should now be black with yellow spikes and yellow lines between them.