Jump to content
  • Advertisement
Sign in to follow this  
kev000

sdl+opengl sdl_image to texture problem

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

I have two questions: 1) how can I translate opengl coords to normal sdl-like coords (ie 0,0 is the top-left most screen coord)? 2) I can't get my image to display. It only displays a lame white box. here are the two relevant files: http://nghost-project.com/temp/gfxengine.cpp http://nghost-project.com/temp/icepanel.cpp NOTE: I'm really _really_ new at opengl stuff. so I'm sure there are a lot of redundancies and really bad code. I hope to get a lot of suggestions from you experienced types. big big thanks

Share this post


Link to post
Share on other sites
Advertisement
Why do you need to translate from OpenGL coordinates to SDL coordinates? Generally speaking, if you're using OpenGL, SDL coordinates are pretty much irrelevant, as OpenGL does your rendering.

Share this post


Link to post
Share on other sites
I want my code to be backwards-compatible with my earlier version written in straight sdl. I'm basically rewriting my SDL gui toolkit to be rendered with opengl. All the coords of the objects are specified via a xml file. I figured it would be advantagious to keep the same form in the xml and just translate the coords. That way user's button at x,y still appears at x,y and he won't have to change anything.

Share this post


Link to post
Share on other sites
I think it got moved somehow, as it was in a different forum when I replied earlier. I'll move it back over there.

Share this post


Link to post
Share on other sites
Quote:
Original post by kev000
I want my code to be backwards-compatible with my earlier version written in straight sdl. I'm basically rewriting my SDL gui toolkit to be rendered with opengl. All the coords of the objects are specified via a xml file. I figured it would be advantagious to keep the same form in the xml and just translate the coords. That way user's button at x,y still appears at x,y and he won't have to change anything.


Ok, that's subtly different from what you said. Really you're looking to be able to use projected screen coordinates rather than world coordinates. For this, you typically need an orthographic projection, via glOrtho. Try searching this and the OpenGL forum for that keyword.

Share this post


Link to post
Share on other sites
Here is the set up code i use to archive sdl coordinates in opengl

glViewport( 0, 0, m_iScreen_Width, m_iScreen_Heigth );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glOrtho( 0, m_iScreen_Width, m_iScreen_Heigth, 0, -1, 1 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();


I then flip the textures by using the texture coordinates on the polygon

Share this post


Link to post
Share on other sites
thanks blackcloak for the example. any hints on why my texture isn't showing up as anything but a white box?

thanks again

Share this post


Link to post
Share on other sites
Can I see the code for how your are createing the texture? Most likely you are missing these funtion calls (your arguments to them may be different)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
before your glTexImage2D.
If that does not work you are welcome to look at how I am doing it
Epee Engine look at the Sprockets::CreateTexture function and the Render function.

Share this post


Link to post
Share on other sites
here is my exact function that creates the texture.

int gfxengine_opengl::createTexture(SDL_Surface *surface, string name)
{
/* Status indicator */
GLuint texture;
/* Create storage space for the texture */
SDL_Surface *TextureImage[1];
textureID t;
/* Create The Texture */
glGenTextures( 1, &texture );

/* Typical Texture Generation Using Data From The Bitmap */
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
/* Generate The Texture */
glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w,
surface->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, surface->pixels );

/* Linear Filtering */

t.handle=texture;
t.name=name;
textures.push_back(t);
cout<<"texture handle: "<<t.handle<<endl;
int texid=textures.size()-1;
/* Free up any memory we may have used */
if ( surface )
SDL_FreeSurface( surface );

return texid;
}

int gfxengine_opengl::drawRect(SDL_Rect coords,int texid)
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
//glColor3f(0.7f,1.0f,0.3f);
cout<<textures[texid].handle<<" "<<textures[texid].name<<endl;
glTranslatef( 0.0f, 0.0f, -4.0f );
glBindTexture( GL_TEXTURE_2D, textures[texid].handle );
glBegin( GL_QUADS ); /* Draw A Quad */
glTexCoord2f( 0.0f, 1.0f );glVertex3f( -35.0f, 1.7f, 0.0f ); /* Top Left */
glTexCoord2f( 1.0f, 1.0f );glVertex3f( 35.0f, 1.7f, 0.0f ); /* Top Right */
glTexCoord2f( 1.0f, 0.0f );glVertex3f( 35.0f, -1.0f, 0.0f ); /* Bottom Right */
glTexCoord2f( 0.0f, 0.0f );glVertex3f( -35.0f, -1.0f, 0.0f ); /* Bottom Left */
glEnd( );
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!