Skip to content

Instantly share code, notes, and snippets.

@chrjen

chrjen/Main.java Secret

Last active Dec 14, 2015
Embed
What would you like to do?
Implementation of the "VBO rendering crashed with glDrawArrays" question on Gamedev Stack Exchange.
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.GLFWDropCallback;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GLContext;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.opengl.GL20.*;
public class Main {
private GLFWErrorCallback errorCallback;
private GLFWKeyCallback keyCallback;
private GLFWFramebufferSizeCallback framebufferCallback;
private GLFWDropCallback dropCallback;
private long window;
int WIDTH = 400;
int HEIGHT = 400;
int vbo_id;
int program;
public Main() {
setupWindow();
glfwMakeContextCurrent(window);
GLContext.createFromCurrent();
GL.createCapabilities(false);
init();
while (true) {
render();
if ( glfwWindowShouldClose(window) == GL_FALSE ) {
glfwPollEvents();
try {
Thread.sleep(34);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
try {
Thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void setupWindow() {
glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
if ( glfwInit() != GL11.GL_TRUE )
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
//glfwWindowHint(GLFW_AUTO_ICONIFY, GL_FALSE); // Does not minimize you application when not focus.
// Create the window
window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, GL_TRUE);
}
});
glfwSetClipboardString(window, "ありがとうございます。 この感じは怖いよ。");
glfwSetDropCallback(window, dropCallback = new GLFWDropCallback() {
@Override
public void invoke(long window, int count, long names) {
PointerBuffer nameBuffer = memPointerBuffer(names, count);
for ( int i = 0; i < count; i++ ) {
System.out.format("\t%d: %s%n", i + 1, memDecodeUTF8(memByteBufferNT1(nameBuffer.get(i))));
}
}
});
ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(
window,
(GLFWvidmode.width(vidmode) - WIDTH) / 2,
(GLFWvidmode.height(vidmode) - HEIGHT) / 2
);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
/*glfwSetWindowRefreshCallback(window, new GLFWWindowRefreshCallback() {
@Override
public void invoke(long window) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GL20.glUniform1f(time_loc, time);
if (time > 1.0f) {
time = -1.0f;
}
time += 0.005f;
GL15.glBindBuffer(GL_VERTEX_ARRAY, triangle);
GL20.glEnableVertexAttribArray(0);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 3 * 4, 0L);
GL11.glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
}
});*/
// Resized the viewport if the window is resized.
glfwSetFramebufferSizeCallback(window, framebufferCallback = new GLFWFramebufferSizeCallback() {
private boolean resize;
@Override
public void invoke(long window, int width, int height) {
resize = true;
WIDTH = width;
HEIGHT = height;
}
});
glfwSetWindowTitle(window, "Simple Test");
}
private void init() {
String vertexsource = "#version 330 core\n" +
"uniform mat4 projectionMatrix;\n" +
"uniform mat4 viewMatrix;\n" +
"uniform mat4 modelMatrix;\n" +
"layout (location = 0) in vec3 in_position;\n" +
"void main() {\n" +
" gl_Position = (projectionMatrix * viewMatrix * modelMatrix) * vec4(in_position, 1.0f);\n" +
"}\n";
String fragmentsource = "#version 330 core\n" +
"void main() {\n" +
"gl_FragColor = vec4(1.0f, 0.0, 1.0f, 1.0f);\n" +
"}\n";
int vertex = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
int fragment = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
GL20.glShaderSource(vertex, vertexsource);
GL20.glShaderSource(fragment, fragmentsource);
GL20.glCompileShader(vertex);
GL20.glCompileShader(fragment);
// Create program from shaders.
program = GL20.glCreateProgram();
GL20.glAttachShader(program, vertex);
GL20.glAttachShader(program, fragment);
GL20.glLinkProgram(program);
String info = GL20.glGetProgramInfoLog(program);
System.out.println("Program info: " + info);
// Set up data
float[] vboData = new float[]{0.0f, 0.0f, 0.0f};
FloatBuffer buffer = BufferUtils.createFloatBuffer(vboData.length);
buffer.put(vboData);
buffer.flip();
vbo_id = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
private void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10);
glEnableVertexAttribArray(0);
GL20.glUseProgram(vbo_id);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_id);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
glDrawArrays(GL_POINTS, 0, 1);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL20.glUseProgram(0);
glfwSwapBuffers(window);
}
public static void main(String[] args) {
new Main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.