PDA

View Full Version : Help with loading image



justin123
12-27-2009, 10:39 PM
This is my code:


#include "player.h"
#include <gl/gl.h>
#include "SOIL.h"

//Physical Variables
GLint x=0;
GLint y=0;
GLint z=0;

GLint x_origin=32;
GLint y_origin=32;

GLdouble angle=0;

int i;

//Sprite Variables

GLint width[0];
GLint height[0];
GLint miplevel = 0;

int image_index;

GLint player_image[0];
GLint player_animation[0];

bool loadPlayerImages(const char* dir)
{

player_image[0] = SOIL_load_OGL_texture
(
dir,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_WIDTH, &amp;width[0]);
glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_HEIGHT, &amp;height[0]);

}

void player::create(int type)
{
loadPlayerImages("img/0.bmp");
//width[0]=64;
//height[0]=64;
}

void player::step()
{
image_index=0;
//x+=1;
}

void player::draw()
{
//glRotated(angle,0,0,32);
//glColor3i(1,1,1);

glBindTexture(GL_TEXTURE_2D, player_image[0]);

glBegin(GL_QUADS); //begin the four sided shape
glTexCoord2i(0,0); glVertex2i(x, y); //first corner at -0.5, -0.5
glTexCoord2i(0,height[image_index]); glVertex2i(x, y+height[image_index]); //second corner at -0.5, 0.5
glTexCoord2i(width[image_index],height[image_index]); glVertex2i(x+width[image_index], y+height[image_index]); //third corner at 0.5, 0.5
glTexCoord2i(width[image_index],0); glVertex2i(x+width[image_index], y); //fourth corner at 0.5, -0.5
glEnd();

}


Its a class for a player object thats supposed to load a image using SOIL, but I can't get it to draw the textured quad. Instead it draws a single colored block.

Can anyone help me?



~Justin123

ColacX
12-28-2009, 04:58 AM
im pretty sure this is the bug

GLint player_image[0]; //this means you just allocated a GLint array that holds 0 GLints you should change it to
GLint player_image[1];

justin123
12-28-2009, 08:52 AM
It still just draws a blue block.

mark ds
12-28-2009, 09:24 AM
Have you enabled texturing?

glEnable( GL_TEXTURE_2D );

justin123
12-28-2009, 01:36 PM
yes I have enabled 2d textures

mark ds
12-28-2009, 01:54 PM
You're tiling the texture over the quad [width]x[height] times. glTexCoord2i, like the float version, expects 0,0 to 1,1 to correctly map the texture.



glBegin(GL_QUADS); //begin the four sided shape
glTexCoord2i(0,0); glVertex2i(x, y); //first corner at -0.5, -0.5
glTexCoord2i(0,1); glVertex2i(x, y+height[image_index]); //second corner at -0.5, 0.5
glTexCoord2i(1,1); glVertex2i(x+width[image_index], y+height[image_index]); //third corner at 0.5, 0.5
glTexCoord2i(1,0); glVertex2i(x+width[image_index], y); //fourth corner at 0.5, -0.5
glEnd();

justin123
12-28-2009, 07:52 PM
Ok, now it works! Thanks a bunch.

~Justin123