[OpenGL]SOIL do not load texture properly

Discussion in 'Mac Programming' started by quanganhct, Oct 23, 2012.

  1. quanganhct macrumors member

    Dec 29, 2010
    Hi, I have problem loading texture from an image using SOIL and OpenGL.

    Here is the code :

    #include <stdlib.h>
    #include <GLUT/GLUT.h>
    #include "SOIL.h"
    int lastFrameTime = 0;
    float degree = 0.0f;
    float boxX = 0.0f;
    float initX = 200.0f;
    float initY = 200.0f;
    float transX = 3.0f;
    float transY = 3.0f;
    float rot = 0.1f;
    int kWindowWidth = 640;
    int kWindowHeight = 420;
    GLuint tex_2d;
    void init(void){
    	tex_2d = SOIL_load_OGL_texture("test.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, 
    void display(void){
    	if (lastFrameTime == 0) {
    		lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
    	int now = glutGet(GLUT_ELAPSED_TIME);
    	int elapsedMiliseconds = now - lastFrameTime;
    	float elapsedTime = elapsedMiliseconds / 1000.0f;
    	lastFrameTime = now;
    	int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
    	boxX += 100.0f * elapsedTime;
    	if (boxX > windowWidth) {
    		boxX -= windowWidth;
    	glBindTexture(GL_TEXTURE_2D, tex_2d);
    	glTranslatef(initX, initY, 0.0f);
    	//glRotatef(degree, 0.0f, 0.0f, 1.0f);
    	glVertex2f(-70.0f, -70.0f);
    	glVertex2f(70.0f, -70.0f);
    	glVertex2f(70.0f, 70.0f);
    	glVertex2f(-70.0f, 70.0f);
    void reshape(int width, int height){
    	glViewport(0, 0, width, height);
    	gluOrtho2D(0, width, 0, height);
    void idle(void){
    void keyPress(unsigned char key, int x, int y){
    	switch (key) {
    		case 'a':
    			initX -= transX;
    			if (initX < 0.0f) {
    				initX = 0.0f;
    		case 'd':
    			initX += transX;
    			if (initX > glutGet(GLUT_WINDOW_WIDTH)) {
    				initX = (float) glutGet(GLUT_WINDOW_WIDTH);
    		case 'w':
    			initY += transY;
    			if (initY > glutGet(GLUT_WINDOW_HEIGHT)) {
    				initY = (float) glutGet(GLUT_WINDOW_HEIGHT);
    		case 's':
    			initY -= transY;
    			if (initY < 0.0f) {
    				initY = 0.0f;
    		case 'r':
    			rot = -rot;
    int main(int argc, char** argv){
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    	glutInitWindowSize(kWindowWidth, kWindowHeight);	
    	glutCreateWindow("GLUT Program");
    	return EXIT_SUCCESS;
    the file "test.png" is attached.

    I 'm sure that the path is right, as I changed "test.png" to "test1.png", it loaded a white square, but with "test.png", a dark-brown square was loaded.


    can't upload image. wonder why
  2. ncl macrumors member

    Aug 16, 2008
    Where do you assign the texture coordinates ?
  3. quanganhct thread starter macrumors member

    Dec 29, 2010
    Er did you mean binding texture ? I did the binding in display(),
    glBindTexture(GL_TEXTURE_2D, tex_2d);

    Sorry if I don't understand the question. I study this myself and not even have a proper book, just wander around, found the code and bang it in. :)
  4. quanganhct thread starter macrumors member

    Dec 29, 2010
    Ok found it, thanks for pointing it out :) Actually there's some works I didn't finish. That is texture mapping.

    This is the new code, and it work.
    glTexCoord2f(0.0f0.0f); glVertex2f(-70.0f, -70.0f);
    glTexCoord2f(1.0f0.0f); glVertex2f(70.0f, -70.0f);
    glTexCoord2f(1.0f1.0f); glVertex2f(70.0f70.0f);
    glTexCoord2f(0.0f1.0f); glVertex2f(-70.0f70.0f);
    Now I do feel I need a book, in case that I miss something and I didn't even know that I miss. That's dangerous. Anybody commend any ebook ?
  5. quanganhct thread starter macrumors member

    Dec 29, 2010
    I have found another problem. When execute the program by Xcode, everything is alright, but when I run the executable file (locate in Project/build/Debug/file.app), it run but it's just a plain white square, seem like it didn't know about the picture. How could I fix this ?
  6. ncl macrumors member

    Aug 16, 2008
    Well, if you are new to OpenGL and you have Lion (or ML), you may want to start directly with OpenGL 3.2.
    As for a book, I don't really know. It has been quite a few years since I did any OpenGL work. The OpenGL SuperBible seems current enough. Otherwise, you will have to look for tutorials, like this one: http://www.arcsynthesis.org/gltut/
    Maybe someone else can help you better with this.
    You may want to lookup the notion of working directory. Then check what is the working directory of your application when it is started from Xcode and when it is started from the Finder. There are several threads about this here on MacRumors.

    Also, maybe you should change the title of your thread, considering your problems have nothing to do with SOIL. It may attract more people here :)
  7. quanganhct thread starter macrumors member

    Dec 29, 2010
    Thanks for your reply. Here is what I found out :

    when trying to use getcwd(), I knew Xcode's working directory is Debug, and when I try to run "file.app/Contents/MacOS/file" by double-click, its working directory is /Users/myname/

    By adding the image to those place, I could have my app work right. But now is the problem, I guess I must use a full adresse in this case, not a relative one. But how do I handle this, for ex if I run it on another machine, in a whatever directory ? How could I take avantage of the directory "Contents" in file.app ? For ex I could add an images folder.
  8. szymczyk, Oct 29, 2012
    Last edited: Oct 29, 2012

    szymczyk macrumors regular

    Mar 5, 2006
    Your image files are inside your application's bundle. You must use either Cocoa's NSBundle class or Core Foundation's CFBundle data structure and functions to read the image files from the app bundle. Since you don't appear to be using Objective-C, I recommend using CFBundle. More information on bundles can be found in the following article:

    Bundles article

    The article was written before Xcode 4 came out but the general concepts still apply.


    You may also want to look into a different cross-platform framework than GLUT, such as SDL, SFML, or Allegro. I don't know about SFML or Allegro, but in SDL, you can modify the code to change the working directory to your app bundle's Resources folder, which makes texture loading much easier. You may find that easier than writing Mac-specific code to load a file from the app bundle, especially if you're writing cross-platform code.

Share This Page