正在渲染OpenGL / C ++不正确的像素

时间:2016-06-16 22:08:54

标签: c++ opengl 2d

我第一次使用OpenGL制作基本游戏。但是,我遇到了一个问题,当加载一个精灵然后渲染它时,偶尔会添加一些额外的像素,它应该是透明的或显示背景。

这是我的纹理代码:

#include "Player.h"

#include <iostream>
#include <glm/glm.hpp>

Player::Player(std::string texLocation)
{
    Vertex vertices[] = { 
        Vertex(glm::vec3(0.0033325 * 9, 0.0033325 * 14, 0.0f),
        glm::vec2(0.0f, 0.0f)),
        Vertex(glm::vec3(0.0033325 * 9, -0.0033325 * 14, 0.0f),
        glm::vec2(0.0f, 1.0f)),
        Vertex(glm::vec3(-0.0033325 * 9, -0.0033325 * 14, 0.0f),
        glm::vec2(1.0f, 1.0f)),
        Vertex(glm::vec3(-0.0033325 * 9, 0.0033325 * 14, 0.0f),
        glm::vec2(1.0f,  0.0f))
};

    _mesh.Init(vertices, sizeof(vertices) / sizeof(vertices[0]));
    _texture.Init(texLocation);
}

Player::~Player()
{
}

void Player::Bind(Texture* tex)
{
    tex->Bind(0);
}

void Player::BindHealth()
{
    _heTexture.Bind(0);
}

void Player::Render()
{
    _mesh.Draw();
}

编辑:这是我用来渲染播放器的代码:

// Since, as per your question, you're using arrays even though
// you state you want ArrayLists, we will assume you know why you're
// doing so.
int[] a = new int[x];
int[] b = new int[x];
Random random = new Random();
IntUnaryOperator unaryOpInt = (p) -> random.nextInt(maxIntVal);
// populate array a with random numbers
Arrays.setAll(a, unaryOpInt); 
// populate array b with random numbers
Arrays.setAll(b, unaryOpInt); 
// Since, as per your question, you require ArrayLists, you can
// create ArrayLists from arrays a and b as follows.
// create an ArrayList from array a
ArrayList<Integer> aArrList = new ArrayList<>(IntStream.of(a).boxed().collect(Collectors.toList()));
// create an ArrayList from array b
ArrayList<Integer> bArrList = new ArrayList<>(IntStream.of(b).boxed().collect(Collectors.toList()));

这个问题只会偶尔出现一次,而我只是在播放器上发现了它。

1 个答案:

答案 0 :(得分:0)

取出glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)并报告发生的情况。