So I’ve recently began trying to learn the basics of how OpenGL works via videos and other resources. But recently I’ve run into some trouble while trying to load an image and render it for a 2D game. But every once in a while, the image’s pixels that should each be rendered by something like 4x4 actual pixels are 4x6 or 6x6 or some other random size. I’m not sure how to clarify further without the help of an image to show what I mean. I think it might have something to do with the texture code, because I have tried it with multiple different meshes, and the same thing occurs. Here is my texture code:
Texture.cpp:
#include "texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
Texture::Texture()
{
}
Texture::~Texture()
{
glDeleteTextures(1, &_texture);
}
void Texture::Init(const std::string& filename)
{
int width, height, numComponents;
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &numComponents, 4);
if (data == NULL)
std::cerr << "Unable to load texture: " << filename << std::endl;
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
void Texture::Bind(unsigned int unit)
{
glBindTexture(GL_TEXTURE_2D, _texture);
}
void Texture::DelTexture()
{
glDeleteTextures(1, &_texture);
}
Texture.h:
#pragma once
#include <GL/glew.h>
#include <string>
class Texture
{
public:
Texture();
void Init(const std::string& filename);
void Bind(unsigned int unit);
void DelTexture();
~Texture();
protected:
private:
GLuint _texture;
};
If you need any other code let me know, and I will post it. Thank you!