# Texture Not Rendering in C++ SDL OpenGL(glut)

The whole drawing function:

///HERE EVERYTHING IS BEING RENDERED
void Main::display(){

int startTime = GetTickCount();

///BACKGROUND COLOR
glClearColor(0.3f, 0.3f, 2.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

///LOAD THE IDENTITY MATRIX FOR THE DRAWING

///SO EVERYTHING HAS THE CORRECT SIZE
glTranslatef(0.0f, 0.0f, -5.0f);

///CAMERA MOVEMENT
glTranslatef(cameraX, 0.0f, 0.0f);

///THE SUN
glColor3f(1.0f,1.0f,0.0f);

glVertex2f(-3.0f, 2.0f); // The bottom left corner
glVertex2f(-3.0f, 2.5f); // The top left corner
glVertex2f(-2.5f, 2.5f); // The top right corner
glVertex2f(-2.5f, 2.0f); // The bottom right corner
glEnd();

///THE GROUND
glColor3f(0.5f,2.0f,0.5f);

glVertex2f(-20.0f,-4.0f);
glVertex2f(-20.0f,-1.0f);
glVertex2f(130.0f,-1.0f);
glVertex2f(130.0f,-4.0f);

glColor3f(0.0f,1.0f,0.0f);

for(float i = 0.0f ; i < 100.0f ; i += 5.0f){
glVertex2f(i,-1.0f);
glVertex2f(i,ranFloat[static_cast<int>(i-4.0f)]);
glVertex2f(i+1.0f,ranFloat[static_cast<int>(i-4.0f)]);
glVertex2f(i+1.0f,-1.0f);
}
glEnd();

///THE GRASS
glColor3f(0.0f,0.5f,0.0f);

glVertex2f(-20.0f,-1.2f);
glVertex2f(-20.0f,-1.0f);
glVertex2f(130.0f,-1.0f);
glVertex2f(130.0f,-1.2f);
glEnd();

///THE PLAYER
glTranslatef(playerX,playerY,0.0f);
glColor3f(1.0f,0.0f,0.0f);

glVertex2f(-3.0f,-1.0f);
glVertex2f(-3.0f,0.0f);
glVertex2f(-2.8f,0.0f);
glVertex2f(-2.8f,-1.0f);
glEnd();

///TEXTUREEEEEEEEEEEEEEEEEEEEEE
glBindTexture(GL_TEXTURE_2D,texture);
glEnable(GL_TEXTURE_2D);

int X = 0;
int Y = 0;
int Width = 1;
int Height = 1;

glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
glTexCoord2f(0, 1); glVertex3f(X + Width, Y, 0);
glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(1, 0); glVertex3f(X, Y + Height, 0);
glEnd();
glDisable(GL_TEXTURE_2D);

///CLOUDS
glColor3f(1.0f,1.0f,1.0f);
for(float i = 0.0f ; i < 100.0f ; i += 5.0f){
glVertex2f(i,3.0f);
glVertex2f(i,4.0f);
glVertex2f(i+4.0f,3.0f);
glVertex2f(i+4.0f,4.0f);
}
glEnd();

if(Input::flying){
Input::returnFly(0.007f);
}

elapsedMS = GetTickCount() - startTime; // Time since start of loop
//std::cout << "FPS: " << elapsedMS << std::endl;

///THE ACTUAL MOVEMENT
playerY += playerFly;//*elapsedMS;
playerX += cameraSpeed*-1;//*elapsedMS;
cameraX += cameraSpeed;//*elapsedMS;

///SWAP THE BUFFERS FOR SMOOTH RENDERING
glutSwapBuffers();
}


The rest of my code has nothing to do with the texture loading. The loading is not the problem, because I didn't get any errors.

• why on earth are you using sdl and glut togther? – The Light Spark Nov 3 '15 at 16:29
• SDL for loading the image and glut for opengl, to draw the texture(SDL image). – OpenGLmaster1992 Nov 3 '15 at 16:30
• SDL has both opengl context creation and texture loading functionality. You should not have required two frameworks that do the same things, (and mess up each other's work?) – The Light Spark Nov 3 '15 at 16:37
• I actually prefer glut, but I don't know if I can load images with glut. – OpenGLmaster1992 Nov 3 '15 at 16:40

Are you checking that your SDL_Surface* is valid? Are you sure that your "Mode" is RGB/RGBA and not BGRA/BGR?

I think the problem is that you need to pass the Surface->format->BytesPerPixel as the third argument to glTexImage2D instead of Mode.

Also, I would set the Min/Mag filter before calling glTexImage2D.

Like this:

void Main::loadStuff() {
GLuint texture = 0;

if (!Surface) {
// error, report?
return;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

int Mode = GL_RGB;

if(Surface->format->BytesPerPixel == 4) {
Mode = GL_RGBA;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, Surface->format->BytesPerPixel, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

SDL_FreeSurface(Surface);
}


Bonus points if you want to work in std::unique_ptr for you surface.

• Can't edit, but, also noticed that GLuint texture is local to void Main::loadStuff() - that could certainly be part of the problem, too (when you try to rebind in "The drawing of the texture:" portion, where does that texture come from? – morandy Nov 3 '15 at 19:27
• It is a static variable I defined in the Main header file – OpenGLmaster1992 Nov 3 '15 at 19:44
• Ah! The local one is taking over, that's probably why. Get rid of the one at the top of void Main::loadStuff() then, it's taking over. – morandy Nov 3 '15 at 19:53
• But how can I draw now. I need the texture variable but now it is only defined in de loadStuff function, and I need in in the display function. – OpenGLmaster1992 Nov 3 '15 at 19:56
• Sorry, I wasn't clear, get rid of the one local to loadStuff and have loadStuff set your static variable in the Main header file. – morandy Nov 3 '15 at 20:05