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 redglDisable(GL_BLEND);glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)glLoadIdentity(); // Load the Identity Matrix to reset our drawing locationsglTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene, otherwise we won't see the primitiverenderPrimitive(); // Render the primitiveglFlush(); // 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 windowglMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewedglLoadIdentity(); // 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 planesglMatrixMode(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 GLUTglutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)glutInitWindowSize (500, 500); // Set the width and height of the windowglutInitWindowPosition (100, 100); // Set the position of the windowglutCreateWindow ("You’re first OpenGL Window"); // Set the title for the windowglutDisplayFunc(display); // Tell GLUT to use the method "display" for renderingglutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshapingglutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key pressesglutKeyboardUpFunc(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 loopreturn 0;}
P.S.: All the code except the SOIL part is from swiftless tutorials (http://www.swiftless.com/tutorials/opengl/square.html)