How to plot graphics and images . png in the same window with openGL and SDL

Asked

Viewed 29 times

-1

I have done some search, in manuals and such on google however I would like to know how to adapt my code below to plot a chart (line for example) along with a file image .png. I do not know where I am missing below:

#include </usr/include/SDL/SDL.h> //compilar com gcc -o exSDLopengl exSDLopengl.c -lSDL -lGL -lSDL_image
#include </usr/include/GL/gl.h>
#include </usr/include/SDL/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>

    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    const int SCREEN_BPP = 32;

    int tex;

 
    void init(){
        glClearColor(0.0,0.0,0.0,0.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0,800,600,1.0,-1.0,1.0);
        glEnable(GL_BLEND);
        glEnable(GL_TEXTURE_2D);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
        
    }
  
    void draw2() {
      
      //init();
      glDisable(GL_TEXTURE_2D); // isto é necessário quando se deseja desenhar SEM texturas
      glColor3f(1,1,0);
      glLineWidth(3);
      glBegin(GL_LINES);
        glVertex2i(0,0);
        glVertex2i(100,100);
      glEnd();
      glColor3f(1,1,1);
    
    
     }
     
     void mostra_imagem() {
     
              SDL_Surface* screen1;
              SDL_Surface* imagemPNG;
              SDL_Rect dest;
              
                            
              
              screen1=SDL_SetVideoMode(800,600,16,SDL_SWSURFACE | SDL_OPENGL);
              
              
              
     
              imagemPNG = IMG_Load("mar.png");
              
              dest.x=10;
              dest.y=10;  
              
              SDL_FillRect(screen1, NULL, 0x3); // Pinta de preto todo o screen
              SDL_BlitSurface(imagemPNG, NULL, screen1, &dest); // mostra imagem do mar
              SDL_UpdateRect(screen1,0,0,0,0); // Atualiza o screen com a imagem blitada
              SDL_Delay(1000);  
        } 
    
    int main(int argc,char** argv){
       
        SDL_Init(SDL_INIT_VIDEO);
        //SDL_Surface* screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_OPENGL);
        
               
         int running=1;
        Uint32 start;
        SDL_Event event;
        //init();
        mostra_imagem();
        draw2();
        
    
        SDL_Quit();
        return 0;
    }

1 answer

0

When Opengl is activated we have to move the image to a texture. Then we need to call the Sdl_gl_swapbuffers() function to update the screen.

#include </usr/include/SDL/SDL.h> //compilar com gcc -o exSDLopengl exSDLopengl.c -lSDL -lGL -lSDL_image
#include </usr/include/GL/gl.h>
#include </usr/include/SDL/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

int tex;

void init(){
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,640,480,1.0,-1.0,1.0);
    glViewport(0, 0, 640, 480);
    glEnable(GL_BLEND);
    glEnable(GL_TEXTURE_2D);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    
}

void draw2() {
    glDisable(GL_TEXTURE_2D); // isto é necessário quando se deseja desenhar SEM texturas
    glColor3f(1,1,0);
    glLineWidth(3);
    glBegin(GL_LINES);
    glVertex2i(0,0);
    glVertex2i(100,100);
    glEnd();
    glColor3f(1,1,1);
}

void mostra_imagem(SDL_Surface* screen, SDL_Surface* image) {
    GLuint texture = 0;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
    
    glColor3f(1.0f, 1.0f, 1.0f);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex2f(image->w, 0.0f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex2f(image->w, image->h);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2f(0.0f, image->h);
    glEnd();
}

int main(int argc,char** argv){
    
    SDL_Init(SDL_INIT_VIDEO);
    int running=1;
    Uint32 start;
    SDL_Event event;
    SDL_Surface* screen1;
    screen1 = SDL_SetVideoMode(800,600,16,SDL_SWSURFACE | SDL_OPENGL);
    SDL_Surface* imagemPNG;
    imagemPNG = IMG_Load("mar.png");
    if(!imagemPNG) {
        printf("IMG_Load: %s\n", IMG_GetError());
    }

    init();
    mostra_imagem(screen1, imagemPNG);
    draw2();
    SDL_GL_SwapBuffers();

    while ( SDL_WaitEvent(&event) >= 0 ) {
        switch (event.type) {
            case SDL_QUIT: {
                printf("Quit.\n");
                exit(0);
            }
            break;
        }
    }

    SDL_Quit();
    return 0;
}
  • 1

    Grateful Augustus. Next step and' use lists to navigate in memory to animate images and drawings made from primitvas. Hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.