Jump to content

  • Log In with Google      Sign In   
  • Create Account

Need help using SOIL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 crk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 November 2010 - 08:21 PM

Hi everyone.
I am trying to display a texture using SOIL (Simple OpenGL image library).
Please find my entire code below. I am unable to understand why I am not able to see any image on the screen. The code below only shows a red screen background with a black square image in the center. It is supposed to show a red image background with an image in the center.

I am able to build and run the program successfully without any errors.

Any help will be greatly appreciated!



#include <stdio.h>
#include <GL/glu.h>
#include <GL/glut.h> // Include the GLUT header file
#include <soil/SOIL.h>

GLuint usertex;

void keyOperations (void) {
}

void renderPrimitive (void) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,usertex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glBegin(GL_QUADS); // Start drawing a quad primitive

glVertex3f(-0.1f, -0.1f, 0.0f); // The bottom left corner
glVertex3f(-0.1f, 0.1f, 0.0f); // The top left corner
glVertex3f(0.1f, 0.1f, 0.0f); // The top right corner
glVertex3f(0.1f, -0.1f, 0.0f); // The bottom right corner

glEnd();
glDisable(GL_TEXTURE_2D);
}

void display (void) {
keyOperations();

glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glDisable(GL_BLEND);
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations

glTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene, otherwise we won't see the primitive

renderPrimitive(); // Render the primitive

glFlush(); // Flush the OpenGL buffers to the window
}

void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}

void keyPressed (unsigned char key, int x, int y) {
}

void keyUp (unsigned char key, int x, int y) {
}

int main (int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("You’re first OpenGL Window"); // Set the title for the window

glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering

glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping

glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events


/* load an image file directly as a new OpenGL texture */
usertex = SOIL_load_OGL_texture
(
"resources/user.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
);

/* check for an error during the load process */
if( 0 == usertex )
{
printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
} else {
printf("SOIL load success for user block");
}


glutMainLoop(); // Enter GLUT's main loop
return 0;
}


P.S.: All the code except the SOIL part is from swiftless tutorials (http://www.swiftless.com/tutorials/opengl/square.html)

Sponsor:

#2 ccsdu2004   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 November 2010 - 08:46 PM

Ham.
I think Soil is not good.
you can use FreeImage,Devil or CImage

www.sooftmoon.com

#3 crk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 November 2010 - 10:44 PM

@ccsdu..
I previously tried to use DevIL. But I was unable to get that to work as well. I kept getting an error code 1290 - Unable to load file.


int loadTextures()
{
ILuint texid; /* ILuint is a 32bit unsigned integer.
Variable texid will be used to store image name. */
ILboolean success; /* ILboolean is type similar to GLboolean and can equal GL_FALSE (0) or GL_TRUE (1)
it can have different value (because it's just typedef of unsigned char), but this sould be
avoided.
Variable success will be used to determine if some function returned success or failure. */
GLuint image;

ilGenImages(1, &texid); /* Generation of one image name */
ilBindImage(texid); /* Binding of image name */
//success = ilutGLLoadImage("user.PNG");
success = ilLoad(IL_PNG,"resources/user.png"); /* Load the image */
if (success!=IL_FALSE) /* If no error occured: */
{
success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); /* Convert every colour component into
unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA */
if (success==IL_FALSE)
{
/* Error occured */
return -1;
}
glGenTextures(1, &image); /* Texture name generation */
glBindTexture(GL_TEXTURE_2D, image); /* Binding of texture name */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear
interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear
interpolation for minifying filter */
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
ilGetData()); /* Texture specification */
}
else
{
/* Error occured */
ILenum err = ilGetError() ;
printf( "\nError Code:%d\n", err );
printf( "Error String: %s\n", ilGetString( err ) );
return -1;
}
ilDeleteImages(1, &texid); /* Because we have already copied image data into texture data
we can release memory used by image. */
}



The code keeps printing the "Error Code: 1290" (IL_COULD_NOT_OPEN_FILE) statement. I have debugged it, and have observed that the ilLoad() keeps returning 0. I have tried using some other functions to load the image (ilLoadImage and ilutGLLoadImage) but one of them was throwing up a 1291 (IL_INVALID_EXTENSION) and I don't remember what the other one was throwing.

I have repeatedly checked the paths of the files. I am accessing some other text files present in the resources folder, and they are all being accessed just fine.

Any pointers on what can make the above code work are equally welcome!

Edit: I forgot to mention this. I don't know if this will have any effect on what I am doing. I am invoking the loadTextures() just before I call glutMainLoop().

#4 ccsdu2004   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 November 2010 - 11:28 PM

You can use FreeImage
If there is any question,
I would like to help you

#5 fcoelho   Members   -  Reputation: 389

Like
0Likes
Like

Posted 03 November 2010 - 11:44 PM


void renderPrimitive (void) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,usertex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glBegin(GL_QUADS); // Start drawing a quad primitive

//texture coordinates?
glVertex3f(-0.1f, -0.1f, 0.0f); // The bottom left corner
glVertex3f(-0.1f, 0.1f, 0.0f); // The top left corner
glVertex3f(0.1f, 0.1f, 0.0f); // The top right corner
glVertex3f(0.1f, -0.1f, 0.0f); // The bottom right corner

glEnd();
glDisable(GL_TEXTURE_2D);
}

You haven't defined any texture coordinates for the quad you're rendering. Take a look here, at the "Using the Texture" section for some ideas.


#6 crk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 04 November 2010 - 01:03 AM

@fcoelho
Wow! Thank you so much! That got the sample program working. Now I need to incorporate it into my original program. You did not set the hyperlink in your post. I referred to http://www.gamedev.net/reference/articles/article947.asp.

Thanks a lot again!

EDIT:
Solution for noobs in OpenGL like me:
Replace the code between glBegin and glEnd in renderPrimitive with this:

float blocksize = 0.125f; // you can specify any dimensions you want here
glTexCoord2d(0,0);
glVertex2f(-blocksize, -blocksize); // The bottom left corner
glTexCoord2d(0,1);
glVertex2f(-blocksize, blocksize); // The top left corner
glTexCoord2d(1,1);
glVertex2f(blocksize, blocksize); // The top right corner
glTexCoord2d(1,0);
glVertex2f(blocksize, -blocksize); // The bottom right corner


You can also use glTexCoord2f(). I wanted the entire square to be filled with my texture image and hence I specified the coordinates as (0,0),(0,1),(1,1) and (1,0)

[Edited by - crk on November 7, 2010 1:03:57 AM]

#7 crk   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 November 2010 - 07:14 PM

@fcoelho
The textures are working just fine now, but there is a greyish border around the textures. I have tried setting GL_TEXTURE_BORDER_COLOR but it does not seem to work.


float bordercolor[] = { 1.0f, 0.0f, 0.0f };

glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bordercolor);


The texture images have 32X32 dimensions.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS