Greetings all !
I intended to write some code that would load a bitmap image of Earth surface ( of size 1024x512 ) into a texture, and that texture would be displayed in a 1024x512 size FreeGLUT initialized window. I would then use this image to draw simple primitives on it. I am using SOIL image loading library to load the bitmap image. Instead of getting a window with the Earth surface image I get a 1024x512 black background window.
#include "SOIL.h"
#include "GL/glew.h"
#include "GL/glut.h"
GLuint texture;
void display (void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, texture);
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1024.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1024.0,512.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,512.0);
glEnd();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (1024, 512);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glEnable( GL_TEXTURE_2D );
texture = SOIL_load_OGL_texture
(
"earth.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glutMainLoop ();
return 0;
}
What did I do wrong or miss to include in the program ? And also… since you know what I intend to do and use the image for, please let me know if my approach to the problem is not right, or if there is a more optimal way of doing it. Thank you very much.
Kind regards,
T
Note : I have edited my question to include the new code that I tried. I will leave the old code here just to serve as “topic history”.
#include <vector>
#include <fstream>
#include "GL/glew.h" // Include the GLEW header file
#include "GL/glut.h" // Include the GLUT header file
#include <cstdint>
using namespace std;
GLuint texture; //the array for our texture
GLfloat angle = 0.0;
int width = 0;
int height = 0;
short BitsPerPixel = 0;
std::vector<unsigned char> Pixels;
GLuint LoadTexture( const char * filename );
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1024.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1024.0,512.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,512.0);
glEnd();
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1024, 512);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
//Load our texture
GLuint texture;
texture= LoadTexture( "earth.bmp" );
glutMainLoop ();
//Free our texture
FreeTexture( texture );
return 0;
}