Jump to content
  • Advertisement
Sign in to follow this  
bugboy

OpenGL Binding a texture with SDL + Opengl

This topic is 5132 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've tried several methods for getting a textured quad and they all are only producing a rectangle. Can you look at my code and see what you think is the deal?
#include <windows.h>
#include <SDL/SDL.h>
#include "SDL_OpenGL.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include	<gl\glaux.h>
#include "pTexture.h"
#include <stdio.h>
#include <stdlib.h>
		
pTexture	you;
GLuint	texture[1];	
static GLboolean should_rotate = GL_TRUE;
int timez=0;
static void quit( int code )
{
SDL_Quit( );
exit( code );
}
AUX_RGBImageRec *LoadBMP(char *Filename)			// Loads A Bitmap Image
{
	FILE *File=NULL;					// File Handle

	if (!Filename)						// Make Sure A Filename Was Given
	{
		return NULL;					// If Not Return NULL
	}

	File=fopen(Filename,"r");				// Check To See If The File Exists

	if (File)						// Does The File Exist?
	{
		fclose(File);					// Close The Handle
		return auxDIBImageLoad(Filename);		// Load The Bitmap And Return A Pointer
	}
	return NULL;						// If Load Failed Return NULL
}
static void handle_key_down( SDL_keysym* keysym )
{
switch( keysym->sym ) {
case SDLK_ESCAPE:
quit( 0 );
break;
default:
break;
}
}
int LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
{
        int Status=FALSE;                               // Status Indicator

        AUX_RGBImageRec *TextureImage[1];               // Create Storage Space For The Texture

        memset(TextureImage,0,sizeof(void *)*1);        // Set The Pointer To NULL

        // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
        if (TextureImage[0]=LoadBMP("you.bmp"))
        {
                Status=TRUE;                            // Set The Status To TRUE

                glGenTextures(1, &texture[0]);          // Create One Texture

                // Create Linear Filtered Texture
                glBindTexture(GL_TEXTURE_2D, texture[0]);
                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
                glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        }
        if (TextureImage[0])                            // If Texture Exists
        {
                if (TextureImage[0]->data)              // If Texture Image Exists
                {
                        free(TextureImage[0]->data);    // Free The Texture Image Memory
                }

                free(TextureImage[0]);                  // Free The Image Structure
        }

        return Status;                                  // Return The Status
}
static void process_events( void )
{
SDL_Event event;

while( SDL_PollEvent( &event ) ) {
switch( event.type ) {
case SDL_KEYDOWN:
handle_key_down( &event.key.keysym );
break;
case SDL_QUIT:
quit( 0 );
break;
}
}
}

static void draw_screen( void )
{
		
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	
	glLoadIdentity();								// Reset The View Before We Draw Each Star
		glTranslatef(0.0f,0.0f,-15.0f);					// Zoom Into The Screen (Using The Value In 'zoom')

		glBindTexture(GL_TEXTURE_2D, texture[0]);	
			glBegin(GL_QUADS);
				glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
				glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
				glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
				glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
			glEnd();	
    SDL_GL_SwapBuffers( );
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
{
	if (height==0)										// Prevent A Divide By Zero By
	{
		height=1;										// Making Height Equal One
	}

	glViewport(0,0,width,height);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}

static void setup_opengl( int width, int height )
{

		LoadGLTextures()		;					// Jump To Texture Loading Routine


	glEnable(GL_TEXTURE_2D);							// Enable Texture Mapping
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	glBlendFunc(GL_SRC_ALPHA,GL_ONE);					// Set The Blending Function For Translucency
	glEnable(GL_BLEND);
}

int main( int argc, char* argv[] )
{
const SDL_VideoInfo* info = NULL;

int width = 0;
int height = 0;

int bpp = 0;

int flags = 0;

if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
fprintf( stderr, "Video initialization failed: %s\n",
SDL_GetError( ) );
quit( 1 );
}

info = SDL_GetVideoInfo( );

if( !info ) {
fprintf( stderr, "Video query failed: %s\n",
SDL_GetError( ) );
quit( 1 );
}

   
width = 640;
height = 480;
bpp = info->vfmt->BitsPerPixel;

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

flags = SDL_OPENGL;

if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
fprintf( stderr, "Video mode set failed: %s\n",
SDL_GetError( ) );
quit( 1 );
}
ReSizeGLScene(width, height);
setup_opengl( width, height );


while( 1 ) {
        process_events( );

        draw_screen( );
}
    return 0;
}


Thanks in advance cp

Share this post


Link to post
Share on other sites
Advertisement
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!