Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created August 26, 2017 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom-Ski/d0673d5b21c35586ff9aa3120ba4a11d to your computer and use it in GitHub Desktop.
Save Tom-Ski/d0673d5b21c35586ff9aa3120ba4a11d to your computer and use it in GitHub Desktop.
#ifdef GL_ES
precision mediump float;
#endif
uniform samplerCube u_skyCubemap;
uniform vec3 u_cameraPos;
varying vec3 v_position;
varying vec3 v_normal;
void main() {
vec3 I = normalize(v_position - u_cameraPos);
vec3 R = reflect(I, normalize(v_normal));
gl_FragColor = vec4(textureCube(u_skyCubemap, R).rgb, 1.0);
}
attribute vec3 a_position;
attribute vec3 a_normal;
uniform mat4 u_worldTrans;
uniform mat4 u_projViewTrans;
uniform mat3 u_normalMatrix;
varying vec3 v_position;
varying vec3 v_normal;
void main() {
v_normal = u_normalMatrix * a_normal;
v_position = vec3(u_worldTrans * vec4(a_position, 1.0));
vec4 pos = u_projViewTrans * vec4(v_position, 1.0);
gl_Position = pos;
}
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Cubemap;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.Shader;
import com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder;
import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.FacedCubemapData;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Matrix3;
import com.badlogic.gdx.utils.Array;
public class CubeMapTest extends ApplicationAdapter {
PerspectiveCamera camera;
FirstPersonCameraController cameraController;
CubeMapShader cubeMapShader;
RenderContext context;
Array<ModelInstance> spheres = new Array<ModelInstance>();
@Override
public void create () {
ModelBuilder builder = new ModelBuilder();
for (int i = 0; i < 100; i++) {
int size = MathUtils.random(2, 10);
ModelInstance instance = new ModelInstance(builder.createSphere(size, size, size, 100, 100, new Material(),
new VertexAttributes(VertexAttribute.Position(), VertexAttribute.Normal()).getMask()));
instance.transform.setToTranslation(MathUtils.random(-50, 50), MathUtils.random(0, 5), MathUtils.random(-50, 50));
spheres.add(instance);
}
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cameraController = new FirstPersonCameraController(camera);
cubeMapShader = new CubeMapShader();
cubeMapShader.init();
context = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.ROUNDROBIN));
Gdx.input.setInputProcessor(cameraController);
}
@Override
public void render () {
cameraController.update();
Gdx.gl.glClearColor(0, 0, 0, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
context.begin();
cubeMapShader.begin(camera, context);
for (ModelInstance sphere : spheres) {
cubeMapShader.render(sphere.getRenderable(new Renderable()));
}
cubeMapShader.end();
context.end();
}
public static class CubeMapShader implements Shader {
private ShaderProgram shaderProgram;
private Cubemap skyboxCubeMap;
private Matrix3 matrix3 = new Matrix3();
@Override
public void init () {
shaderProgram = new ShaderProgram(Gdx.files.internal("testshaders/cubemap.vert"), Gdx.files.internal("testshaders/cubemap.frag"));
if (!shaderProgram.isCompiled()) {
Gdx.app.log("ShaderError", shaderProgram.getLog());
}
FileHandle base = Gdx.files.internal("models/");
FacedCubemapData faces = new FacedCubemapData(base.child("sky_left.png"), base.child("sky_right.png"), base.child("sky_up.png"), base
.child("sky_down.png"), base.child("sky_front.png"), base
.child("sky_back.png"), true);
base = Gdx.files.internal("models/testskybox/");
faces = new FacedCubemapData(base.child("sky_right.png"), base.child("sky_left.png"), base.child("sky_up.png"), base
.child("sky_down.png"), base.child("sky_front.png"), base
.child("sky_back.png"), true);
skyboxCubeMap = new Cubemap(faces);
}
@Override
public int compareTo (Shader other) {
return 0;
}
@Override
public boolean canRender (Renderable instance) {
return true;
}
@Override
public void begin (Camera camera, RenderContext context) {
context.setCullFace(GL20.GL_BACK);
context.setDepthTest(GL20.GL_LEQUAL);
shaderProgram.begin();
shaderProgram.setUniformf("u_cameraPos", camera.position);
shaderProgram.setUniformMatrix("u_projViewTrans", camera.combined);
int bind = context.textureBinder.bind(skyboxCubeMap);
shaderProgram.setUniformi("u_skyCubemap", bind);
}
@Override
public void render (Renderable renderable) {
matrix3.set(renderable.worldTransform).inv().transpose();
shaderProgram.setUniformMatrix("u_normalMatrix", matrix3);
shaderProgram.setUniformMatrix("u_worldTrans", renderable.worldTransform);
renderable.meshPart.render(shaderProgram);
}
@Override
public void end () {
shaderProgram.end();
}
@Override
public void dispose () {
}
}
public static void main (String[] args) {
LwjglApplicationConfiguration configuration = new LwjglApplicationConfiguration();
configuration.samples = 16;
configuration.width = 1280;
configuration.height = 720;
new LwjglApplication(new CubeMapTest(), configuration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment