Skip to content

Instantly share code, notes, and snippets.

@jherico
Created May 18, 2014 01:45
Show Gist options
  • Save jherico/b35ba72bd2cb1ff69adb to your computer and use it in GitHub Desktop.
Save jherico/b35ba72bd2cb1ff69adb to your computer and use it in GitHub Desktop.
package org.saintandreas.vr;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.saintandreas.ExampleResource;
import org.saintandreas.gl.IndexedGeometry;
import org.saintandreas.gl.MatrixStack;
import org.saintandreas.gl.buffers.IndexBuffer;
import org.saintandreas.gl.buffers.VertexArray;
import org.saintandreas.gl.shaders.Attribute;
import org.saintandreas.gl.shaders.Program;
import org.saintandreas.gl.textures.Texture;
import org.saintandreas.math.Vector2f;
import org.saintandreas.math.Vector3f;
import org.saintandreas.math.Vector4f;
import org.saintandreas.vr.oculus.RiftApp;
import com.oculusvr.capi.OvrLibrary;
public class RiftDemo extends RiftApp {
Program program;
IndexedGeometry geometry;
Texture texture;
public static IndexedGeometry makeTexturedQuad() {
Vector2f min = new Vector2f(-1.5f, -1.5f);
Vector2f max = new Vector2f(1.5f, 1.5f);
Vector2f texMin = new Vector2f(0, 0);
Vector2f texMax = new Vector2f(1, 1);
List<Vector4f> vertices = new ArrayList<>();
vertices.add(new Vector4f(min.x, min.y, 0, 1));
vertices.add(new Vector4f(texMin.x, texMin.y, 0, 0));
vertices.add(new Vector4f(max.x, min.y, 0, 1));
vertices.add(new Vector4f(texMax.x, texMin.y, 0, 0));
vertices.add(new Vector4f(max.x, max.y, 0, 1));
vertices.add(new Vector4f(texMax.x, texMax.y, 0, 0));
vertices.add(new Vector4f(min.x, max.y, 0, 1));
vertices.add(new Vector4f(texMin.x, texMax.y, 0, 0));
List<Short> indices = new ArrayList<>();
indices.add((short) 0); // LL
indices.add((short) 1); // LR
indices.add((short) 3); // UL
indices.add((short) 2); // UR
IndexedGeometry.Builder builder = new IndexedGeometry.Builder(indices, vertices);
builder.withDrawType(GL_TRIANGLE_STRIP).withAttribute(Attribute.POSITION).withAttribute(Attribute.TEX);
return builder.build();
}
@Override
protected void initGl() {
super.initGl();
MatrixStack.MODELVIEW.lookat(Vector3f.UNIT_X.scale(ipd * 5), // eye position
Vector3f.ZERO, // origin of the scene
Vector3f.UNIT_Y); // up direction
program = new Program(ExampleResource.SHADERS_TEXTURED_VS, ExampleResource.SHADERS_TEXTURED_FS);
program.link();
geometry = makeTexturedQuad();
//geometry = OpenGL.makeColorCube();
try {
texture = Texture.loadImage(new File("C:\\Users\\bdavis\\Dropbox\\Pictures\\vlcsnap-2013-07-27-18h49m35s156.png").toURI().toURL());
} catch (IOException e) {
throw new IllegalStateException(e);
}
texture.bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
texture.unbind();
}
@Override
protected void update() {
MatrixStack.MODELVIEW.lookat(Vector3f.UNIT_Z.scale(ipd * 5), // eye position
Vector3f.ZERO, // origin of the scene
Vector3f.UNIT_Y); // up direction
}
@Override
public void renderScene() {
glUseProgram(0);
glEnable(GL_DEPTH_TEST);
glClearColor(0.2f, 0.2f, 0.2f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glDisable(GL_DEPTH_TEST);
// glPointSize(10);
// glBegin(GL_POINTS);
// glColor3f(1, 1, 1);
// glVertex3f(0, 0, 0);
// glEnd();
MatrixStack mv = MatrixStack.MODELVIEW;
mv.push();
{
mv.scale(OvrLibrary.OVR_DEFAULT_IPD);
program.use();
MatrixStack.bindAll(program);
texture.bind();
geometry.bindVertexArray();
geometry.draw();
IndexBuffer.unbind();
VertexArray.unbind();
Program.clear();
}
mv.pop();
}
public static void main(String[] args) {
new RiftDemo().run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment