Everytime I am running this code it will give me a white window with the loading cursor of death. After some seconds the window is marked as non responding.
This is my code that I have written:
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit())
throw new IllegalStateException("Couldn't initialize GLFW");
long window = GLFW.glfwCreateWindow(1200, 750, "WorldCraft", MemoryUtil.NULL, MemoryUtil.NULL);
//GLFW.glfwMakeContextCurrent(window);
// Enable v-sync
Timer timer = new Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (GLFW.glfwGetCurrentContext() == MemoryUtil.NULL) {
GLFW.glfwMakeContextCurrent(window);
GLFW.glfwSwapInterval(1);
GLFW.glfwShowWindow(window);
GL.createCapabilities();
}
System.out.println("Begin Render");
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GLFW.glfwPollEvents();
GL11.glPushMatrix();
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(0.65F, 0.34F, 0.15F);
GL11.glVertex3d(8, 0, 8);
GL11.glVertex3d(-8, 0, 8);
GL11.glVertex3d(8, 0, -8);
GL11.glVertex3d(-8, 0, -8);
GL11.glEnd();
GL11.glPopMatrix();
GLFW.glfwSwapBuffers(window);
System.out.println("End Render");
}
});
timer.start();
What am I doing wrong?
Related
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:
platformR = new ShapeRenderer();
platformR.setProjectionMatrix(camera.combined);
platformR.begin(ShapeType.Filled);
platformR.identity();
platformR.setColor(new Color(0, 1, 0, 1));
platformR.rotate(0, 0, 1, 90);*/
platformR.rect(getxPos(), getyPos(), getSprite().getWidth() + getxPos(),
getSprite().getHeight() + getxPos());
platformR.end();
This is the code I have for displaying a rectangle, but the rectangle doesn't display. Is there anything I'm missing?
Make sure you place your .begin() / .end() method calls in the render() method.
public class ShapeRendererExample extends ApplicationAdapter {
ShapeRenderer shapeRenderer;
#Override
public void create () {
shapeRenderer = new ShapeRenderer();
}
#Override
public void render () {
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(new Color(1, 0, 0, 1));
shapeRenderer.rect(100f, 100f, 100f, 100f);
shapeRenderer.end();
}
private void clearScreen()
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
in LWJGL I add text to my Display. When done, the program drops to about 1-2FPS. I have capped it with Display.sync(60);
Without text, it runs fine. With, absolutely... terrible?
Here is the source for my text:
import java.awt.Font;
import java.io.InputStream;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.util.ResourceLoader;
public class Text {
private TrueTypeFont font2;
public void drawString(String font, String string, int x, int y, Color color) {
// TODO: Fix extreme lag issues.
try {
InputStream inputStream = ResourceLoader.getResourceAsStream(font);
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont.deriveFont(24f); // set font size
font2 = new TrueTypeFont(awtFont, false);
Color.white.bind();
font2.drawString(x, y, string, color);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Dont exactly want to release my games source but heres the code that displays the text.
// Initialization code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Text t = new Text();
t.drawString("res/Minecraftia.ttf", "test", 0, 0, Color.yellow);
Edit:
// Initialization code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT);
while (!Display.isCloseRequested()) {
// Render
init();
input();
grid.draw();
drawSelectionBox();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
private void init() {
// This is what i'm meant to do????
Text t = new Text();
t.drawString("res/Minecraftia.ttf", "test", 0, 0, Color.white);
}
Edit again: (Hope not too long)
public Boot() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("Test Program.");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
grid = new BlockGrid();
// Initialization code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT);
init();
input();
grid.draw();
drawSelectionBox();
Display.update();
Text t = new Text();
t.drawString("res/Minecraftia.ttf", "test", 0, 0, Color.white);
while (!Display.isCloseRequested()) {
// Render
init();
input();
grid.draw();
drawSelectionBox();
//Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
Because you read the whole font and create it in every render call.
public void drawString(String font, String string, int x, int y, Color color) {
try {
//You create a new resource stream and load a file
InputStream inputStream = ResourceLoader.getResourceAsStream(font); // <-- slow
//You create a new Fonts and load it out of the input stream
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream); // <-- extremely slow
//You create a new TrueTypeFont out of it
awtFont = awtFont.deriveFont(24f); // set font size
font2 = new TrueTypeFont(awtFont, false); // <-- slow
Color.white.bind();
font2.drawString(x, y, string, color);
} catch (Exception e) {
e.printStackTrace();
}
}
Instead, you should do the Font-loading only once in an init call and then just refer to the TrueTypeFont when rendering.
Remember that rendering is a very performance sensitive call and creating instances or streams and loading in that method (and in update(..), too) can drastically drop your FPS rate and should be avoided.
Update
Furthermore, you are creating a new instance of Text every rendering call too:
Text t = new Text();
t.drawString("res/Minecraftia.ttf", "test", 0, 0, Color.yellow);
Here too, create this once in the beginning or, better, just pass the String to draw to the drawString method instead of having a wrapper object like Text for that.
So I have successfully imported textures into my game, but they are upside down, distorted and also off-centre.
The code can also be found here: http://pastebin.com/nvWMZqpX
If anyone could help it would be greatly appreciated.
public class Main {
private Texture sky;
public Main() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Spark");
Display.create();
sky = TextureLoader.getTexture("PNG", new FileInputStream(new File("resources/textures/sky.png")));
while(!Display.isCloseRequested()) {
setCamera();
sky.bind();
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3d(1, 1, 1);
glTexCoord2f(1, 0);
glVertex2i(640, 0);
glTexCoord2f(0, 0);
glVertex2i(0, 0);
glTexCoord2f(0, 1);
glVertex2i(0, 480);
glTexCoord2f(0, 1);
glVertex2i(0, 480);
glTexCoord2f(1, 1);
glVertex2i(640, 640);
glTexCoord2f(1, 0);
glVertex2i(640, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
Display.update();
Display.sync(60);
}
sky.release();
Display.destroy();
} catch (LWJGLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Main();
}
public static void setCamera() {
//Clear Screen
glClear(GL_COLOR_BUFFER_BIT);
//Modifying the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
//Modify modelviewing matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
Is your texture a power of 2 (POT) image? (for example 512x512 pixels). This could be the cause of the distortion.
Also try inverting your texcoord arguments. This would fix the upside down part.
I might also noticed a wrong argument
glTexCoord2f(1, 1);
glVertex2i(640, 0);
glTexCoord2f(0, 1);
glVertex2i(0, 0);
glTexCoord2f(0, 0);
glVertex2i(0, 480);
glTexCoord2f(0, 0);
glVertex2i(0, 480);
glTexCoord2f(1, 0);
glVertex2i(640, 480); // <--- this line was 640,640?
glTexCoord2f(1, 1);
glVertex2i(640, 0);
Texture coordinates in openGL are handled upside down like this:
(source: learnopengles.com)
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.