Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 24, 2012, 01:18 AM   #1
quanganhct
macrumors member
 
Join Date: Dec 2010
[OpenGL]SOIL do not load texture properly

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 is offline   0 Reply With Quote
Old Oct 24, 2012, 04:41 AM   #2
ncl
macrumors member
 
Join Date: Aug 2008
Where do you assign the texture coordinates ?
ncl is offline   0 Reply With Quote
Old Oct 24, 2012, 09:52 PM   #3
quanganhct
Thread Starter
macrumors member
 
Join Date: Dec 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.
quanganhct is offline   0 Reply With Quote
Old Oct 24, 2012, 10:12 PM   #4
quanganhct
Thread Starter
macrumors member
 
Join Date: Dec 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.
PHP Code:
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 ?
quanganhct is offline   0 Reply With Quote
Old Oct 24, 2012, 10:40 PM   #5
quanganhct
Thread Starter
macrumors member
 
Join Date: Dec 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 ?
quanganhct is offline   0 Reply With Quote
Old Oct 25, 2012, 03:53 AM   #6
ncl
macrumors member
 
Join Date: Aug 2008
Quote:
Originally Posted by quanganhct View Post
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.
Quote:
Originally Posted by quanganhct View Post
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
ncl is offline   0 Reply With Quote
Old Oct 28, 2012, 11:35 PM   #7
quanganhct
Thread Starter
macrumors member
 
Join Date: Dec 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.
quanganhct is offline   0 Reply With Quote
Old Oct 29, 2012, 12:29 PM   #8
szymczyk
macrumors regular
 
Join Date: Mar 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.

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.
__________________
Mark Szymczyk
Author, Xcode Tools Sensei
http://www.meandmark.com

Last edited by szymczyk; Oct 29, 2012 at 01:19 PM.
szymczyk is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
OS X: Torchlight Texture Mod ? Washac Mac and PC Games 0 Jul 15, 2013 03:42 AM
Resolved: CIImage with OpenGL FBO texture Bunkum Mac Programming 0 Aug 25, 2012 11:09 PM
Photoshop Texture Help bpletcher Design and Graphics 4 Aug 3, 2012 08:56 PM
Weird texture on RMBP unibody? Jamesesesesess MacBook Pro 11 Jul 23, 2012 04:04 AM

Forum Jump

All times are GMT -5. The time now is 09:31 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC