SDL 上的 OpenGL 纹理仅显示某些纹理
OpenGL Textures on SDL display only some textures
我遇到了一个关于放置在 2D 四边形上的 2D 纹理的问题。我有一个太空街机游戏,它有小行星,行星和一艘船(有纹理),加载得很好。
我现在正在研究道具,但由于某种奇怪的原因,它只显示我在每次道具上指定的 3 种不同纹理之一,并且不时在左上角显示一个大版本的纹理,它什么都不做(没有碰撞功能)。
在进入游戏循环之前,我将纹理加载到变量中,并检查它是否实际加载并且 int 值保存值,所以这很好。当小行星被摧毁时,将实例化一个 PowerUp 对象,并使用先前加载的纹理中的 int 值填充 GLuint 纹理属性。
我将发布一些代码片段以显示我可能犯的一些错误:
//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");
以下部分是在主循环中(当子弹与小行星碰撞发生时)
//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;
}
及其在头文件中的降级:
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);
};
添加一个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()
。
相关文章:
- dx11 渲染到纹理仅显示透明颜色
- 无法获取要在 OpenGL ES2 中显示的 RGB 纹理
- 如何显示具有 opengl 纹理的灰度图像
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- 如何使用 openGL 在屏幕上显示加载的纹理
- 在 SFML 中不使用动态内存时显示白色纹理代替实际纹理
- 纹理显示为黑色
- 使用两个纹理 SDL2 显示视频
- 纹理未显示
- 渲染到框架/纹理显示为空
- 无法使用 OpenGL 显示纹理
- OpenGL纹理未显示
- 使用OpenGL显示两个对象.纹理不像预期的那样行为
- 试图显示纹理,但只能使用OpenGL获得纯色
- 带有Clang的DirectX11显示黑色纹理(与Visual Studio配合使用)
- OpenGL - 纹理无法正确显示(夹紧?
- 显示加载的纹理
- OpenGL:从数据阵列生成2D纹理以显示在四边形上
- OpenGL3两组着色器,纹理显示为黑色
- SFML 纹理显示为白框