• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
crk

Need help using SOIL

6 posts in this topic

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)
0

Share this post


Link to post
Share on other sites
@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().
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites
@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]
0

Share this post


Link to post
Share on other sites
@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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0