Hi. So I’m trying out OpenGL 3.3 with Core Profile and I’m having some weird results when drawing textures to triangles.
Here’s what the result looks like:
The problem: I’m not expecting that to be drawn. I have a plain pink texture (255, 0, 255, 255) that should be drawn on the triangle. There is some transparency issues too.
Here’s my main render loop:
protected void renderScene(Renderer renderer) {
viewMatrix.rotateX(xRot);
viewMatrix.rotateY(yRot);
viewMatrix.translate(new Vector3f(pos).negate());
basicShader.bind();
glActiveTexture(GL_TEXTURE0);
texture.bind();
basicShader.setUniform("tex", 0);
basicShader.setUniform("modelMatrix", modelMatrix);
basicShader.setUniform("viewMatrix", viewMatrix);
basicShader.setUniform("projectionMatrix", projectionMatrix);
Tessellator t = Tessellator.instance;
t.begin(GL_TRIANGLES);
t.colour(1, 1, 0, 1);
t.normal(0, -2, 0);
t.texCoord(1.0f, 1.0f);
t.vertex(1.0f, 1.0f, 0.0f);
t.texCoord(0.0f, 1.0f);
t.vertex(-1.0f, 1.0f, 0.0f);
t.texCoord(1.0f, 0.0f);
t.vertex(1.0f, -1.0f, 0.0f);
t.texCoord(0.0f, 0.0f);
t.vertex(-1.0f, -1.0f, 0.0f);
t.end();
texture.unbind();
basicShader.unbind();
}
Note: The Tessellator class is basically a helpful vertex drawing tool where I use Vertex Array Objects to draw my vertices.
Here’s my fragment shader code:
#version 330 core
in vec4 vColour;
in vec2 vTexCoords;
out vec4 fragColour;
uniform sampler2D tex;
void main() {
vec4 colour = vColour*texture(tex, vTexCoords);
fragColour = colour;
}
Edit: Here’s my Texture loading code, sorry.
public Texture(int width, int height, int[] pixels) {
this.width = width;
this.height = height;
this.pixels = pixels;
}
public void create(ParameterListener parameterListener) {
ByteBuffer buffer = MemoryTracker.createByteBuffer(width * height * 4);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int colour = pixels[x + y * width];
byte r = (byte) ((colour >> 16) & 0xff);
byte g = (byte) ((colour >> 8) & 0xff);
byte b = (byte) (colour & 0xff);
byte a = (byte) ((colour >> 24) & 0xff);
buffer.put(r).put(g).put(b).put(a);
}
}
id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST/*GL_LINEAR*/);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);
System.out.println("Successfully created texture!");
}
If you need anything else to help me please ask. Thanks for your time.