0

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.

vallentin
  • 23,478
  • 6
  • 59
  • 81

1 Answers1

3

You're getting an invalid value because you're passing 4 as the internalFormat. Passing 4 represents GL_TRIANGLES which isn't a valid format.

Check the documentation for valid formats.

You probably meant to pass GL_RGBA.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sur->pixels);

The weirdest part about this is i got to work on my old laptop

The reason it worked on your old laptop, is because the driver was being clever.

It still gives me the same error and still renders a pure white square

First of all instead of setting w and h yourself, you can get them from the SDL_Surface.

SDL_Surface *sur = IMG_Load("someimage.jpg");

GLuint txID = 0;
glGenTextures(1, &txID);
glBindTexture(GL_TEXTURE_2D, txID);

int mode = GL_RGB;
if(sur->format->BytesPerPixel == 4)
    mode = GL_RGBA;

glTexImage2D(GL_TEXTURE_2D, 0, mode, sur->w, sur->h, 0, mode, GL_UNSIGNED_BYTE, sur->pixels);

I now noticed that you're targeting OpenGL 4.0. So the invalid value could also be caused by you trying to use the fixed-function pipeline, while having a OpenGL 4.0 context.

While I would recommend staying away from the fixed-function pipeline in general. Try requesting an OpenGL 2.0 context and see if that resolves the problem.

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
vallentin
  • 23,478
  • 6
  • 59
  • 81
  • It still gives me the same error and still renders a pure white square. The weirdest part about this is i got to work on my old laptop a while back and now on this new desktop it wont work. – BlindSide78 Apr 07 '17 at 01:50