Okay so let me start off by saying that this code does work. It does exactly what I expected it to do, no problem (whew!) My question is, is this the proper way? I know I shouldn’t be using a static context here, but this is my first foray into textures so I wanted the entire program to be in one simple class. I am more interested in things like, am I putting “glUseProgram()” in the proper place. I know, I know, if it works who cares, but I will eventually want to divide a lot of this into objects and I don’t want to put code places where it shouldn’t go.
Here is the code:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL33.*;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class TextureTestWindow {
private static final float[] verts = {
0.8f, 0.8f, 0f, 1f,
-0.8f, 0.8f, 0f, 1f,
-0.8f, -0.8f, 0f, 1f,
0.8f, -0.8f, 0f, 1f,
};
private static final float[] texCords = {
1f, 0f,
0f, 0f,
0f, 1f,
1f, 1f};
private static final byte[] texData = {
(byte) 255, (byte) 0, (byte) 0, (byte) 255,
(byte) 0, (byte) 255, (byte) 0, (byte) 255,
(byte) 255, (byte) 255, (byte) 0, (byte) 255,
(byte) 0, (byte) 0, (byte) 255, (byte) 255};
private static final String easyTexVert =
"#version 330\n" +
"layout (location = 0) in vec4 position;\n" +
"layout (location = 1) in vec2 texCord;\n" +
"varying vec2 outTexCord;\n" +
"void main() {\n" +
" gl_Position = position;\n" +
" outTexCord = texCord;\n" +
"}";
private static final String easyTexFrag =
"#version 330\n" +
"varying vec2 outTexCord;\n" +
"out vec4 outputColor;\n" +
"uniform sampler2D tex;\n" +
"void main() {\n" +
" outputColor = texture(tex, outTexCord);\n" +
"}";
public static void main(String[] args) {
try {
Display.setDisplayMode(new DisplayMode(500, 500));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(1);
}
// Create the program
int vShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vShader, easyTexVert);
glCompileShader(vShader);
int fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fShader, easyTexFrag);
glCompileShader(fShader);
int program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glValidateProgram(program);
// Set up the buffers
FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(verts.length+texCords.length);
vertBuffer.put(verts);
vertBuffer.put(texCords);
vertBuffer.flip();
int vertBufferObj = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertBufferObj);
glBufferData(GL_ARRAY_BUFFER, vertBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
ByteBuffer texBuffer = BufferUtils.createByteBuffer(texData.length);
texBuffer.put(texData);
texBuffer.flip();
// Create the texture
int texture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, texBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glBindTexture(GL_TEXTURE_2D, 0);
// get the Uniform location
int textureUnif = glGetUniformLocation(program, "tex");
// set up the sampler
int sampler = glGenSamplers();
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int i = 0; // using the first texture
// Put the texture into the uniform
glUseProgram(program);
glUniform1i(textureUnif, i);
glUseProgram(0);
// make the VAO
int vao = glGenVertexArrays();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vertBufferObj);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, Float.SIZE/8 * verts.length);
glBindVertexArray(0);
// Finally reader to render!! :)
while(!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
// Set up the context
glUseProgram(program);
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, texture);
glBindSampler(i, sampler);
glBindVertexArray(vao);
glDrawArrays(GL_QUADS, 0, 4);
// Clean up
glBindSampler(i, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(0);
Display.update();
Display.sync(30);
}
}
}
Any help is appreciated! ;D (EDIT: I added some comments to the code)