Jump to content
  • Advertisement
Sign in to follow this  
cHimura

OpenGL Textures with SDL2 + OpenGL

This topic is 994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everybody,

I'm migrating my engine to SDL2 + OpenGL. Originally, my engine worked with SDL 1.2 + OpenGL.

Now, I have problems with the textures, the engine doesn't render the textures.

Only see a black square.

This is the code to load the image and convert to OpenGL texture:

bool LoadTexture(GLuint* texture, string fileName, GLuint& width, GLuint& height)
{
	SDL_Surface *surface = IMG_Load(fileName.c_str()); // this surface will tell us the details of the image

	GLint nColors;
	GLenum textureFormat;

    if(surface){
        // Check that the image’s width is a power of 2
        if ( (surface->w & (surface->w - 1)) != 0 ) {
            cout << "Warning: image.bmp’s width is not a power of 2\n" << endl;
        }

        // Also check if the height is a power of 2
        if ( (surface->h & (surface->h - 1)) != 0 ) {
            cout << "Warning: image.bmp’s height is not a power of 2\n" << endl;
        }

        //get number of channels in the SDL surface
        nColors = surface->format->BytesPerPixel;

        //contains an alpha channel
        if(nColors == 4)
        {
            if(surface->format->Rmask==0x000000ff)
                textureFormat = GL_RGBA;
            else
                textureFormat = GL_BGRA;
        }
        else if(nColors == 3) //no alpha channel
        {
            if(surface->format->Rmask==0x000000ff)
                textureFormat = GL_RGB;
            else
                textureFormat = GL_BGR;
        }
        else
        {
            cout << "warning: the image is not truecolor…this will break " << endl;
        }

        // Have OpenGL generate a texture object handle for us
        glGenTextures(1, texture);

        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, *texture);

        // Set the texture’s stretching properties
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


        glTexImage2D( GL_TEXTURE_2D, 0, nColors, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);

		width = surface->w;
		height = surface->h;
    }
    else {
		cout << "LoadTexture:: Could not load " << fileName.c_str() << ": " << SDL_GetError() << endl;

		return false;
		//SDL_Quit();
    }

    // Free the SDL_Surface only if it was successfully created
    if(surface) {
        SDL_FreeSurface(surface);
    }

	return true;
}

This is the Vertex Shader

attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;

void main()
{
    gl_Position = a_position;
    v_texCoord = a_texCoord;
}

This is the Fragment Shader

precision mediump float;                            
varying vec2 v_texCoord;                            
uniform sampler2D s_texture;                        
void main()                                         
{                                                   
  gl_FragColor = texture2D( s_texture, v_texCoord );
}

And the function to render

void SquareTextureDemo::Render(){
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

	glBindVertexArray(vao);

    glUseProgram(shaderProgram);
    
    glBindBuffer(GL_ARRAY_BUFFER, m_geometryBuffer);
    
    glVertexAttribPointer(att_position, 3, GL_FLOAT, GL_FALSE, sizeof(PositionTextured), (void*)0);
    glEnableVertexAttribArray(att_position);
    
    glVertexAttribPointer(att_texCoord, 2, GL_FLOAT, GL_FALSE, sizeof(PositionTextured), (void*)12);
    glEnableVertexAttribArray(att_texCoord);
    
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex);
    glUniform1i(uni_samplerTex, 0);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndices);
    glDrawElements(GL_TRIANGLES, (6 * sizeof(GLubyte))/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);

	glBindVertexArray(0);

	OPENGLRENDERER->SwapGLBuffers();
}

I don't know what is wrong, with SDL 1.2 + OpenGL works well but now.

 

Someone can help me??

Regards

Share this post


Link to post
Share on other sites
Advertisement

Hi,

 

Thank you for your response.

 

Yes, I read the guide of migration but it says anything about the textures.

 

I found the error. I sent the nColor variable instead textureFormat i tne glTexImage2D function and now works well.

 

Regards

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!