Textured Quads Problem (SDL/OPENGL)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
NovaCaine
Posts: 34
Joined: Tue Dec 20, 2005 3:31 pm
Location: New Zealand
Contact:

Textured Quads Problem (SDL/OPENGL)

Post by NovaCaine »

Hey all,

I have a new problem now, and my instant guess is I either forgot something or I'm doing something I shouldn't be.

First of all this code runs fine thru SDL on Windows - so I dont think its the code (mainly just hacked together from various tutorials) hence the guess of doing something I shouldn't be. Also just want to point out that I want to keep the code so that it will run on both windows and PSP with minimal code differences.

The problem is it wont render the texture, just a black quad.

Heres the code (it's in a cpp file, thats why theres an extern C)

Code: Select all

/* Test SDL OpenGL support. */ 
#include <stdio.h> 
#include <stdlib.h> 

#include "SDL.h" 
#include "SDL_image.h"
#include "SDL_opengl.h" 

#define SCREEN_WIDTH 480 
#define SCREEN_HEIGHT 272 

SDL_Surface*	screen;
GLuint image;//This is our texture
SDL_Surface *temp, *temp2;//This will help get the pixel data for our texture

int LoadGLTextures&#40;&#41;									// Load Bitmaps And Convert To Textures
&#123;
	// Load image
	SDL_Surface* LoadImage = IMG_Load&#40;"TEST.PNG"&#41;;
	
	if &#40;LoadImage == NULL&#41; &#123;
			fprintf&#40;stderr, "IMG_Load&#58; %s\n", IMG_GetError&#40;&#41;&#41;;
			return 1;
	&#125;
	
	// Convert using SDL_DisplayFormatAlpha
	temp2 = SDL_DisplayFormatAlpha&#40; LoadImage &#41;;
	// Call SDL_SetAlpha on surface
	SDL_SetAlpha&#40; temp2, 0, 0 &#41;;
	// Create temp RGB surface and blit to it
	temp = SDL_CreateRGBSurface&#40;temp2->flags, temp2->w, temp2->h, temp2->format->BitsPerPixel, temp2->format->Rmask, temp2->format->Gmask, temp2->format->Bmask, temp2->format->Amask &#41;; 
	SDL_BlitSurface&#40; temp2, NULL, temp, NULL &#41;;

	if &#40; temp &#41; &#123;
		        
		//Check that the image is square &#40;width equal to height&#41;
		if &#40;temp->w != temp->h&#41; &#123;
			return 2;
		&#125;    

		//Check that the image's width is valid and then check that the image's width is a power of 2
		if &#40;temp->w < 1&#41; &#123;
			return 3;
		&#125; else if &#40; !&#40;&#40;temp->w & &#40;temp->w-1&#41;&#41;==0&#41; &#41; &#123;
			return 4;
		&#125;    
		        
		//Create the texture
		glGenTextures&#40;1, &image&#41;;

		//Load the texture
		glBindTexture&#40;GL_TEXTURE_2D, image&#41;;

		//Generate the texture
		glTexImage2D&#40;GL_TEXTURE_2D, 0, GL_RGBA, temp->w, temp->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, temp->pixels&#41;;

		//Use nearest filtering, very good
		glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST&#41;;
		glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST&#41;;
	&#125; else &#123;
		return 5;
	&#125;    

	//Free up our temp surface if it was ever used
	if &#40;temp&#41; &#123; 
		SDL_FreeSurface&#40;temp&#41;; 
		SDL_FreeSurface&#40;temp2&#41;; 
		SDL_FreeSurface&#40;LoadImage&#41;; 
	&#125;

	return 0;										// Return The Status
&#125;

int DrawGLScene&#40;GLvoid&#41;									// Here's Where We Do All The Drawing
&#123;

	float x = 32.0f, y = 16.0f;

	//Make certain everything is cleared from the screen before we draw to it
	glClear&#40;GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT&#41;;

	//Enable texturing
	glEnable&#40;GL_TEXTURE_2D&#41;;

	// Enable transparency
	glBlendFunc&#40;GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA&#41;;
	glEnable&#40;GL_BLEND&#41;;

	//Load the texture
	glBindTexture&#40;GL_TEXTURE_2D, image&#41;;

	glBegin&#40;GL_QUADS&#41;;
		// ISO tile
		////Top-left vertex &#40;corner&#41;
		//glTexCoord2i&#40;0,0&#41;;
		//glVertex3f&#40;x, y-16.0f, 0.0f&#41;;

		////Bottom-left vertex &#40;corner&#41;
		//glTexCoord2i&#40;0,1&#41;;
		//glVertex3f&#40;x-32.0f, y, 0.0f&#41;;

		////Bottom-right vertex &#40;corner&#41;
		//glTexCoord2i&#40;1,1&#41;;
		//glVertex3f&#40;x, y+16, 0.0f&#41;;

		////Top-right vertex &#40;corner&#41;
		//glTexCoord2i&#40;1,0&#41;;
		//glVertex3f&#40;x+32, y, 0.0f&#41;;

		// Fullscreen
		//Top-left vertex &#40;corner&#41;
		glTexCoord2f&#40;0.0f,0.0f&#41;;
		glVertex3f&#40;0.0f, 0.0f, 0.0f&#41;;

		//Bottom-left vertex &#40;corner&#41;
		glTexCoord2f&#40;0.0f,1.0f&#41;;
		glVertex3f&#40;0.0f, SCREEN_HEIGHT, 0.0f&#41;;

		//Bottom-right vertex &#40;corner&#41;
		glTexCoord2f&#40;1.0f,1.0f&#41;;
		glVertex3f&#40;SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f&#41;;

		//Top-right vertex &#40;corner&#41;
		glTexCoord2f&#40;1.0f,0.0f&#41;;
		glVertex3f&#40;SCREEN_WIDTH, 0.0f, 0.0f&#41;;
	glEnd&#40;&#41;;

	//Disable texturing
	glDisable&#40;GL_TEXTURE_2D&#41;;				// Keep Going
	glDisable&#40;GL_BLEND&#41;;

	return 1;
&#125;

extern "C" &#123;
int SDL_main&#40;&#41; &#123;
//int main&#40;int argc, char* argv&#91;&#93;&#41; &#123;
    int done = 0; 

    if &#40;SDL_Init&#40;SDL_INIT_VIDEO | SDL_INIT_JOYSTICK&#41; < 0&#41; 
    &#123; 
        fprintf&#40;stderr, "Unable to initialize SDL&#58; %s\n", SDL_GetError&#40;&#41;&#41;; 
        return 1; 
    &#125; 

		SDL_GL_SetAttribute&#40; SDL_GL_RED_SIZE, 5 &#41;;
		SDL_GL_SetAttribute&#40; SDL_GL_GREEN_SIZE, 5 &#41;;
		SDL_GL_SetAttribute&#40; SDL_GL_BLUE_SIZE, 5 &#41;;
		SDL_GL_SetAttribute&#40; SDL_GL_DEPTH_SIZE, 16 &#41;;
		SDL_GL_SetAttribute&#40; SDL_GL_DOUBLEBUFFER, 1 &#41;;
		if &#40; &#40;screen=SDL_SetVideoMode&#40; SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_OPENGL | SDL_FULLSCREEN &#41;&#41; == NULL &#41; &#123;  
        fprintf&#40;stderr, "Unable to create OpenGL screen&#58; %s\n", SDL_GetError&#40;&#41;&#41;; 
        SDL_Quit&#40;&#41;; 
        return 1; 
    &#125; 

    glViewport&#40;0, 0, SCREEN_WIDTH, SCREEN_HEIGHT&#41;; 
    glMatrixMode&#40;GL_PROJECTION&#41;; 
    glLoadIdentity&#40;&#41;; 
    glOrtho&#40;0.0f, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, -1.0f, 1.0f&#41;; 
//    glOrtho&#40;-2, 2, -2, 2, -2, 2&#41;;
    glMatrixMode&#40;GL_MODELVIEW&#41;; 
    glLoadIdentity&#40;&#41;;

	int result = LoadGLTextures&#40;&#41;;

	if&#40; result != 0 &#41; &#123;		
		fprintf&#40;stderr, "Unable to load textures. Returned %d\n", result&#41;; 
    SDL_Quit&#40;&#41;; 
    return 1;
	&#125;

	glClearColor&#40;1.0f,0.0f,1.0f,0.5f&#41;;
	glClearDepth&#40;1.0f&#41;;
	glDepthFunc&#40;GL_LEQUAL&#41;;
	glEnable&#40;GL_DEPTH_TEST&#41;;
	glShadeModel&#40;GL_SMOOTH&#41;;
	glDisable&#40;GL_CULL_FACE&#41;;

  while &#40;!done&#41; 
    &#123; 
//        glClearColor&#40;1.0f, 0.0f, 1.0f, 0.5f&#41;; 
//
//        angle += delta; 
//        glMatrixMode&#40;GL_MODELVIEW&#41;; 
//        glLoadIdentity&#40;&#41;; 
//        glRotatef&#40;angle * 1.32f, 0.0f, 0.0f, 1.0f&#41;; 
//
//        glShadeModel&#40;GL_SMOOTH&#41;; 
//
//        glClear&#40;GL_COLOR_BUFFER_BIT&#41;; 
//        glBegin&#40;GL_TRIANGLES&#41;; 
//            glColor3f&#40;0.0f, 0.0f, 1.0f&#41;; glVertex3f&#40;1.0f, 0.0f, 0.0f&#41;; 
//            glColor3f&#40;0.0f, 1.0f, 0.0f&#41;; glVertex3f&#40;0.0f, 1.0f, 0.0f&#41;; 
//            glColor3f&#40;1.0f, 0.0f, 0.0f&#41;; glVertex3f&#40;0.0f, 0.0f, 1.0f&#41;; 
//        glEnd&#40;&#41;; 
        

		DrawGLScene&#40;&#41;;

//		glFinish&#40;&#41;; 

        SDL_GL_SwapBuffers&#40;&#41;; 
    &#125; 

    return 0; 
&#125;
&#125;//extern "C"
And the makefile

Code: Select all

TARGET = hello
OBJS = Main.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = SDL GL

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
PSPBIN = $&#40;PSPSDK&#41;/../bin
CFLAGS += `$&#40;PSPBIN&#41;/sdl-config --cflags` -DHAVE_OPENGL
LIBS += -lSDL_image -lpng -lz -ljpeg -lglut -lGLU -lGL `$&#40;PSPBIN&#41;/sdl-config --libs` -lm -lc -lpspdebug -lpspge -lpspdisplay -lpspctrl -lpspsdk -lpspvfpu -lpspuser -lpspkernel -lstdc++
include $&#40;PSPSDK&#41;/lib/build.mak
Any Ideas?

Thanks in advance for any help.
Last edited by NovaCaine on Fri Feb 10, 2006 1:30 pm, edited 1 time in total.
HaQue
Posts: 91
Joined: Fri Nov 25, 2005 8:52 am
Location: Adelaide, Australia
Contact:

Post by HaQue »

in your code it is test.png, so use that. is the /Data/ folder "Data" as in your code? and is the image actaully in YOURAPP/Data/test.png?
NovaCaine
Posts: 34
Joined: Tue Dec 20, 2005 3:31 pm
Location: New Zealand
Contact:

Post by NovaCaine »

sorry, I just edited the whole first post to refer to a new problem. the first problem was a basic logic error

Code: Select all

if &#40;!LoadImage == NULL&#41; &#123;

should have read

if &#40;LoadImage == NULL&#41; &#123;
Now my problem is the texture isn't rendering to the quad, and I get a black square instead.
ufoz
Posts: 86
Joined: Thu Nov 10, 2005 2:36 am
Location: Tokyo
Contact:

Post by ufoz »

I thought pspgl didn't support quads at all?
Or is that fixed by now? :|
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Correct - GL_QUADS is mapped to GL_TRIANGLE_FAN, so if you only render one quad at a time it's ok (but slow if you do lots), but it's broken if you try to draw more than one.

Jim
NovaCaine
Posts: 34
Joined: Tue Dec 20, 2005 3:31 pm
Location: New Zealand
Contact:

Post by NovaCaine »

Jim wrote:Correct - GL_QUADS is mapped to GL_TRIANGLE_FAN, so if you only render one quad at a time it's ok (but slow if you do lots), but it's broken if you try to draw more than one.

Jim
I had only just realised this after I had posted and left work (I currently dont have the net at home.) but even after I had changed it to triangles it crashes majorly (No BSOD, and have to manually reset the PSP). Just wondering how I would go about rendering a textured triangle, as I'm guessing there is something PSPGL doesn't like about my code.
Post Reply