Skip to content

Commit

Permalink
Updating methods as per new method signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Rex Infernum committed Dec 5, 2023
1 parent 786e8ab commit 30ce6ce
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 32 deletions.
28 changes: 12 additions & 16 deletions jsgl/src/main/java/jsgl/jogl/FrameBufferObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* an FBO, it must be bound as the current Framebuffer.
*
* @author Justin Stoecker
* @see http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt
* @see http://www.songho.ca/opengl/gl_fbo.html
* @see <a href="http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt">...</a>
* @see <a href="http://www.songho.ca/opengl/gl_fbo.html">...</a>
*/
public class FrameBufferObject implements GLDisposable
{
Expand All @@ -45,6 +45,10 @@ public class FrameBufferObject implements GLDisposable
private int texWidth;
private int texHeight;

public static FrameBufferObject create(GL2 gl, int w, int h, int glRgb) {
return null;
}

public int getID()
{
return id;
Expand Down Expand Up @@ -91,15 +95,10 @@ public static FrameBufferObject generate(GL gl)
* @param internalFormat -
* @return Returns a Framebuffer Objects if successful; null otherwise
*/
public static FrameBufferObject create(GL gl, int w, int h, int internalFormat)
{
public static FrameBufferObject create(GL gl, int w, int h, int internalFormat, Texture2D.Texture2DBuilder colorTexBuilder) {
Texture2D colorTex = Texture2D.generate(gl);
colorTex.bind(gl);
Texture2D.setParameter(gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
Texture2D.setParameter(gl, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
Texture2D.setParameter(gl, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
Texture2D.setParameter(gl, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
colorTex.texImage(gl, 0, internalFormat, w, h, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, null);
colorTexBuilder.build(gl, colorTex);
Texture2D.unbind(gl);

RenderBuffer depthBuffer = RenderBuffer.createDepthBuffer(gl, w, h);
Expand All @@ -120,16 +119,12 @@ public static FrameBufferObject create(GL gl, int w, int h, int internalFormat)
return fbo;
}


/** Creates an FBO with a color texture attachment and no depth attachment */
public static FrameBufferObject createNoDepth(GL gl, int w, int h, int internalFormat)
{
public static FrameBufferObject createNoDepth(GL gl, int w, int h, int internalFormat, Texture2D.Texture2DBuilder colorTexBuilder) {
Texture2D colorTex = Texture2D.generate(gl);
colorTex.bind(gl);
Texture2D.setParameter(gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
Texture2D.setParameter(gl, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
Texture2D.setParameter(gl, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
Texture2D.setParameter(gl, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
colorTex.texImage(gl, 0, internalFormat, w, h, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, null);
colorTexBuilder.build(gl, colorTex);
Texture2D.unbind(gl);

FrameBufferObject fbo = FrameBufferObject.generate(gl);
Expand All @@ -148,6 +143,7 @@ public static FrameBufferObject createNoDepth(GL gl, int w, int h, int internalF
return fbo;
}


/**
* Creates a Framebuffer Object that can be used for offscreen rendering to
* a texture of a specified with and height. This FBO is setup for
Expand Down
2 changes: 1 addition & 1 deletion jsgl/src/main/java/jsgl/jogl/Texture2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Texture2DBuilder data(Buffer data) {
}

public void build(GL gl, Texture2D texture) {
texture.texImage(gl, level, internalFormat, width, height, border, format, type, data);
texture.texImage(gl, this);
}
}

Expand Down
11 changes: 3 additions & 8 deletions jsgl/src/main/java/jsgl/jogl/model/ObjMaterialLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

package jsgl.jogl.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.ArrayList;

/**
Expand Down Expand Up @@ -65,7 +61,7 @@ private LineType getLineType(String line) {
return LineType.UNKNOWN;
}

private void processLine(LineType lineType, String line, String texturePath, ClassLoader cl, ObjMaterial currentMaterial) {
private void processLine(LineType lineType, String line, String texturePath, ClassLoader cl, ObjMaterial currentMaterial) throws IOException {
switch (lineType) {
case NEW_MATERIAL:
// new material definition, so current material is finished
Expand Down Expand Up @@ -95,12 +91,11 @@ private void processLine(LineType lineType, String line, String texturePath, Cla
processTextureMap(line, texturePath, cl, currentMaterial);
break;
case UNKNOWN:
// Handle unknown line type if needed
break;
}
}

private void processTextureMap(String line, String texturePath, ClassLoader cl, ObjMaterial currentMaterial) {
private void processTextureMap(String line, String texturePath, ClassLoader cl, ObjMaterial currentMaterial) throws IOException {
String textureName = line.split("\\s+")[1];
InputStream is = null;
if (cl != null) {
Expand Down
49 changes: 45 additions & 4 deletions jsgl/src/main/java/jsgl/jogl/model/ObjModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.io.*;
import java.util.ArrayList;
import jsgl.jogl.GLDisposable;
import jsgl.math.BoundingBox;
Expand Down Expand Up @@ -200,6 +198,49 @@ public static ObjModel load(File file) throws IOException
return model;
}

private static ObjGroup processGroup(String line, ObjModel model, ObjGroup currentGroup) {
if (currentGroup != null) model.groups.add(currentGroup);
return new ObjGroup(line.split("\\s+")[1]);
}


private static void processUseMaterial(String line, ObjModel model) {
String requestedMaterial = line.split("\\s+")[1];
ObjMaterial currentMaterial;
for (ObjMaterial mat : model.mtllib.materials)
if (mat.name.equals(requestedMaterial))
currentMaterial = mat;
}

private static void processMaterialLibrary(String line, File file, ObjModel model) throws IOException {
File f = new File(file.getParent(), line.split("\\s+")[1]);
if (model.mtllib == null) model.mtllib = new ObjMaterialLibrary();
BufferedReader br2 = new BufferedReader(new FileReader(f));
file.getParentFile();
model.mtllib.load(br2, file.getParent(), null);
// TODO: can files have multiple material libraries?
}


private static void processFaceLine(String line, ObjGroup currentGroup, ObjMaterial currentMaterial, ObjModel model) {
if (currentGroup == null) currentGroup = new ObjGroup("Unnamed Default Group");
currentGroup.faces.add(new Face(line, currentMaterial));
}


private static void processVertexLine(String line, ObjModel model, Vec3f min, Vec3f max) {
float[] vert = readFloatValues(line);
if (vert.length > 2) {
if (vert[0] > max.x) max.x = vert[0];
if (vert[1] > max.y) max.y = vert[1];
if (vert[2] > max.z) max.z = vert[2];
if (vert[0] < min.x) min.x = vert[0];
if (vert[1] < min.y) min.y = vert[1];
if (vert[2] < min.z) min.z = vert[2];
}
model.verts.add(vert);
}

private static LineType getLineType(String line) {
if (line.startsWith("v ")) return LineType.VERTEX;
if (line.startsWith("vn ")) return LineType.NORMAL;
Expand Down
33 changes: 33 additions & 0 deletions jsgl/src/main/java/jsgl/math/vector/Vec3f.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package jsgl.math.vector;

import java.nio.FloatBuffer;

import jsgl.math.Plane;
import jsgl.math.Tuplef;

/**
Expand Down Expand Up @@ -72,6 +74,37 @@ public Vec3f(float v)
this.z = v;
}

public static Vec3f intersectInfiniteLineWithPlane(Vec3f a, Vec3f b, Plane p) {
Vec3f n = p.getNormal();
Vec3f d = b.minus(a);

// ray's direction is parallel to surface of the plane
float nDotD = n.dot(d);
if (nDotD == 0)
return null;

float t = (n.dot(p.getPoint()) - n.dot(a)) / nDotD;

return a.plus(d.times(t));
}

public static Vec3f intersectLineSegmentWithPlane(Vec3f a, Vec3f b, Plane p) {
Vec3f n = p.getNormal();
Vec3f d = b.minus(a);

// ray's direction is parallel to surface of the plane
float nDotD = n.dot(d);
if (nDotD == 0)
return null;

float t = (n.dot(p.getPoint()) - n.dot(a)) / nDotD;

if (t >= 0 && t <= 1)
return a.plus(d.times(t));

return null;
}

/**
* Returns the result of the 3D dot product of this Vec3f and that Vec3f
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rv/world/rendering/ShadowMapRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private Texture2D createTexture(GL2 gl)
Texture2D.setParameter(gl, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
Texture2D.setParameter(gl, GL.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE);
Texture2D.setParameter(gl, GL.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE);
tex.texImage(gl, 0, TEX_FORMAT, texWidth, texHeight, 0, GL2.GL_RGBA, GL2.GL_FLOAT, null);
tex.texImage(gl, new Texture2D.Texture2DBuilder());
Texture2D.unbind(gl);

return tex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ class Renderer : GLProgram(MainWindow.instance.width, MainWindow.instance.height
private fun genFBO(gl: GL2, vp: Viewport) {
if (numSamples > 0) {
msSceneFBO = FrameBufferObject.create(gl, vp.w, vp.h, GL.GL_RGBA, numSamples)
sceneFBO = FrameBufferObject.createNoDepth(gl, vp.w, vp.h, GL.GL_RGB8)
sceneFBO = FrameBufferObject.createNoDepth(gl, vp.w, vp.h, GL.GL_RGB8, null);
} else {
sceneFBO = FrameBufferObject.create(gl, vp.w, vp.h, GL.GL_RGB)
sceneFBO = FrameBufferObject.create(gl, vp.w, vp.h, GL.GL_RGB, null);
}
}

Expand Down

0 comments on commit 30ce6ce

Please sign in to comment.