Thread: OpenGL texture rendering using SDL2 surfaces

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    1

    Question OpenGL texture rendering using SDL2 surfaces

    Hi there, I'm relatively new to SDL2/OpenGL stuff, and I wanted to render
    a textured quad. It seems like you can use SDL2 surfaces to create these
    textures so that's what I went out and tried. In the end, I did not get any
    errors, but the quad just appeared white, and had no texture. I was hoping
    that any of you may have a clue to whats going wrong.

    Code:
    #include "graphics.h"
    
    SDL_Window* graphics_window = NULL;
    SDL_GLContext graphics_context;
    
    
    
    
    // Textures
    typedef struct graphics_texture {
        char* image;
        GLuint id;
        SDL_Surface* surface;
        int mode;
    }graphics_texture;
    
    
    graphics_texture noise = {"../media/noise.bmp", 0};
    
    
    // Texture functions
    int graphics_texturecreate(graphics_texture* texture) {
        texture->surface = SDL_LoadBMP(texture->image);
        if(texture->surface == NULL) {return 1;}
    
    
        glGenTextures(1, &texture->id);
        glBindTexture(GL_TEXTURE_2D, texture->id);
    
    
        texture->mode = GL_RGB;
    
    
        glTexImage2D(GL_TEXTURE_2D, 0, texture->mode, texture->surface->w, texture->surface->h, 0, texture->mode, GL_UNSIGNED_BYTE, texture->surface->pixels);
        
        SDL_FreeSurface(texture->surface);
        
        return 0;
    }
    
    
    int graphics_init(void) {
        // Initialize SDL2
        if(SDL_Init(SDL_INIT_VIDEO)) {
            printf("Could not initialize SDL2: %s\n", SDL_GetError());
            return 1;
        }
    
    
        // Create SDL2 window
        graphics_window = SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, GRAPHICS_WIDTH, GRAPHICS_HEIGHT, SDL_WINDOW_OPENGL);
        if(graphics_window == NULL) {
            printf("Could not create window: %s\n", SDL_GetError());
            return 1;
        }
        
        // Initialize GL context
        graphics_context = SDL_GL_CreateContext(graphics_window);
        
        // Initialize OpenGL
        glClearColor(0.0, 0.0, 0.0, 1.0);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
        
        glViewport(0, 0, GRAPHICS_WIDTH, GRAPHICS_HEIGHT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, GRAPHICS_WIDTH, GRAPHICS_HEIGHT, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
    
    
        // Textures
        glEnable(GL_TEXTURE_2D);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    
        if(graphics_texturecreate(&noise)) {
            printf("Could not create texture: %s\n", SDL_GetError());
            return 1;
        }
    
    
        return 0;
    }
    
    
    void graphics_render(void) {
        // Rendering
        glClear(GL_COLOR_BUFFER_BIT);
        
        // Draw a textured quad
        glBindTexture(GL_TEXTURE_2D, 0);
        glBegin(GL_QUADS);
            //glColor3ub(255, 0, 0);
            glTexCoord2f(0, 0);
            glVertex2i(100, 100);
    
    
            //glColor3ub(0, 255, 0);
            glTexCoord2f(0, 1);
            glVertex2i(100, 164);
    
    
            //glColor3ub(0, 0, 255);
            glTexCoord2f(1, 1);
            glVertex2i(164, 164);
    
    
            //glColor3ub(255, 0, 255);
            glTexCoord2f(1, 0);
            glVertex2i(164, 100);
        glEnd();
        
        // Present frame
        SDL_GL_SwapWindow(graphics_window);
    }
    
    
    
    
    void graphics_close(void) {
        // Clean up
        SDL_DestroyWindow(graphics_window);
        SDL_GL_DeleteContext(graphics_context);
    
    
        // Exit SDL2
        SDL_Quit();
    }
    keep in mind that I am still looking at what the best structure for my
    code should be, I will probably still split up the texture stuff from the
    regular stuff, and change something in my naming convention, but
    until then, this is what I've got.

    Thanks for the help all :]

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    I din't analize your code, but one thing you have to do is to tell SDL2 the type of context of OpenGL you are using. Nowadays OpenGL expects CORE PROFILE, but you are using COMPATIBLE PROFILE. So, add this call to your code:
    Code:
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-09-2018, 11:14 AM
  2. OpenGL - Texture or no texture?
    By MathFan in forum Game Programming
    Replies: 1
    Last Post: 12-21-2008, 12:48 PM
  3. openGL rendering
    By zacs7 in forum Game Programming
    Replies: 11
    Last Post: 10-24-2007, 03:58 PM
  4. Texture mapping in OpenGL
    By Dragon in forum Game Programming
    Replies: 5
    Last Post: 10-19-2003, 09:47 AM
  5. rendering texture to bmp
    By jverkoey in forum Game Programming
    Replies: 0
    Last Post: 07-06-2003, 11:55 AM

Tags for this Thread