-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-Added PixelShader class! (is called to shade when a pixel in the Camera is set) -Added Shader class! (is called to shade the vertexes and polygons by the SceneRenderer)
- Loading branch information
John
committed
Sep 5, 2018
1 parent
7ccd8de
commit a85dc46
Showing
15 changed files
with
255 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/johnsproject/jpge/graphics/PixelShader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.johnsproject.jpge.graphics; | ||
|
||
import com.johnsproject.jpge.utils.Vector2Utils; | ||
|
||
/** | ||
* The PixelShader class is used to used shade the pixels that are set to the {@link Camera}. | ||
* | ||
* @author John´s Project - John Konrad Ferraz Salomon | ||
*/ | ||
public class PixelShader { | ||
|
||
/** | ||
* This method is called by the {@link Camera} when a pixel is set. | ||
* | ||
* @param x position of pixel in the x axis. | ||
* @param y position of pixel in the y axis. | ||
* @param color color of pixel. | ||
* @param cameraWidth the width of the camera at the screen, used when to calculate pixel position in the viewBuffer. | ||
* @param viewBuffer buffer containing pixels of the {@link Camera}. | ||
*/ | ||
public void shadePixel (int x, int y, int color, int cameraWidth, int[] viewBuffer) { | ||
setPixel(x, y, color, cameraWidth, viewBuffer); | ||
} | ||
|
||
/** | ||
* Sets the color of the pixel at the given position in the viewBuffer equals to the given color. | ||
* | ||
* @param x position of pixel in the x axis. | ||
* @param y position of pixel in the y axis. | ||
* @param color color of pixel. | ||
* @param cameraWidth the width of the camera at the screen, used when to calculate pixel position in the viewBuffer. | ||
* @param viewBuffer buffer containing pixels of the {@link Camera}. | ||
*/ | ||
public void setPixel(int x, int y, int color, int cameraWidth, int[] viewBuffer) { | ||
int pos = x + (y * Vector2Utils.getX(cameraWidth)); | ||
if (pos >= 0 && pos < viewBuffer.length) viewBuffer[pos] = color; | ||
} | ||
|
||
/** | ||
* Returns the color of the pixel at the given position in the viewBuffer. | ||
* | ||
* @param x position of pixel in the x axis. | ||
* @param y position of pixel in the y axis. | ||
* @param cameraWidth the width of the camera at the screen, used when to calculate pixel position in the viewBuffer. | ||
* @param viewBuffer buffer containing pixels of the {@link Camera}. | ||
* @return color of a pixel of the viewBuffer at the given position. | ||
*/ | ||
public int getPixel(int x, int y, int cameraWidth, int[] viewBuffer) { | ||
int pos = x + (y * Vector2Utils.getX(cameraWidth)); | ||
if (pos >= 0 && pos < viewBuffer.length) return viewBuffer[pos]; | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.johnsproject.jpge.graphics; | ||
|
||
import java.util.List; | ||
|
||
import com.johnsproject.jpge.utils.VectorMathUtils; | ||
import com.johnsproject.jpge.utils.VertexUtils; | ||
|
||
/** | ||
* The Shader class is used to used shade the vertexes and the polygons that are being rendered by the {@link SceneRenderer}. | ||
* | ||
* @author John´s Project - John Konrad Ferraz Salomon | ||
*/ | ||
public class Shader { | ||
|
||
/** | ||
* This method by the {@link SceneRenderer} at the rendering process. | ||
* | ||
* @param vertex vertex to shade. | ||
* @param sceneObjectTransform {@link Transform} of the {@link SceneObject} this vertex belongs to. | ||
* @param camera {@link Camera} being rendered to. | ||
* @param lights All {@link Light Lights} of the {@link Scene} being rendered. | ||
* @return shaded vertex. | ||
*/ | ||
public long shadeVertex(long vertex, Transform sceneObjectTransform, Camera camera, List<Light> lights) { | ||
Transform objt = sceneObjectTransform; | ||
Transform camt = camera.getTransform(); | ||
long vector = VertexUtils.getVector(vertex); | ||
//transforming vertex in object space | ||
vector = VectorMathUtils.movePointByScale(vector, objt.getScale()); | ||
vector = VectorMathUtils.movePointByAnglesXYZ(vector, objt.getRotation()); | ||
vector = VectorMathUtils.add(vector, objt.getPosition()); | ||
//transforming vertex in camera space | ||
vector = VectorMathUtils.subtract(vector, camt.getPosition()); | ||
vector = VectorMathUtils.movePointByAnglesXYZ(vector, camt.getRotation()); | ||
//projecting vector to screen coordinates | ||
vector = SceneRenderer.projectVertex(vector, objt.getPosition(), camera); | ||
vertex = VertexUtils.setVector(vertex, vector); | ||
return vertex; | ||
} | ||
|
||
/** | ||
* This method by the {@link SceneRenderer} at the rendering process. | ||
* | ||
* @param polygon polygon to shade. | ||
* @param mesh {@link Mesh} this polygon belongs to. | ||
* @param camera {@link Camera} being rendered to. | ||
* @return shaded polygon. | ||
*/ | ||
public int[] shadePolygon(int[] polygon, Mesh mesh, Camera camera) { | ||
polygon[Mesh.CULLED] = 1; | ||
if (!SceneRenderer.cullViewFrustum(polygon, mesh, camera)) { | ||
if (!SceneRenderer.cullBackface(polygon, mesh)) { | ||
polygon[Mesh.CULLED] = 0; | ||
} | ||
} | ||
return polygon; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.