PDA

View Full Version : C++ Extra Pixels Are Being Added



DBGreene
06-20-2016, 06:13 PM
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!

john_connor
06-21-2016, 02:16 AM
i think you are missing 2 further filter parameters:



glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

https://www.opengl.org/wiki/GLAPI/glTexParameter

why are you enabling blending there ? it has nothing to do with textures

DBGreene
06-21-2016, 06:12 AM
The reason I was missing those two parameters out was because in some other forum a while back someone told me to remove them and see what happened, and I never put them back. Also thanks for noticing the blending, I don't know why I had put it into the texture.cpp. But once I added those filter parameters nothing seemed to have changed.