2012-06-18 73 views
2

我遇到了有關放置在二維四邊形上的2D紋理的問題。我有一個空間街機遊戲,它有小行星,行星和一艘船(紋理),加載完美。SDL上的OpenGL紋理僅顯示一些紋理

我現在正在研究電源ups,但出於某種奇怪的原因,它只顯示了我在每次開機時指定的3種不同紋理之一,並且不時顯示左上角紋理的大版本沒有做任何事情(沒有碰撞功能)。

我在進入遊戲循環之前將紋理加載到變量中,並檢查它是否實際加載並且int值保留了值,因此很好。當一顆小行星被破壞時,一個PowerUp對象被實例化,並且GLuint紋理屬性被來自之前加載的紋理的int值填充。

我會後一些代碼片段顯示一些錯誤我可能會做:

//Initialisation of Vectors that hold all PowerUps dropped in game 
vector<PowerUp_Speed> powerUps; 
vector<PowerUp_Gun> powerUps_Gun; 
vector<PowerUp_FireRate> powerUps_FireRate; 

//PowerUps Texture Loading (Before the main loop and after SDL init) 
int speedTexture = loadTexture("sprites/Speed.png"); 
int rateTexture = loadTexture("sprites/Rate.png"); 
int gunTexture = loadTexture("sprites/Gun.png"); 

下面的部分是在主迴路(當子彈VS小行星發生碰撞時)

//PowerUps 
PowerUp_Speed boost; 
boost.texture = speedTexture; 
bool push = boost.Drop(powerUps, destr[d]); 
if(push) powerUps.push_back(boost); 

PowerUp_Gun gunBoost; 
gunBoost.texture = rateTexture; 
push = gunBoost.Drop(powerUps_Gun, destr[d]); 
if(push) powerUps_Gun.push_back(gunBoost); 

PowerUp_FireRate fireBoost; 
fireBoost.texture = gunTexture; 
push = fireBoost.Drop(powerUps_FireRate, destr[d]); 
if(push) powerUps_FireRate.push_back(fireBoost); 

而且其中一個PowerUp對象顯示如何使用這些功能:

#include "SpaceGame.h" 

PowerUp_Speed::PowerUp_Speed() 
{ 
    isAlive = true; 
    width = 40; 
    height = width; 
    chance = 5; 
} 

void PowerUp_Speed::boostPlayer(Ship &ship) 
{ 
    ship.vel += 0.1f; 
} 

void PowerUp_Speed::Draw() 
{ 
    if(isAlive) 
    { 
     glPushMatrix(); 
     glColor4ub(255, 255, 255, 255); 
     glEnable(GL_TEXTURE_2D); 
     glBindTexture(GL_TEXTURE_2D, texture); 
     glBegin(GL_QUADS); 
      glTexCoord2d(0,0); glVertex2f(x, y); 
      glTexCoord2d(1,0); glVertex2f(x + width, y); 
      glTexCoord2d(1,1); glVertex2f(x + width, y + height); 
      glTexCoord2d(0,1); glVertex2f(x, y + height); 
     glDisable(GL_TEXTURE_2D); 
     glPopMatrix(); 
    } 
} 

bool PowerUp_Speed::Drop(vector<PowerUp_Speed> &powerUps, Destructable d) 
{ 
    if(rand() % chance == 1) 
    { 
     if(powerUps.size() == 0) 
     { 
      x = d.x; 
      y = d.y; 
      return true; 
     } 
     else 
     { 
      bool succes = false; 

      for(unsigned int i = 0; i < powerUps.size(); i++) 
      { 
       if(!powerUps[i].isAlive) 
       { 
        powerUps[i].isAlive = true; 
        powerUps[i].x = d.x; 
        powerUps[i].x = d.y; 
        powerUps[i].texture = texture; 
        succes = true; 
        return false; 
       } 
      } 

      if(!succes) 
      { 
       x = d.x; 
       y = d.y; 
       return true; 
      } 
     } 
    } 
    return false; 
} 

及其刪除i n中的頭文件:

class PowerUp_Speed 
{ 
    public: 
     float x, y; 
     int width, height; 
     int chance; 
     bool isAlive; 
     GLuint texture; 
     PowerUp_Speed(); 
     void Draw(); 
     void boostPlayer(Ship &ship); 
     bool Drop(vector<PowerUp_Speed> &powerUps, Destructable d); 
}; 

回答

2

添加glEnd()PowerUp_Speed::Draw()

glBegin(GL_QUADS); 
    glTexCoord2d(0,0); glVertex2f(x, y); 
    glTexCoord2d(1,0); glVertex2f(x + width, y); 
    glTexCoord2d(1,1); glVertex2f(x + width, y + height); 
    glTexCoord2d(0,1); glVertex2f(x, y + height); 
glEnd(); 

glBegin()需要一個相應的glEnd()

+1

Omg,我真是個白癡!我怎麼可能沒有注意到這一點。非常感謝您的注意!添加一個glEnd()修復了我的問題;)。 –

+1

@JoeyDewd:太棒了!我很高興這是簡單的:) – genpfault