libGDX Shadow not showing on floor - java

I'm trying to add shadows to the bottom of my model but the shadow isn't showing at the bottom. Can anyone help? This is what I tried using the deprecated DirectionalShadowLight:
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
shadowLight = new DirectionalShadowLight(1024, 1024, 60, 60, 1f, 300);
shadowLight.set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f);
environment.add(shadowLight);
environment.shadowMap = shadowLight;
shadowBatch = new ModelBatch(new DepthShaderProvider());
camera = new PerspectiveCamera(67, screenWidth, screenHeight);
camera.position.set(0, 6, 2);
camera.direction.set(0, 0, -4).sub(camera.position).nor();
camera.near = 1;
camera.far = 300;
camera.update();
private void renderSanta(GL20 gl) {
gl.glEnable(GL20.GL_DEPTH_TEST);
gl.glEnable(GL20.GL_CULL_FACE);
// /****************
shadowLight.begin(Vector3.Zero, camera.direction);
shadowBatch.begin(shadowLight.getCamera());
shadowBatch.render(santaModel);
shadowBatch.end();
shadowLight.end();
// ********************/
modelBatch.begin(camera);
modelBatch.render(santaModel, environment);
modelBatch.end();
gl.glDisable(GL20.GL_CULL_FACE);
gl.glDisable(GL20.GL_DEPTH_TEST);
}
private void renderBackground() {
viewMatrix.setToOrtho2D(0, 0, 480, 800);
spriteBatch.setProjectionMatrix(viewMatrix);
spriteBatch.begin();
spriteBatch.enableBlending();
spriteBatch.setColor(Color.WHITE);
spriteBatch.draw(background, 0, 0, 480, 800);
spriteBatch.draw(hammock, 0, 800 - 500 - 95, 182, 95);
spriteBatch.disableBlending();
spriteBatch.end();
}
#Override
public void draw(float delta) {
GL20 gl = Gdx.gl;
gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
renderBackground();
renderSanta(gl);
}
EDIT:
So like Xoppa commented, I added a "plane" under the santa model but still the shadow wasn't shown:
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.ColorUnpacked,
new Material(ColorAttribute.createDiffuse(Color.WHITE)));
mpb.box(0, -1.5f, 0, 7, 1, 7);
mpb.setColor(1f, 0f, 1f, 1f);
model = modelBuilder.end();
instance = new ModelInstance(model);
instance.transform.setTranslation(0, -5, -3);
public void renderModel() {
shadowLight.begin(Vector3.Zero, camera.direction);
shadowBatch.begin(shadowLight.getCamera());
shadowBatch.render(instance);
shadowBatch.end();
shadowLight.end();
modelBatch.begin(camera);
modelBatch.render(instance, environment);
modelBatch.end();
}
#Override
public void draw(float delta) {
GL20 gl = Gdx.gl;
gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
renderBackground();
renderModel();
renderSanta(gl);
}
Updated output still now showing shadows
Edit 2: Thanks to Xoppa I was able to make the shadow appear:

Related

LWJGL texture rendering/indexing

I am currently having issues with trying to render two textures onto two totally separate objects through a single vertex, and fragment shader. The issue seems to lie in trying to index, and bind the two textures onto their own objects. In trying to index and bind the textures, the smaller index will always appear onto both objects.
Can someone help me, or at least push me into the right direction?
here is my code for the main class, the renderer, and the fragment shader.
(feel free to request more code)
main:
import Engine.IO.Image;
import Engine.IO.Input;
import Engine.IO.Window;
import Engine.graphics.*;
import Engine.maths.Vector2f;
import Engine.maths.Vector3f;
import Engine.objects.Camera;
import Engine.objects.GameObject;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL13;
public class Main implements Runnable {
public Thread game;
public Window window;
public Renderer renderer;
public Shader shader;
public final int WIDTH = 1280, HEIGHT = 720;
private String[] textureImageNames = {"nice_dude.jpg", "color.jpg", "pepe.jpg"};
public GameObject thing1 = new GameObject(new Vector3f(-1, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1), new Mesh(new Vertex[]{
new Vertex(new Vector3f(-0.5f, 0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(0.0f,0.0f)),
new Vertex(new Vector3f(0.5f, 0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(1.0f,0.0f)),
new Vertex(new Vector3f(0.5f, -0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(1.0f,1.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(0.0f,1.0f))
}, new int[]{
0, 1, 2, 0, 3, 2
}), new Material(textureImageNames[0]));
public GameObject thing2 = new GameObject(new Vector3f(1, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1), new Mesh(new Vertex[]{
new Vertex(new Vector3f(-0.5f, 0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(0.0f,0.0f)),
new Vertex(new Vector3f(0.5f, 0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(1.0f,0.0f)),
new Vertex(new Vector3f(0.5f, -0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(1.0f,1.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.0f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(0.0f,1.0f))
}, new int[]{
0, 1, 2, 0, 3, 2
}), new Material(textureImageNames[2]));
public Camera camera = new Camera(new Vector3f(0, 0, 1), new Vector3f(0, 0,0));
public void start(){
game = new Thread(this,"game");
game.start();
}
public void init(){
System.out.println("Initializing Game!");
window = new Window(WIDTH, HEIGHT, "Game");
shader = new Shader("/shaders/mainVertex.glsl", "/shaders/mainFragment.glsl");
window.setBackgroundColor(0.0f, 0.5f, 0.0f);
window.create();
thing1.getMesh().create();
thing2.getMesh().create();
thing1.getMaterial().create(new Image());
thing2.getMaterial().create(new Image());
shader.create();
renderer = new Renderer(window, shader);
renderer.renderMesh();
renderer.enableShaderProgram();
renderer.bindVAO(thing1);
renderer.bindVAO(thing2);
renderer.setUniformIndex(thing1,"tex", GL13.GL_TEXTURE0);
renderer.setUniformIndex(thing2,"tex2", GL13.GL_TEXTURE1);
}
public void run(){
init();
while(!window.shouldClose() && !Input.isKeyDown(GLFW.GLFW_KEY_ESCAPE)){
update();
render();
if(Input.isKeyDown(GLFW.GLFW_KEY_F11)){window.setFullscreen(!window.isFullscreen());}
}
close();
}
private void update(){
//System.out.println("updating Game!");
window.update();
camera.update();
}
private void render(){
renderer.updateRenderer(thing1);
renderer.updateRenderer(thing2);
renderer.renderCamera(camera);
window.swapBuffers();
}
private void close(){
window.destroy();
thing1.getMesh().destroy();
thing1.destroyMaterial();
thing2.getMesh().destroy();
thing2.destroyMaterial();
shader.destroy();
renderer.destroyRenderer();
}
public static void main(String[] args){
new Main().start();
}
}
render class:
package Engine.graphics;
import Engine.IO.Window;
import Engine.maths.Matrix4f;
import Engine.objects.Camera;
import Engine.objects.GameObject;
import org.lwjgl.opengl.*;
public class Renderer {
private Shader shader;
private Window window;
public Renderer(Window window, Shader shader){
this.shader = shader;
this.window = window;
}
public void renderMesh() {
GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1);
GL30.glEnableVertexAttribArray(2);
}
public void enableShaderProgram(){
shader.bind();
}
public void bindVAO(GameObject object){
GL30.glBindVertexArray(object.getMesh().getVAO());
}
public void setUniformIndex(GameObject object, String textureName, int index){
GL13.glActiveTexture(index);
shader.setUniform(textureName, index);
GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMaterial().getTextureID());
}
public void updateRenderer(GameObject object){
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
shader.setUniform("projection", window.getProjectionMatrix());
}
public void renderCamera(Camera camera){
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
}
public void destroyRenderer(){
shader.unBind();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}
}
fragment shader:
#version 460 core
in vec3 passColor;
in vec2 passTextureCoord;
out vec4 outColor;
uniform sampler2D tex;
uniform sampler2D tex2;
void main(){
outColor = texture(tex, passTextureCoord);
outColor = texture(tex2, passTextureCoord);
}
The value which has to be set to the texture sampler uniform is the index of the texture unit rather then the texture unit constant (e.g.: 0 for GL13.GL_TEXTURE0 and 1 for GL13.GL_TEXTURE1):
public void setUniformIndex(GameObject object, String textureName, int unit, int index){
shader.setUniform(textureName, index);
GL13.glActiveTexture(unit);
GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMaterial().getTextureID());
}
OpenGL is a state engine. Binding a VAO and or a texture object changes a global state. It is not possible to bind 2 objects at once. Only the last object which was bound is stated. You have to bind the Vertex Array Object and the texture object before the draw call:
private void render(){
renderer.bindVAO(thing1);
renderer.setUniformIndex(thing1,"tex", GL13.GL_TEXTURE0, 0);
renderer.updateRenderer(thing1);
renderer.bindVAO(thing2);
renderer.setUniformIndex(thing2,"tex2", GL13.GL_TEXTURE1, 1);
renderer.updateRenderer(thing2);
renderer.renderCamera(camera);
window.swapBuffers();
}

My rigid body models are dropping through the floor

My rigid body models are dropping through the floor. I am experimenting with Libgdx. When models fall on the ground created from a box they stop moving, but when I replace the ground with a model created from blender the objects fall through. I am trying to build a road track. Here is the code and other examples I tried I keep getting objects that fall through the modeled ground. I'm using blender for the modelling.
public BulletSample(ThrustCopter thrustCopter) {
super(thrustCopter);
// Create ModelBatch that will render all models using a camera
modelBatch = new ModelBatch(new DefaultShaderProvider());
// Create a camera and point it to our model
camera = new PerspectiveCamera(70, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(10f, 10f, 12f);
camera.lookAt(0, 0, 0);
camera.near = 0.1f;
camera.far = 300f;
camera.update();
// Create the generic camera input controller to make the app interactive
cameraController = new CameraInputController(camera);
Gdx.input.setInputProcessor(cameraController);
// Set up environment with simple lighting
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
// environment.set(new ColorAttribute(ColorAttribute.Fog, 0.13f, 0.13f, 0.13f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.8f, 0.3f, -1f));
shadowLight = new DirectionalShadowLight(1024, 1024, 60, 60, 1f, 300);
shadowLight.set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f);
environment.add(shadowLight);
environment.shadowMap = shadowLight;
shadowBatch = new ModelBatch(new DepthShaderProvider());
Bullet.init();
collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
broadphase = new btDbvtBroadphase();
solver = new btSequentialImpulseConstraintSolver();
world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
world.setGravity(new Vector3(0, -9.81f, 1f));
ModelBuilder modelBuilder = new ModelBuilder();
/* Model ground = modelBuilder.createBox(40f, 2f, 40f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
groundInstance=new ModelInstance(ground);
btCollisionShape groundshape = new btBoxShape(new Vector3(20, 1f, 20));
shapes.add(groundshape);
btRigidBody body = new btRigidBody(0,null,groundshape);
bodies.add(body);
world.addRigidBody(body); */
// testing rigid body ground with model from blender
game.manager.load("gameAssets/roadtest1.g3db", Model.class);
game.manager.finishLoading();
Model roadmodel = game.manager.get("gameAssets/roadtest1.g3db", Model.class);
roadInstance = new ModelInstance(roadmodel);
//instances.add(road);
btCollisionShape roadshape = createConvexHullShape(roadmodel, true);
shapes.add(roadshape);
btRigidBody body = new btRigidBody(0, null, roadshape);
world.addRigidBody(body);
Vector3 position=new Vector3();
for(int i=0;i<10;i++){
Model box = modelBuilder.createBox(1f, 1f, 1f,
new Material(ColorAttribute.createDiffuse(Color.BLUE)),
Usage.Position | Usage.Normal);
ModelInstance boxInstance=new ModelInstance(box);
instances.add(boxInstance);
models.add(box);
if(i<5){
position.set(-1, i+1, 0);
}else{
position.set(1, i-4, 0);
}
boxInstance.transform.setToTranslation(position);
btDefaultMotionState motionState = new btDefaultMotionState(boxInstance.transform);
motionState.setWorldTransform(boxInstance.transform.trn(0, 0, 0));
motionStates.add(motionState);
btCollisionShape boxshape = new btBoxShape(new Vector3(0.5f, 0.5f, 0.5f));
shapes.add(boxshape);
btRigidBody boxbody = new btRigidBody(1, motionState, boxshape);
bodies.add(boxbody);
world.addRigidBody(boxbody);
}
//Loading model
game.manager.load("planeanim1.g3db", Model.class);
game.manager.finishLoading();
Model model = game.manager.get("planeanim1.g3db", Model.class);
ModelInstance plane = new ModelInstance(model);
instances.add(plane);
plane.transform.setToRotation(Vector3.Y, 180);
plane.transform.trn(0, 7, 10);
btDefaultMotionState motionState = new btDefaultMotionState(plane.transform);
motionState.setWorldTransform(plane.transform.trn(0, 0, 0));
// motionState.setWorldTransform(plane.transform.rotate(0, 0, .5f,45));
motionStates.add(motionState);
btCollisionShape planeshape = createConvexHullShape(model,true);
shapes.add(planeshape);
btRigidBody planebody = new btRigidBody(5, motionState, planeshape);
bodies.add(planebody);
world.addRigidBody(planebody);
planebody.userData="plane";
planebody.applyCentralImpulse(new Vector3(0,0,-65));
// You use an AnimationController to control animations. Each control is tied to the model instance
controller = new AnimationController(plane);
// Pick the current animation by name
controller.setAnimation("Scene",-1);
contactListener = new MyContactListener();
}
public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize) {
final Mesh mesh = model.meshes.get(0);
final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
if (!optimize) return shape;
// now optimize the shape
final btShapeHull hull = new btShapeHull(shape);
hull.buildHull(shape.getMargin());
final btConvexHullShape result = new btConvexHullShape(hull);
// delete the temporary shape
shape.dispose();
hull.dispose();
return result;
}
#Override
public void dispose() {
groundInstance.model.dispose();
instances.clear();
modelBatch.dispose();
for (Model model : models)
model.dispose();
for (btRigidBody body : bodies) {
body.dispose();
}
for (btMotionState motion : motionStates)
motion.dispose();
for (btCollisionShape shape : shapes)
shape.dispose();
world.dispose();
collisionConfiguration.dispose();
dispatcher.dispose();
broadphase.dispose();
solver.dispose();
contactListener.dispose();
shadowBatch.dispose();
shadowLight.dispose();
}
#Override
public void render(float delta) {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glClearColor(0.13f, 0.13f, 0.13f, 1);
if(started){
world.stepSimulation(Gdx.graphics.getDeltaTime(), 5);
for (int i = 0; i < motionStates.size; i++) {
motionStates.get(i).getWorldTransform(instances.get(i).transform);
}
}else{
if(Gdx.input.isTouched()){
started=true;
System.out.println(instances.size);
if(Gdx.input.justTouched())instances.get(10).transform.rotate(0,0.2f,0,25);
}
}
// Respond to user events and update the camera
cameraController.update();
controller.update(delta);
shadowLight.begin(Vector3.Zero, camera.direction);
shadowBatch.begin(shadowLight.getCamera());
shadowBatch.render(instances);
shadowBatch.end();
shadowLight.end();
// Draw all model instances using the camera
modelBatch.begin(camera);
//modelBatch.render(groundInstance, environment);
modelBatch.render(roadInstance, environment);
modelBatch.render(instances, environment);
modelBatch.end();
super.render(delta);
}

Wraping texture around the box JavaFX

I need to make a rubik cube in JavaFX not using the standard libs and SceneBulider. The question is - I have a texture that I need to wrap around the cube. Using this:
Skinning custom 3D cube in javafx 8
and this Create a cube using different textures in JavaFX
was not able to get any result.
Anyway here's my Controller.
import com.sun.javafx.geom.transform.GeneralTransform3D;
import javafx.fxml.FXML;
import javafx.scene.*;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
public class Controller{
/* double mousePosX;
double mousePosY;
double mouseOldX;
double mouseOldY;
Rotate rotateX = new Rotate(30, 0, 0, 0, Rotate.X_AXIS);
Rotate rotateY = new Rotate(20, 0, 0, 0, Rotate.Y_AXIS);*/
#FXML
Camera camera = new PerspectiveCamera(true);
#FXML
private Box rubik;
#FXML
private AnchorPane anchor;
#FXML
private MeshView mesh;
#FXML
private AmbientLight ambientlight;
private void paintCube()
{
Image dieImage = new Image(getClass().getResourceAsStream("textura.png"));
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(dieImage);
ambientlight.setColor(Color.WHITE);
float hw = 300/2f;
float hh = 300/2f;
float hd = 300/2f;
float points[] =
{
0, 0, 300, //P0
300, 0, 300, //P1
0, 300, 300, //P2
300, 300, 300, //P3
0, 0, 0, //P4
300, 0, 0, //P5
0, 300, 0, //P6
300, 300, 0 //P7
};
float tex[] =
{
0.25f, 0, //T0
0.5f, 0, //T1
0, 0.25f, //T2
0.25f, 0.25f, //T3
0.5f, 0.25f, //T4
0.75f, 0.25f, //T5
1, 0.25f, //T6
0, 0.5f, //T7
0.25f, 0.5f, //T8
0.5f, 0.5f, //T9
0.75f, 0.5f, //T10
1, 0.5f, //T11
0.25f, 0.75f, //T12
0.5f, 0.75f //T13
};
int faces[] =
{
5,1,4,0,0,3 //P5,T1 ,P4,T0 ,P0,T3
,5,1,0,3,1,4 //P5,T1 ,P0,T3 ,P1,T4
,0,3,4,2,6,7 //P0,T3 ,P4,T2 ,P6,T7
,0,3,6,7,2,8 //P0,T3 ,P6,T7 ,P2,T8
,1,4,0,3,2,8 //P1,T4 ,P0,T3 ,P2,T8
,1,4,2,8,3,9 //P1,T4 ,P2,T8 ,P3,T9
,5,5,1,4,3,9 //P5,T5 ,P1,T4 ,P3,T9
,5,5,3,9,7,10 //P5,T5 ,P3,T9 ,P7,T10
,4,6,5,5,7,10 //P4,T6 ,P5,T5 ,P7,T10
,4,6,7,10,6,11 //P4,T6 ,P7,T10 ,P6,T11
,3,9,2,8,6,12 //P3,T9 ,P2,T8 ,P6,T12
,3,9,6,12,7,13 //P3,T9 ,P6,T12 ,P7,T13
};
TriangleMesh mesh1 = new TriangleMesh();
mesh1.getPoints().addAll(points);
mesh1.getTexCoords().addAll(tex);
mesh1.getFaces().addAll(faces);
//mesh = new MeshView(mesh1);
//mesh.setMaterial(material);
mesh.setMesh(mesh1);
rubik.setMaterial(material);
}
#FXML
public void initialize()
{
Rotate cameraRotateX, cameraRotateY, cameraRotateZ;
Translate cameraTranslate;
cameraRotateX = new Rotate(10, Rotate.X_AXIS);
cameraRotateY = new Rotate(10, Rotate.Y_AXIS);
cameraRotateZ = new Rotate(0, Rotate.Z_AXIS);
rubik.getTransforms().addAll(
cameraRotateX,
cameraRotateY,
cameraRotateZ);
// Rotate rotateX = new Rotate(30, 0, 0, 0, Rotate.X_AXIS);
// Rotate rotateY = new Rotate(20, 0, 0, 0, Rotate.Y_AXIS);
//rubik.setRotate(45);
paintCube();
}
/* #FXML
public void klik(MouseEvent mouseEvent) {
mouseOldX = mouseEvent.getX();
mouseOldY = mouseEvent.getY();
}
#FXML
public void gowno(MouseEvent mouseEvent) {
mousePosX = mouseEvent.getX();
mousePosY = mouseEvent.getY();
rotateX.setAngle(rotateX.getAngle()-(mousePosY - mouseOldY));
rotateY.setAngle(rotateY.getAngle()+(mousePosX - mouseOldX));
mouseOldX = mousePosX;
mouseOldY = mousePosY;
}
*/
}
the cube is 300/300/300.

LibGDX - Cannot get texture from FrameBuffer

I try to implement some post-processing, so I need to use FrameBuffer to collect entire picture and then post-process it with shader. I've found some examples of how to work with FrameBuffer in LibGDX(Rendering a 3D model to texture in LibGDX), but it doesn't work. I get black screen. My code snippet:
#Override
public void show() {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1f, 1f, 0f, 1f));
environment.add(new DirectionalLight().set(1f, 0.0f, 0.0f, 0f, -2f, 0f));
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(1f, 1f, 1f);
cam.lookAt(0,0,0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();
com.badlogic.gdx.graphics.g3d.loader.ObjLoader loader =new ObjLoader();
model = loader.loadModel(Gdx.files.internal("cube/cube.obj"));
instance = new ModelInstance(model);
fbo = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
sb= new SpriteBatch();
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
#Override
public void render(float delta) {
fbo.begin();
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instance, environment);
modelBatch.end();
fbo.end();
//getting texture
tex = fbo.getColorBufferTexture();
//render texture
spriteBatch.begin();
spriteBatch.draw(tex,0,0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.end();
}
What do I wrong?
UPDATE
I got another problem. Look at the pictures bellow:
I see this hidden fragment of landscape. I use
fb = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
But when I try to switch on the depth of FrameBuffer, it shows me black screen again:
fb = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
How to get rid of showing this fragment of landscape and why FrameBuffer doesn't work with depth parameter?
You can use ScreenUtils functions to get framebuffer/
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/ScreenUtils.html
ScreenUtils.getFrameBufferPixels(int x, int y, int w, int h, boolean flipY)

JOGL texture not working

I'm trying to fill a torus texture of wood, but leaves only one color:
img http://dl.dropbox.com/u/70996029/Screenshots/Screenshot-2012-04-12_01.28.44.png
The image is 128x128 png. What am I doing wrong?
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LESS);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glOrtho(-17.0, 17.0, -17.0, 17.0, -17.0, 17.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
public void display(GLAutoDrawable drawable)
{
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
float mat_diffuse[] = { 0.5f, 0.5f, 0.8f, 1.0f };
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
glut.glutSolidTorus(2, 5, 80, 80);
gl.glRotatef(90, 1, 0, 0);
gl.glTranslatef(5, 0, 0);
Texture texture = null;
try {
texture = TextureIO.newTexture(new File("D:\\Program Files\\eclipse\\projects\\rotation OGL\\src\\wood.png"), true);
texture.setTexParameterf(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
texture.setTexParameterf(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
} catch (Exception e) {
e.printStackTrace();
}
gl.glEnable(GL2.GL_TEXTURE_2D);
texture.bind(gl);
texture.enable(gl);
glut.glutSolidTorus(2, 5, 80, 80);
texture.disable(gl);
}
Try generating some texture coordinates for your geometry.
glutSolidTorus() does not generate any for you.

Categories