Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# JRTR Base Code
This project contains base code developed for an introduction to computer graphics course taught by Matthias Zwicker at the University of Maryland, College Park ([CMSC427, Computer Graphics](https://cs.umd.edu/class)). This is educational code intended for students to learn about 3D graphics programming. The framework provides a skeleton for a real-time rendering engine.
This project contains base code developed for an introduction to computer graphics course taught by Matthias Zwicker at the University of Maryland, College Park ([CMSC427, Computer Graphics](https://cs.umd.edu/class)). This is educational code intended for students to learn about 3D graphics programming. The framework provides a skeleton for a real-time rendering engine. It is modular to support different rendering back-ends, coming with an OpenGL-based renderer, and a renderer using OpenGL and OpenVR to render onto VR goggles.

## Getting Started

Expand All @@ -15,6 +15,11 @@ After importing the project, you'll need to make two adjustments in order for th

After that, you're all set! You should be able to run the base code on your machine.

### For OpenVR sample
To run the "simpleVR" project, you must have access to a Windows PC with a VR-compatible dedicated graphics card and a tethered VR headset. First, you should install [SteamVR on Steam](https://store.steampowered.com/app/250820/SteamVR/) in order to obtain the OpenVR runtime on your computer. You should try running the SteamVR app to make sure there are no issues connecting to the VR headset. After that, you should be able to run the "simpleVR" project.

In some cases, your computer may default to running your Java project with your CPU's integrated graphics, which might cause nothing to render onto your VR headset's display. To fix this, you should force your computer to use the dedicated graphics card. On a computer with an NVIDIA GPU, this can be achieved using the NVIDIA Control Panel.

## Known issues

### Maven dependencies
Expand Down
11 changes: 10 additions & 1 deletion jrtr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<version>0.0.1-SNAPSHOT</version>

<properties>
<lwjgl.version>3.2.3</lwjgl.version>
<lwjgl.version>3.3.0</lwjgl.version>
</properties>

<profiles>
Expand Down Expand Up @@ -128,6 +128,10 @@
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-openvr</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-stb</artifactId>
Expand Down Expand Up @@ -157,6 +161,11 @@
<artifactId>lwjgl-opengl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-openvr</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-stb</artifactId>
Expand Down
41 changes: 41 additions & 0 deletions jrtr/src/main/java/jrtr/gldeferredrenderer/FrameBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package jrtr.gldeferredrenderer;

import static org.lwjgl.opengl.GL46.*;

/**
* A simple GLBuffer, which contains only one texture.
* This class can be used for ping pong buffers.
* @author Heinrich Reich
*
*/
public class FrameBuffer extends GLBuffer{


public FrameBuffer(int width, int height, boolean useDepthBuffer) throws RuntimeException{
super(width, height, 1, useDepthBuffer, GL_RGB8);
}

FrameBuffer(int width, int height, boolean useDepthBuffer, int format){
super(width, height, 1, useDepthBuffer, format);
}

/**
* Basically the same as {@link GLBuffer#beginRead(int)}.
*/
public void beginRead(){
super.beginRead(0);
}

@Override
protected void handleCreationError(boolean failed) throws RuntimeException{
if(failed)
throw new RuntimeException("Error occured while creating the framebuffer object!");
}

/**
* @return the internal OpenGL id of the texture.
*/
public int getRenderedTexture(){
return this.textures.get(0);
}
}
212 changes: 212 additions & 0 deletions jrtr/src/main/java/jrtr/gldeferredrenderer/GLBuffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package jrtr.gldeferredrenderer;

import java.nio.*;

import static org.lwjgl.opengl.GL46.*;
import static org.lwjgl.system.MemoryUtil.*;

/**
* A simple class for creating, reading, and writing to an OpenGL framebuffer object (FBO).
* FBOs are the key OpenGL data structures for writing (rendering) and reading from off-screen
* images and textures.
*
* @author Heinrich Reich
*
*/
public abstract class GLBuffer {

// references to the different OpenGL objects
public IntBuffer frameBuffer, drawBuffers, textures, depthBuffer;
// format and render target index
public final int format, renderTargets;
// temporary indices of read and written fbo
// width and height of the textures in this buffer
private int prevWriteFBO, prevReadFBO, width, height;
// whether to use depth buffer or not
public final boolean useDepthBuffer;

/**
* Creates a new OpenGL framebuffer object (FBO).
* @param gl the OpenGL context
* @param width the width of the textures
* @param height the height of the texture
* @param renderTargets number of textures/render targets
* @param useDepthBuffer whether to use a depth buffer
* @param format the format
*/
public GLBuffer(int width, int height, int renderTargets, boolean useDepthBuffer, int format){
this.renderTargets = renderTargets;
this.width = width;
this.height = height;
this.format = format;
this.useDepthBuffer = useDepthBuffer;
this.init();
}

/**
* Creates a new OpenGL framebuffer object (FBO) with a default GL3.GL_RGB8 format.
* @param gl
* @param width
* @param height
* @param renderTargets
* @param useDepthBuffer
*/
public GLBuffer(int width, int height, int renderTargets, boolean useDepthBuffer){
this(width, height, renderTargets, useDepthBuffer, GL_RGB8);
}

/**
* Initialize the OpenGL framebuffer object (FBO). First we generate a framebuffer object
* and bind it as the current buffer. Then we create as many textures as specified in
* {@link GLBuffer#GLBuffer}. If needed, we also create a depth buffer for the render target.
* Later (after we rendered into the FBO), we can read from these textures via the
* usual OpenGL functionality.
* The abstract method {@link GLBuffer#handleCreationError} gets called here.
*/
private void init(){
this.frameBuffer = memAllocInt(1);
this.textures = memAllocInt(renderTargets);
this.drawBuffers = memAllocInt(renderTargets);

// Generate a reference to a FBO and bind it ("activate it")
glGenFramebuffers(frameBuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer.get(0));

// Create references for the FBO textures
glGenTextures(textures);

// Attach textures to the FBO
for (int i = 0 ; i < textures.capacity() ; i++) {
glBindTexture(GL_TEXTURE_2D, this.textures.get(i));
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_RGBA, GL_FLOAT, (FloatBuffer) null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Each texture will be linked to a buffer index i; the corresponding index is used in the shader
// using the directive layout(location = i) to address this texture
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, this.textures.get(i), 0);
}

// Depth texture
if(this.useDepthBuffer) this.createDepthBuffer();

for(int i = 0; i< drawBuffers.capacity(); i++)
drawBuffers.put(i, GL_COLOR_ATTACHMENT0+i);
glDrawBuffers(drawBuffers);

this.handleCreationError(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE);

// Bind default render and frame buffer ("deactivate" the FBO we just created)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}

/**
* Resizes the frame buffer, i.e. destroys the buffer and its textures,
* and re-initializes the buffer with the new dimensions.
* @param width
* @param height
*/
public void resize(int width, int height){
this.width = width;
this.height = height;
this.dispose();
this.init();
}

/**
* Gets called after the initialization.
* @param failed <code>true</code> if an error occurred during initialization step.
*/
protected abstract void handleCreationError(boolean failed);

/**
* Binds ("activate") this FBO, so any rendering calls will draw into this FBO.
* Use {@link GLBuffer#endWrite()} when you are finished with writing.
*/
public void beginWrite(){
// Store current FBO to restore when done with writing
this.prevWriteFBO = glGetInteger(GL_DRAW_FRAMEBUFFER);

// Bind this FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer.get(0));
glViewport(0, 0, width, height);
}

/**
* Unbinds this framebuffer, i.e. binds the previous buffer again.
* Only use this method if you called {@link GLBuffer#beginWrite()} before.
*/
public void endWrite(){
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this.prevWriteFBO);
}

/**
* Begins reading from a texture from this FBO by binding ("activating") it.
* Use {@link GLBuffer#endRead()} if you are done with reading.
* @param i the id of the wished texture.
*/
public void beginRead(int i){
if(this.prevReadFBO != this.frameBuffer.get(0)){
this.prevReadFBO = glGetInteger(GL_READ_FRAMEBUFFER);
glBindFramebuffer(GL_READ_FRAMEBUFFER, frameBuffer.get(0));
}
glReadBuffer(GL_COLOR_ATTACHMENT0+i);
}

/**
* Ends reading from this buffer.
* Only use this method if you called {@link GLBuffer#beginRead(int)} before.
*/
public void endRead(){
glBindFramebuffer(GL_READ_FRAMEBUFFER, this.prevReadFBO);
}

/**
* Does all OpenGL calls for creating a depth buffer for this buffer.
*/
private void createDepthBuffer(){
this.depthBuffer = memAllocInt(1);
glGenTextures(this.depthBuffer);
glBindTexture(GL_TEXTURE_2D, this.depthBuffer.get(0));
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (FloatBuffer) null);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, this.depthBuffer.get(0), 0);
}

/**
* Disposes this buffer and its textures, i.e. releases all memory.
*/
public void dispose(){
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
if(this.useDepthBuffer) glDeleteTextures(depthBuffer);
glDeleteTextures(textures);
glDeleteFramebuffers(frameBuffer);
memFree(frameBuffer);
memFree(textures);
if(this.useDepthBuffer) memFree(depthBuffer);
}

/**
* @return the current set width.
*/
public int getWidth(){
return this.width;
}

/**
* @return the current set height.
*/
public int getHeight(){
return this.height;
}

/**
* @param index
* @return the internal texture index in OpenGL, need for passing textures to a shader.
*/
public int getTexture(int index){
return this.textures.get(index);
}

}
4 changes: 4 additions & 0 deletions jrtr/src/main/java/jrtr/gldeferredrenderer/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* An OpenGL renderer with deferred shading implementing the {@link jrtr} interfaces.
*/
package jrtr.gldeferredrenderer;
18 changes: 8 additions & 10 deletions jrtr/src/main/java/jrtr/glrenderer/GLVertexArrayObject.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package jrtr.glrenderer;

import java.nio.IntBuffer;
import java.nio.*;

import static org.lwjgl.opengl.GL45.*;

//import com.jogamp.opengl.GL3;
import static org.lwjgl.opengl.GL46.*;
import static org.lwjgl.system.MemoryUtil.*;

/**
* A utility class to encapsulate an OpenGL "vertex array object" (VAO).
Expand All @@ -13,9 +12,7 @@ public class GLVertexArrayObject {

private IntBuffer vao;
private IntBuffer vbo;

// private GL3 gl;


/**
* Make an OpenGL "vertex array object" (VAO) with a desired number of
* "vertex buffer objects" (VBOs). Each VBO refers to a buffer
Expand All @@ -26,16 +23,15 @@ public class GLVertexArrayObject {
* the number of VBOs to be stored in the VAO
*/
public GLVertexArrayObject(int numberOfVBOs) {

// For all vertex attributes, make vertex buffer objects.
// References to the VBOs are stored in the array vbo.
vbo = IntBuffer.allocate(numberOfVBOs);
vbo = memAllocInt(numberOfVBOs);
for(int i=0; i<numberOfVBOs;i++)
vbo.put(i, glGenBuffers());

// Make a vertex array object. A reference to the VAO
// is stored in the array vao.
vao = IntBuffer.allocate(1);
vao = memAllocInt(1);
vao.put(0, glGenVertexArrays());
}

Expand Down Expand Up @@ -69,5 +65,7 @@ public void dispose(){
glBindBuffer(0, 0);
glDeleteBuffers(vbo);
glDeleteVertexArrays(vao);
memFree(vao);
memFree(vbo);
}
}
Loading