Results 1 to 3 of 3

Thread: C++ Extra Pixels Are Being Added

  1. #1
    Newbie Newbie
    Join Date
    Jun 2016
    Posts
    2

    C++ Extra Pixels Are Being Added

    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:

    Code :
    #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:

    Code :
    #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!

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    i think you are missing 2 further filter parameters:

    Code :
    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

  3. #3
    Newbie Newbie
    Join Date
    Jun 2016
    Posts
    2
    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.

Similar Threads

  1. FBO problems - youtube Link added ....
    By stringa in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-13-2008, 11:25 PM
  2. What must be added...
    By bkamenov in forum OpenGL ES
    Replies: 2
    Last Post: 09-08-2005, 09:00 PM
  3. TagCMA WIP [Look! Added QuickTime Movies]
    By tgraupmann in forum OpenGL: Windows
    Replies: 2
    Last Post: 01-10-2005, 07:23 AM
  4. GLSL quietly added to OS X?
    By Jeff Russell in forum OpenGL: macOS
    Replies: 3
    Last Post: 06-26-2004, 07:45 PM
  5. What was added.
    By Mazy in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 06-02-2002, 06:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean