1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| 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); } 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);
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(); 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); int textureUnif = glGetUniformLocation(program, "tex"); 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; glUseProgram(program); glUniform1i(textureUnif, i); glUseProgram(0); 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);
while(!Display.isCloseRequested()) { glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program); glActiveTexture(GL_TEXTURE0 + i); glBindTexture(GL_TEXTURE_2D, texture); glBindSampler(i, sampler); glBindVertexArray(vao); glDrawArrays(GL_QUADS, 0, 4); glBindSampler(i, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); glUseProgram(0); Display.update(); Display.sync(30); } }
} |