[DEV] tutorial 23 'texture atlas' implementation
This commit is contained in:
parent
e31adfe7c6
commit
4069fd6ae0
BIN
res/fern_atlas.png
Normal file
BIN
res/fern_atlas.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 577 KiB |
@ -66,8 +66,9 @@ public class MainGameLoop {
|
|||||||
flowerModel.getTexture().setUseFakeLighting(true);
|
flowerModel.getTexture().setUseFakeLighting(true);
|
||||||
|
|
||||||
TexturedModel fernModel = new TexturedModel(OBJLoader.loadObjModel("fern", loader),
|
TexturedModel fernModel = new TexturedModel(OBJLoader.loadObjModel("fern", loader),
|
||||||
new ModelTexture(loader.loadTexture("fern")));
|
new ModelTexture(loader.loadTexture("fern_atlas")));
|
||||||
fernModel.getTexture().setHasTransparency(true);
|
fernModel.getTexture().setHasTransparency(true);
|
||||||
|
fernModel.getTexture().setNumberOfRows(2);
|
||||||
|
|
||||||
Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1));
|
Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1));
|
||||||
|
|
||||||
@ -93,11 +94,12 @@ public class MainGameLoop {
|
|||||||
new Vector3f(x, y, z),
|
new Vector3f(x, y, z),
|
||||||
new Vector3f(0,0,0),3));
|
new Vector3f(0,0,0),3));
|
||||||
}
|
}
|
||||||
for (int iii=0; iii<500; iii++) {
|
for (int iii=0; iii<5000; iii++) {
|
||||||
float x = random.nextFloat()*800 - 400;
|
float x = random.nextFloat()*800 - 400;
|
||||||
float z = random.nextFloat() * -600;
|
float z = random.nextFloat() * -600;
|
||||||
float y = terrain.getHeightOfTerrain(x, z);
|
float y = terrain.getHeightOfTerrain(x, z);
|
||||||
entities.add(new Entity(fernModel,
|
entities.add(new Entity(fernModel,
|
||||||
|
random.nextInt(4),
|
||||||
new Vector3f(x, y, z),
|
new Vector3f(x, y, z),
|
||||||
new Vector3f(0,0,0),0.6f));
|
new Vector3f(0,0,0),0.6f));
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,31 @@ public class Entity {
|
|||||||
private Vector3f position;
|
private Vector3f position;
|
||||||
private Vector3f rotation;
|
private Vector3f rotation;
|
||||||
private float scale;
|
private float scale;
|
||||||
|
private int textureIndex = 0;
|
||||||
public Entity(TexturedModel model, Vector3f position, Vector3f rotation, float scale) {
|
public Entity(TexturedModel model, Vector3f position, Vector3f rotation, float scale) {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.rotation = rotation;
|
this.rotation = rotation;
|
||||||
this.scale = scale;
|
this.scale = scale;
|
||||||
}
|
}
|
||||||
|
public Entity(TexturedModel model, int textureIndex, Vector3f position, Vector3f rotation, float scale) {
|
||||||
|
this.model = model;
|
||||||
|
this.textureIndex = textureIndex;
|
||||||
|
this.position = position;
|
||||||
|
this.rotation = rotation;
|
||||||
|
this.scale = scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getTextureXOffset() {
|
||||||
|
int column = textureIndex%model.getTexture().getNumberOfRows();
|
||||||
|
return (float)column/(float)model.getTexture().getNumberOfRows();
|
||||||
|
}
|
||||||
|
public float getTextureYOffset() {
|
||||||
|
int row = textureIndex/model.getTexture().getNumberOfRows();
|
||||||
|
return (float)row/(float)model.getTexture().getNumberOfRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void increasePosition(float dx, float dy, float dz) {
|
public void increasePosition(float dx, float dy, float dz) {
|
||||||
this.position = new Vector3f(position.x + dx, position.y + dy, position.z + dz);
|
this.position = new Vector3f(position.x + dx, position.y + dy, position.z + dz);
|
||||||
}
|
}
|
||||||
|
@ -28,4 +28,5 @@ public class TexturedModel {
|
|||||||
this.texture = texture;
|
this.texture = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,7 @@ public class EntityRenderer {
|
|||||||
GL20.glEnableVertexAttribArray(1);
|
GL20.glEnableVertexAttribArray(1);
|
||||||
GL20.glEnableVertexAttribArray(2);
|
GL20.glEnableVertexAttribArray(2);
|
||||||
ModelTexture texture = model.getTexture();
|
ModelTexture texture = model.getTexture();
|
||||||
|
shader.loadNumberOfRows(texture.getNumberOfRows());
|
||||||
if (texture.isHasTransparency()) {
|
if (texture.isHasTransparency()) {
|
||||||
MasterRenderer.disableCulling();
|
MasterRenderer.disableCulling();
|
||||||
}
|
}
|
||||||
@ -78,6 +79,7 @@ public class EntityRenderer {
|
|||||||
private void prepareInstance(Entity entity) {
|
private void prepareInstance(Entity entity) {
|
||||||
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotation(), entity.getScale());
|
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotation(), entity.getScale());
|
||||||
shader.loadTransformationMatrix(transformationMatrix);
|
shader.loadTransformationMatrix(transformationMatrix);
|
||||||
|
shader.loadOffset(entity.getTextureXOffset(), entity.getTextureYOffset());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import java.io.IOException;
|
|||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
|
|
||||||
import org.atriaSoft.etk.math.Matrix4f;
|
import org.atriaSoft.etk.math.Matrix4f;
|
||||||
|
import org.atriaSoft.etk.math.Vector2f;
|
||||||
import org.atriaSoft.etk.math.Vector3f;
|
import org.atriaSoft.etk.math.Vector3f;
|
||||||
|
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
@ -68,6 +69,9 @@ public abstract class ShaderProgram {
|
|||||||
protected void loadVector(int location, Vector3f value) {
|
protected void loadVector(int location, Vector3f value) {
|
||||||
GL20.glUniform3f(location, value.x, value.y, value.z);
|
GL20.glUniform3f(location, value.x, value.y, value.z);
|
||||||
}
|
}
|
||||||
|
protected void loadVector(int location, Vector2f value) {
|
||||||
|
GL20.glUniform2f(location, value.x, value.y);
|
||||||
|
}
|
||||||
|
|
||||||
protected void loadBoolean(int location, boolean value) {
|
protected void loadBoolean(int location, boolean value) {
|
||||||
//System.out.println("set value " + value + " " + (value==true?1.0f:0.0f));
|
//System.out.println("set value " + value + " " + (value==true?1.0f:0.0f));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package shaders;
|
package shaders;
|
||||||
|
|
||||||
import org.atriaSoft.etk.math.Matrix4f;
|
import org.atriaSoft.etk.math.Matrix4f;
|
||||||
|
import org.atriaSoft.etk.math.Vector2f;
|
||||||
import org.atriaSoft.etk.math.Vector3f;
|
import org.atriaSoft.etk.math.Vector3f;
|
||||||
|
|
||||||
import entities.Camera;
|
import entities.Camera;
|
||||||
@ -20,6 +21,8 @@ public class StaticShader extends ShaderProgram {
|
|||||||
private int location_shineDamper;
|
private int location_shineDamper;
|
||||||
private int location_useFakeLighting;
|
private int location_useFakeLighting;
|
||||||
private int location_skyColor;
|
private int location_skyColor;
|
||||||
|
private int location_numberOfRows;
|
||||||
|
private int location_offset;
|
||||||
|
|
||||||
public StaticShader() {
|
public StaticShader() {
|
||||||
super(VERTEX_FILE, FRAGMENT_FILE);
|
super(VERTEX_FILE, FRAGMENT_FILE);
|
||||||
@ -43,6 +46,17 @@ public class StaticShader extends ShaderProgram {
|
|||||||
location_shineDamper = super.getUniformLocation("shineDamper");
|
location_shineDamper = super.getUniformLocation("shineDamper");
|
||||||
location_useFakeLighting = super.getUniformLocation("useFakeLighting");
|
location_useFakeLighting = super.getUniformLocation("useFakeLighting");
|
||||||
location_skyColor = super.getUniformLocation("skyColor");
|
location_skyColor = super.getUniformLocation("skyColor");
|
||||||
|
location_numberOfRows = super.getUniformLocation("numberOfRows");
|
||||||
|
location_offset = super.getUniformLocation("offset");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void loadNumberOfRows(int numberOfRows) {
|
||||||
|
super.loadFloat(location_numberOfRows, numberOfRows);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadOffset(float x, float y) {
|
||||||
|
super.loadVector(location_offset, new Vector2f(x,y));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadSkyColour(Vector3f color) {
|
public void loadSkyColour(Vector3f color) {
|
||||||
|
@ -18,6 +18,9 @@ uniform vec3 lightPosition;
|
|||||||
|
|
||||||
uniform float useFakeLighting;
|
uniform float useFakeLighting;
|
||||||
|
|
||||||
|
uniform float numberOfRows;
|
||||||
|
uniform vec2 offset;
|
||||||
|
|
||||||
const float density = 0.007;
|
const float density = 0.007;
|
||||||
const float gradient = 1.5;
|
const float gradient = 1.5;
|
||||||
|
|
||||||
@ -26,7 +29,7 @@ void main(void) {
|
|||||||
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
|
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
|
||||||
vec4 positionRelativeToCam = viewMatrix * worldPosition;
|
vec4 positionRelativeToCam = viewMatrix * worldPosition;
|
||||||
gl_Position = projectionMatrix * positionRelativeToCam;
|
gl_Position = projectionMatrix * positionRelativeToCam;
|
||||||
pass_textureCoordinates = textureCoords;
|
pass_textureCoordinates = (textureCoords/numberOfRows) + offset;
|
||||||
|
|
||||||
vec3 actualNormal = normal;
|
vec3 actualNormal = normal;
|
||||||
if (useFakeLighting > 0.5) {
|
if (useFakeLighting > 0.5) {
|
||||||
|
@ -10,6 +10,7 @@ public class ModelTexture {
|
|||||||
private float shineDamper = 1;
|
private float shineDamper = 1;
|
||||||
private boolean hasTransparency = false;
|
private boolean hasTransparency = false;
|
||||||
private boolean useFakeLighting = false;
|
private boolean useFakeLighting = false;
|
||||||
|
private int numberOfRows = 1;
|
||||||
|
|
||||||
public ModelTexture(int id) {
|
public ModelTexture(int id) {
|
||||||
this.textureID = id;
|
this.textureID = id;
|
||||||
@ -51,5 +52,13 @@ public class ModelTexture {
|
|||||||
this.useFakeLighting = useFakeLighting;
|
this.useFakeLighting = useFakeLighting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getNumberOfRows() {
|
||||||
|
return numberOfRows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumberOfRows(int numberOfRows) {
|
||||||
|
this.numberOfRows = numberOfRows;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user