• Advertisement
Sign in to follow this  

MacosX / SDL/ OpenGL and render texture

This topic is 4579 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

Hello, I have a problem when I use SDL to put texture in OpenGL. Curiosily I testing my code on pc, the texture is on quads. So, not problem. But on macosX I don't see the texture!!!
[source lang = "cpp"] 
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

SDL_Surface *Screen = NULL;

#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];

static GLuint texName;

//--------------------------------------------
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    
    glEnable(GL_DEPTH_TEST);
    //makeCheckImage();
    glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
    
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);
    
 
    SDL_Surface *image = SDL_LoadBMP("image.bmp");
    if(image == NULL) exit(1);
    
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w,
                 image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, 
                 image->pixels);
                 
    SDL_FreeSurface( image );            
}
//--------------------------------------------
void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glEnable(GL_TEXTURE_2D);
 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 glBindTexture(GL_TEXTURE_2D, texName);
 
    
 glBegin(GL_QUADS);
                   glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
                   glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
                   glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
                   glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
                   
                   glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
                   glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
                   glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
                   glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
 glEnd();
 glFlush();
 
 glDisable(GL_TEXTURE_2D);
}
//--------------------------------------------
void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
    glMatrixMode(GL_MODELVIEW);
    glTranslatef(0.0, 0.0, -3.6); 
}
//--------------------------------------------
int main(int argc, char *argv[]){
  SDL_Event event;

  SDL_Init(SDL_INIT_EVERYTHING);
  
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  Screen = SDL_SetVideoMode(250, 250, 16, SDL_OPENGLBLIT  );
  
  init();
  reshape(250, 250);
  
  
  int done = 0;
  while(!done){
    
    
    SDL_PollEvent(&event);
    if(event.key.keysym.sym == SDLK_ESCAPE)
      done = 1;
    
    display();
     
    
    SDL_GL_SwapBuffers();
  }
  SDL_Quit();
  return(0);
}

Please I am a newbies. Somebody can find my blunder? thanks...

Share this post


Link to post
Share on other sites
Advertisement
The only thing I noticed (and this probably doesn't have anything to do with the problem) is that you're using the SDL_OPENGLBLIT flag, which is depreciated, and you should be using the SDL_OPENGL flag instead.

Share this post


Link to post
Share on other sites
This could be the problem...

On a PC, developing with something like MSVC you can get away with putting your images in the same folder as all your source code, and your project file then build and it finds the image file just fine...

e.g.

C:\MySpiffyCode\main.cpp
C:\MySpiffyCode\myproject.vsw (or what ever a workspace file is called)
C:\MySpiffyCode\image.bmp

so in your code the "image.bmp" for the filename works fine.

In xcode i have found that if i had the same project set up just like above, in code i would have to give the file name like this.

"../../image.bmp"

and everything works fine. but then when it comes time to release the file and directory structures, i don't know what will happen.


so change
SDL_Surface *image = SDL_LoadBMP("image.bmp");
to
SDL_Surface *image = SDL_LoadBMP("../../image.bmp");

and i bet it will work.

Share this post


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

  • Advertisement