1
\$\begingroup\$

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();
\$\endgroup\$
  • 1
    \$\begingroup\$ Does glGetError() return any errors? If so, find out what line exactly triggers it. \$\endgroup\$ – HolyBlackCat Feb 29 '16 at 8:17
  • \$\begingroup\$ Where would I call glGetError() ? \$\endgroup\$ – loremIpsum1771 Feb 29 '16 at 19:42
  • \$\begingroup\$ @HolyBlackCat I'm using Visual Studio btw. \$\endgroup\$ – loremIpsum1771 Feb 29 '16 at 19:48
  • \$\begingroup\$ Just put it anywhere in the main loop. \$\endgroup\$ – HolyBlackCat Mar 1 '16 at 12:07
  • \$\begingroup\$ @HolyBlackCat Yeah, just put it in the main game loop and I'm not seeing any output from it \$\endgroup\$ – loremIpsum1771 Mar 1 '16 at 15:15
0
\$\begingroup\$

The problem was that the shader program needed to be passed to the SheetSprite class as a pointer instead of just being passed as an object.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ If your problem was that your class was incorrectly copied, you may want to ensure that it is properly copyable or noncopyable instead of accidentally doing the wrong thing when copied or assigned. \$\endgroup\$ – Lars Viklund Sep 26 '16 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.