[英]2d Texture missing triangle chunk using SOIL and OpenGL (C++)
我有一些Java游戏开发经验,这是我第一次尝试C ++游戏开发。
该游戏被认为是一种非常简单的2d游戏,有点怪异但带有气泡。
似乎我没有正确渲染我的Sprites,所以发生了这种情况:
我读了这篇文章,所以我将纹理的尺寸调整为400x400px,但这并不能解决任何问题。
这与深度有关吗? 或混合? 我究竟做错了什么?
主要:
#include "Engine\Engine.h"
#include <iostream>
#include "Engine/Graphics/Sprite.h"
using namespace std;
int main()
{
Engine engine;
engine.Initialize("Bursting!");
Sprite testSprite = Sprite("Assets/Art/Bubble.png", 100,100);
while (true)
{
engine.Update();
testSprite.Update();
engine.BeginRender();
testSprite.Render();
engine.EndRender();
}
return 0;
}
Engine.cpp:
#include "Engine.h"
int Engine::SCREEN_WIDTH = 1024;
int Engine::SCREEN_HEIGHT = 768;
GLFWwindow* Engine::window = NULL;
Engine::Engine()
{
}
Engine::~Engine()
{
}
bool Engine::Initialize(char* windowTitle)
{
if (!glfwInit())
{
cout << "Error initializing GLFW" << endl;
return false;
}
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, windowTitle, NULL, NULL);
if (window == NULL)
{
cout << "Error creating window " << endl;
return false;
}
//OpenGL Setup
glfwMakeContextCurrent(window);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glfwSwapInterval(1);
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
int xPos = (mode->width - SCREEN_WIDTH) / 2;
int xyPos = (mode->height - SCREEN_HEIGHT) / 2;
//GL Setup
//Viewport
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -32, 32);
glDepthRange(-32, 32);
glMatrixMode(GL_MODELVIEW);
//Alpha Blending
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
void Engine::Update()
{
glfwPollEvents();
}
void Engine::BeginRender()
{
glClearColor(0, 0, 0, 0); //clear back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
//this swaps buffers, anything that needs to be drawn and seen by human eyes needs to happen before this func
void Engine::EndRender()
{
glfwSwapBuffers(window);
}
Texture.cpp:
#include "Texture.h"
Texture::Texture()
{
id = -1; //Opengl tracks our texture as, this allows us to set texture before drawing
}
Texture::Texture(int _id)
{
id = _id;
if (!GetTextureParams())
{
cout << "Error loading Image with ID: " << id << endl;
}
}
Texture::Texture(string path)
{
id = SOIL_load_OGL_texture(path.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MULTIPLY_ALPHA);
if (!GetTextureParams())
{
cout << "Error loading Image from path: " << path << endl;
}
}
int Texture::getId()
{
return id;
}
int Texture::getWidth()
{
return width;
}
int Texture::getHeight()
{
return height;
}
bool Texture::GetTextureParams()
{
if (id > 0)
{
int mipLevel = 0;
glBindTexture(GL_TEXTURE_2D, id);
glGetTexLevelParameteriv(GL_TEXTURE_2D, mipLevel, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, mipLevel, GL_TEXTURE_HEIGHT, &height);
return true;
}
return false;
}
Sprite.cpp:
#include "Sprite.h"
Sprite::Sprite()
{
xPos = 0;
yPos = 0,
texture = Texture();
}
Sprite::Sprite(string imgPath)
{
texture = Texture(imgPath);
xPos = 0;
yPos = 0;
}
Sprite::Sprite(string imgPath, float _xPos, float _yPos)
{
texture = Texture(imgPath);
xPos = _xPos;
yPos = _yPos;
}
void Sprite::Update()
{
}
void Sprite::Render()
{
glEnable(GL_TEXTURE_2D),
glBindTexture(GL_TEXTURE_2D, texture.getId());
glLoadIdentity();
//TRANS -> ROT -> SCALE
glTranslatef(xPos, yPos, 0);
//rendering
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texture.getWidth(), 0);
glTexCoord2f(0, 1); glVertex2f(0 , texture.getHeight());
glTexCoord2f(1, 1); glVertex2f(texture.getWidth(), texture.getHeight());
glEnd();
glDisable(GL_TEXTURE_2D);
}
我在构建错误或导入错误。 即行的顺序错误:
在我的sprite.cpp中,我的render()看起来像:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texture.getWidth(), 0);
glTexCoord2f(0, 1); glVertex2f(0 , texture.getHeight()); //error
glTexCoord2f(1, 1); glVertex2f(texture.getWidth(), texture.getHeight());
glEnd();
glDisable(GL_TEXTURE_2D);
我改变了顺序:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texture.getWidth(), 0);
glTexCoord2f(1, 1); glVertex2f(texture.getWidth(), texture.getHeight());
glTexCoord2f(0, 1); glVertex2f(0 , texture.getHeight()); //correct position
glEnd();
glDisable(GL_TEXTURE_2D);
现在就可以了! 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.