I'm using glew 1.1.0, SDL_image 2.0 and mingw(Code::Blocks, Windows). I'm trying to import an .png file by using SDL_Image and to make it a texture and display to the screen i use OpenGL. When i run the program it displays a pure white square, it has the same width and height of the image im trying to import, but it's pure white. i looked over the code for about 30 minutes straight and the code looks fine, and it never gave me any errors execpt "Error initializing OpenGL! invalid value".
my main.cpp:
#include <GL/glew.h>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <stdio.h>
#include <SDL_image.h>
#include <SDL_opengl.h>
using namespace std;
void init();
void Render();
bool ex;
bool akey;
SDL_Event e;
SDL_Window *win = NULL;
GLuint txID;
int x, y, w, h;
int main(int argc, char* args[])
{
init();
while(!ex)
{
while(SDL_PollEvent(&e) != 0)
{
if(e.type == SDL_QUIT)
{
ex = true;
}
Render();
}
}
}
void init()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
win = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
SDL_GLContext cont = SDL_GL_CreateContext(win);
SDL_GL_SetSwapInterval(1);
glewInit();
glClear( GL_COLOR_BUFFER_BIT );
glViewport( 0.f, 0.f, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 640, 480, 0.0, 1.0, -1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClearColor( 0.f, 0.f, 0.f, 1.f );
glEnable( GL_TEXTURE_2D );
glEnable(GL_BLEND);
SDL_Surface *sur = IMG_Load("ef.png");
w = 40;
h = 60;
glGenTextures( 1, &txID );
glBindTexture(GL_TEXTURE_2D, txID);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sur->pixels);
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
}
//Set texture parameters
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
//Unbind texture
glBindTexture(GL_TEXTURE_2D, NULL);
}
void Render()
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(20, 20, 0);
glBindTexture(GL_TEXTURE_2D, txID);
glBegin( GL_QUADS );
glTexCoord2f( 0.f, 0.f ); glVertex2f(0.f, 0.f);
glTexCoord2f( 1.f, 0.f ); glVertex2f(w, 0.f);
glTexCoord2f( 1.f, 1.f ); glVertex2f(w, h);
glTexCoord2f( 0.f, 1.f ); glVertex2f(0.f, h);
glEnd();
SDL_GL_SwapWindow(win);
}
This is the image I'm trying to load:
The program and the error, as you can see the white box is where the image is supposed to be.