I'm making space invaders (in OpenGL/SDL) and I've run into a problem when trying to draw the sprite for the spaceship from the spritesheet. In my entity class, I have a pointer for the sprite that belongs to the current instance of the object. In main, I make an instance on the heap of the sheetSprite, passing in the u,v,width, and height of the sprite for the parameters and then storing this reference in the mySprite field of the spaceship entity.
The coordinates provided by the texture atlas are: <SubTexture name="playerShip2_green.png" x="112" y="866" width="112" height="75"/>
To draw the spaceship, I call the draw method from its "mySprite" attribute. For some reason, only a white square is being rendered without the actual texture. What could be causing the problem? The relevant code is below:
class SheetSprite {
public:
SheetSprite(unsigned int textureID, float u, float v, float width, float height, float size, ShaderProgram program)
:textureID(textureID), u(u), v(v), width(width), height(height), size(size), program(program) {}
void Draw() {
//glBindTexture(GL_TEXTURE_2D, textureID);
glUseProgram(program.programID);
GLfloat texCoords[] = {
u, v + height,
u + width, v,
u, v,
u + width, v,
u, v + height,
u + width, v + height
};
float aspect = width / height;
float vertices[] = {
-0.5f * size * aspect, -0.5f * size,
0.5f * size * aspect, 0.5f * size,
-0.5f * size * aspect, 0.5f * size,
0.5f * size * aspect, 0.5f * size,
-0.5f * size * aspect, -0.5f * size ,
0.5f * size * aspect, -0.5f * size };
glBindTexture(GL_TEXTURE_2D, textureID);
//float vertices2[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
glVertexAttribPointer(program.positionAttribute, 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program.positionAttribute);
//float texCoords2[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
glVertexAttribPointer(program.texCoordAttribute, 2, GL_FLOAT, false, 0, texCoords);
glEnableVertexAttribArray(program.texCoordAttribute);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program.positionAttribute);
glDisableVertexAttribArray(program.texCoordAttribute);
}
float size;
unsigned int textureID;
float u;
float v;
float width;
float height;
ShaderProgram program;
};
class Entity {
public:
Matrix modelMatrix, projectionMatrix, viewMatrix;
float width, height = 1.0;
float xDir, yDir = 0.0;
float posX, posY = 0.0;
float objSpeed = 0.0;
float rotState = 0.0;
float u, v, spr_width, spr_hght, spr_size = 0.0;
unsigned int textureID;
ShaderProgram program;
//GLuint spriteSheetTexture = LoadTexture("sheet.png");
SheetSprite* mySprite;
Entity(float wid, float hght, float xDirect, float yDirect,
float xPosition, float yPosition, float speed, float rState, ShaderProgram program) : width(wid), height(hght),
xDir(xDirect), yDir(yDirect), posX(xPosition), posY(yPosition), objSpeed(speed), rotState(rState), program(program) {}
//Extra methods
};
//instantiation of sheetSprite in main
Entity spaceship(0.1f, 0.7f, 1.0f, 1.0f, -5.1f, 0.0f, 3.0f, 0.0f, program);
spaceship.objSpeed = 10;
GLuint spriteSheetTexture = LoadTexture("sheet.png");
spaceship.mySprite = new SheetSprite(spriteSheetTexture, 112.0f / 1024.0f, 866.0f / 1024.0f, 112.0f / 1024.0f, 75.0f / 1024.0f, 0.7, program);
//in the game loop
spaceship.mySprite->Draw();
glGetError()
return any errors? If so, find out what line exactly triggers it. \$\endgroup\$glGetError()
? \$\endgroup\$