Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
Hi, I have problem loading texture from an image using SOIL and OpenGL.

Here is the code :

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, 
								   SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);
}

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;
	}
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	
	glPushMatrix();
	glMatrixMode(GL_MODELVIEW);
	
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, tex_2d);

	glTranslatef(initX, initY, 0.0f);
	//glRotatef(degree, 0.0f, 0.0f, 1.0f);
	
	degree+=rot;
	
	glBegin(GL_QUADS);
	glVertex2f(-70.0f, -70.0f);
	glVertex2f(70.0f, -70.0f);
	glVertex2f(70.0f, 70.0f);
	glVertex2f(-70.0f, 70.0f);
	glEnd();
	
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glutSwapBuffers();
}

void reshape(int width, int height){
	glViewport(0, 0, width, height);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, width, 0, height);
	glMatrixMode(GL_MODELVIEW);
}

void idle(void){
	glutPostRedisplay();
}

void keyPress(unsigned char key, int x, int y){
	switch (key) {
		case 'a':
			initX -= transX;
			if (initX < 0.0f) {
				initX = 0.0f;
			}
			break;
		case 'd':
			initX += transX;
			if (initX > glutGet(GLUT_WINDOW_WIDTH)) {
				initX = (float) glutGet(GLUT_WINDOW_WIDTH);
			}
			break;
		case 'w':
			initY += transY;
			if (initY > glutGet(GLUT_WINDOW_HEIGHT)) {
				initY = (float) glutGet(GLUT_WINDOW_HEIGHT);
			}
			break;
		case 's':
			initY -= transY;
			if (initY < 0.0f) {
				initY = 0.0f;
			}
			break;
		case 'r':
			rot = -rot;
			break;
			
			
			
		default:
			break;
	}
	
}

int main(int argc, char** argv){
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(kWindowWidth, kWindowHeight);	
	glutCreateWindow("GLUT Program");
	
	init();
	
	glutKeyboardFunc(keyPress);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(idle);	

	glutMainLoop();
	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
 

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
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. :)
 

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
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.
PHP:
glTexCoord2f(0.0f, 0.0f); glVertex2f(-70.0f, -70.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(70.0f, -70.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(70.0f, 70.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-70.0f, 70.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 ?
 

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
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 ?
 

ncl

macrumors member
Aug 16, 2008
58
0
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 ?
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.
How could I fix 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 :)
 

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
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.
 

szymczyk

macrumors regular
Mar 5, 2006
187
17
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.

UPDATE

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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.