Skip to content

Commit 315029f

Browse files
committed
WIP
1 parent 5fbd3d1 commit 315029f

11 files changed

Lines changed: 116 additions & 151 deletions

File tree

‎build.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- Project-specific configuration -->
66
<loadproperties srcfile="project.properties"/>
7-
<property name="main.class" value="${package.main}.${ant.project.name}"/>
7+
<property name="main.class" value="${package.main}.Launcher"/>
88
<property name="base.name" value="${artifactId}-${version}"/>
99

1010
<!-- Standard Directory Layout -->
@@ -53,7 +53,7 @@
5353
</target>
5454

5555
<target name="run" depends="compile" description="Run the application.">
56-
<java classname="${main.class}" classpath="${build.dir}/classes">
56+
<java classname="${main.class}" classpath="${build.dir}/classes" fork="yes">
5757
<classpath refid="build.classpath"/>
5858
<arg value="--name"/>
5959
<arg value="Java"/>

‎project.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
artifactId = sample-lwjgl-project
33
description = An Ant-based sample Java project.
44
version = 1.0-SNAPSHOT
5-
package.main = sample.java.project
5+
package.main = com.nullprogram.noise
66

77
# External information
88
groupId = com.nullprogram

‎src/com/nullprogram/lwjgl/GlBuffer.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88
* An OpenGL buffer object, the abstract base class. Because the LWJGL
99
* API depends highly on the <i>type</i> of the underlying Buffer, it
1010
* is generic and each subclass takes on a different Buffer subclass.
11-
* @param <T> the type of buffer backing this OpenGL buffer
1211
*/
13-
public abstract class GlBuffer<T extends Buffer> {
12+
public abstract class GlBuffer {
1413

1514
/** OpenGL handle for this buffer. */
1615
@Getter
1716
private final int handle;
1817

19-
/** Native buffer that backs this buffer. */
20-
@Getter
21-
private final T buffer;
22-
2318
/** True of this buffer has been disposed. */
2419
private boolean disposed = false;
2520

21+
/** Native buffer that backs this buffer. */
22+
@Getter
23+
private final Buffer buffer;
24+
2625
/**
2726
* Create a new buffer.
2827
* @param id the buffer's handle
2928
* @param data the native buffer backing this buffer
3029
*/
31-
protected GlBuffer(final int id, final T data) {
30+
protected GlBuffer(final int id, final Buffer data) {
3231
handle = id;
3332
buffer = data;
3433
}

‎src/com/nullprogram/lwjgl/GlByteBuffer.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* OpenGL buffer based on a ByteBuffer.
88
*/
9-
public class GlByteBuffer extends GlBuffer<ByteBuffer> {
9+
public class GlByteBuffer extends GlBuffer {
1010

1111
/**
1212
* Create a new OpenGL buffer for the given data. The usage hint
@@ -28,6 +28,6 @@ public GlByteBuffer(final ByteBuffer data,
2828
final int target, final int usage) {
2929
super(GL15.glGenBuffers(), data);
3030
GL15.glBindBuffer(target, getHandle());
31-
GL15.glBufferData(target, getBuffer(), usage);
31+
GL15.glBufferData(target, data, usage);
3232
}
3333
}

‎src/com/nullprogram/lwjgl/GlFloatBuffer.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* OpenGL buffer based on a FloatBuffer.
88
*/
9-
public class GlFloatBuffer extends GlBuffer<FloatBuffer> {
9+
public class GlFloatBuffer extends GlBuffer {
1010

1111
/**
1212
* Create a new OpenGL buffer for the given data. The usage hint
@@ -25,9 +25,9 @@ public GlFloatBuffer(final float[] data, final int target) {
2525
* @param usage usage hint for this buffer
2626
*/
2727
public GlFloatBuffer(final FloatBuffer data,
28-
final int target, final int usage) {
28+
final int target, final int usage) {
2929
super(GL15.glGenBuffers(), data);
3030
GL15.glBindBuffer(target, getHandle());
31-
GL15.glBufferData(target, getBuffer(), usage);
31+
GL15.glBufferData(target, data, usage);
3232
}
3333
}

‎src/com/nullprogram/lwjgl/Program.java‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.nullprogram.lwjgl;
22

3+
import java.nio.Buffer;
4+
import java.nio.FloatBuffer;
35
import java.util.HashSet;
46
import java.util.Set;
57
import lombok.Getter;
@@ -23,13 +25,19 @@ public Program add(Shader shader) {
2325
return this;
2426
}
2527

26-
public Program detach(Shader shader) {
28+
public void detach(Shader shader) {
2729
if (shaders.remove(shader)) {
28-
GL20.glDetachShader(shader.getHandle());
30+
GL20.glDetachShader(handle, shader.getHandle());
2931
}
3032
}
3133

32-
public Program attrib(GlBuffer buffer, String name) {
34+
public Program attrib(GlFloatBuffer buffer, String name, int size) {
35+
int attrib = GL20.glGetAttribLocation(handle, name);
36+
if (attrib < 1) {
37+
throw new IllegalStateException("Unknown attrib \"" + name + "\"");
38+
}
39+
GL20.glVertexAttribPointer(attrib, size, false, size,
40+
(FloatBuffer) buffer.getBuffer());
3341
return this;
3442
}
3543

@@ -43,6 +51,10 @@ public void link() throws LWJGLException {
4351
}
4452
}
4553

54+
public void run() {
55+
GL20.glUseProgram(handle);
56+
}
57+
4658
public void dispose() {
4759
if (!disposed) {
4860
GL20.glDeleteProgram(handle);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.nullprogram.noise;
2+
3+
import com.nullprogram.lwjgl.GlFloatBuffer;
4+
import com.nullprogram.lwjgl.Lwjgl;
5+
import com.nullprogram.lwjgl.Program;
6+
import com.nullprogram.lwjgl.Shader;
7+
import com.nullprogram.lwjgl.VertexShader;
8+
import com.nullprogram.lwjgl.FragmentShader;
9+
import java.net.URL;
10+
import java.nio.FloatBuffer;
11+
import lombok.SneakyThrows;
12+
import org.lwjgl.BufferUtils;
13+
import org.lwjgl.opengl.Display;
14+
import org.lwjgl.opengl.DisplayMode;
15+
import org.lwjgl.opengl.GL11;
16+
import org.lwjgl.opengl.GL15;
17+
18+
public class Launcher {
19+
20+
@SneakyThrows
21+
public static void main(String[] args) {
22+
try {
23+
/* Data */
24+
float[] positions = {1f, 1f, 1.5f, -0.5f};
25+
FloatBuffer magnitudes
26+
= BufferUtils.createFloatBuffer(positions.length / 2);
27+
28+
/* OpenGL initialization */
29+
Lwjgl.setup();
30+
Display.setDisplayMode(new DisplayMode(640, 480));
31+
Display.create();
32+
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
33+
34+
/* Prepare buffers */
35+
GlFloatBuffer glPositions =
36+
new GlFloatBuffer(positions, GL15.GL_ELEMENT_ARRAY_BUFFER);
37+
GlFloatBuffer glMagnitudes =
38+
new GlFloatBuffer(magnitudes, GL15.GL_ELEMENT_ARRAY_BUFFER,
39+
GL15.GL_DYNAMIC_DRAW);
40+
41+
/* Prepare shaders */
42+
URL magsrc = Launcher.class.getResource("magnitude.glsl");
43+
URL basesrc = Launcher.class.getResource("base.glsl");
44+
Shader magnitude = new VertexShader(magsrc);
45+
Shader base = new FragmentShader(basesrc);
46+
47+
/* Create program */
48+
Program program = new Program();
49+
program.add(magnitude);
50+
program.add(base);
51+
program.link();
52+
System.out.println("Link successful?");
53+
54+
/* Run it */
55+
program.attrib(glPositions, "position", 2);
56+
program.attrib(glMagnitudes, "magnitude", 1);
57+
program.run();
58+
System.out.println("Run successful?");
59+
for (magnitudes.position(0); magnitudes.remaining() > 0;) {
60+
System.out.println(magnitudes.get());
61+
}
62+
Thread.sleep(10000);
63+
} catch (Throwable t) {
64+
t.printStackTrace();
65+
}
66+
System.exit(0); // Call this or LWJGL crashes
67+
}
68+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 110
2+
3+
varying float magnitude;
4+
5+
void main()
6+
{
7+
gl_FragColor = vec4(magnitude, 0, 0, 0);
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 110
2+
3+
attribute vec2 position;
4+
5+
varying float magnitude;
6+
7+
void main()
8+
{
9+
gl_Position = vec4(position, 0, 0);
10+
magnitude = sqrt(position.x * position.x + position.y * position.y);
11+
}

‎src/sample/java/project/SampleLwjglProject.java‎

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)