Can not draw anything after text rendering initialization code in lwjgl (Java) - java

Hello I have two seperate initialization codes to switch between rendering 2d shapes and (2d) text in lwjgl. If the initialization code for rendering text is executed, the 2d shapes will not be drawn. I tried everything, and I found the problem line: GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
After I have done something with the glBlendFunc, I can only render tekst, and switching to the initialization code for rendering 2d shapes won't work anymore.
Here are my 2 codes:
Simple 2d rendering:
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
Code for rendering text:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
// Problem line
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
I think the problem is just a wrong OpenGl state, but how can I put the states right?

It's hard to find out the issue without access to your whole code, thus I can just post some guesses:
Do you clear the colour and depth buffer (glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);)?
Do you enable GL_TEXTURE_2D when drawing textures and disable it otherwise?
I use the same blend function and can draw images just fine (it's the most used blend function for drawing images that have transparency).
PS: This are the only settings I use to draw any kind of 2D stuff (I'm using vertex array objects and shaders for rendering though):
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDisable(GL_DEPTH_TEST);

Here is more code
Renderer.java
package game;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.opengl.GL11.GL_CONSTANT_COLOR;
import static org.lwjgl.opengl.GL11.GL_ONE;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Renderer {
private static boolean in3d = false;
public static void initText2D() {
in3d = false;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void init2D() {
in3d = false;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL_TEXTURE_2D);
// Test
//GL11.glBlendFunc(GL_CONSTANT_COLOR, GL_ONE);
}
public static void init3D(float fov, float aspect, float near, float far) {
in3d = true;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
GL11.glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
public static void begin(int shape) {
GL11.glBegin(shape);
}
public static void end() {
GL11.glEnd();
}
public static void setVertex3(float x, float y, float z) {
if(!in3d) {
System.out.println("[WARNING] > Adding 3d vertex but there is no 3d context");
}
GL11.glVertex3f(x, y, z);
}
public static void setColor(float r, float g, float b) {
GL11.glColor3f(r, g, b);
}
public static void setVertex2(float x, float y) {
if(in3d) {
System.out.println("[WARNING] > Adding 2d vertex while in 3d context");
}
GL11.glVertex2f(x, y);
}
}
Button.java (example of how I draw a button in lwjgl)
package gui;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import game.Renderer;
import game.TextRenderer;
import org.lwjgl.opengl.GL11;
public class Button extends AbstractButton {
private String text;
public Button(int x, int y, int w, int h, String text) {
this.setX(x);
this.setY(y);
this.setWidth(w);
this.setHeight(h);
this.text = text;
paintElement();
}
public Button(String text) {
this.text = text;
paintElement();
}
public Button() {
paintElement();
}
public void paintElement() {
Renderer.init2D();
Renderer.begin(GL11.GL_QUADS);
Renderer.setColor(this.getColorR(), this.getColorG(), this.getColorB());
Renderer.setVertex2(this.getX(), this.getY());
Renderer.setVertex2(this.getX() + this.getWidth(), this.getY());
Renderer.setVertex2(this.getX() + this.getWidth(), this.getY() + this.getHeight());
Renderer.setVertex2(this.getX(), this.getY() + this.getHeight());
Renderer.end();
Renderer.initText2D();
TextRenderer.drawString(this.getX() + 10, this.getY() + 10, this.text);
}
}
Main.java
package game;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import java.awt.Font;
import gui.Button;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
public class Main {
private int gamestate;
private boolean closeRequested;
private static Camera cam;
private static User user;
public Main() {
createUser();
createDisplay();
createCamera();
gameLoop();
cleanUp();
}
private void createUser() {
}
private void createDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setResizable(true);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
private void createCamera() {
}
private void gameLoop() {
while(!closeRequested) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Renderer.init2D();
/*
// Test 2d line
Renderer.init2D();
Renderer.setColor(1.0f, 1.0f, 1.0f);
Renderer.begin(GL11.GL_LINE);
Renderer.setVertex2(0, 0);
Renderer.setVertex2(100, 100);
Renderer.end();
*/
// set the color of the quad (R,G,B,A)
glColor3f(0.7f, 0.5f, 1.0f);
// draw quad
glBegin(GL_QUADS);
glVertex2f(100,100);
glVertex2f(100+200,100);
glVertex2f(100+200,100+200);
glVertex2f(100,100+200);
glEnd();
checkInput();
Button b = new Button(100, 100, 100, 30, "Test");
Display.update();
Display.sync(10);
}
return;
}
private void checkInput() {
// Check keyboard, mouse and other input
if(Display.isCloseRequested()) {
closeRequested = true;
}
return;
}
private void cleanUp() {
}
public static void main(String[] args) {
new Main();
}
}

I got it working!
This is my new (working) code:
init2d
GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();
// solution
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
initText2d
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
I think the problem was that slick uses textures for text rendering, and I had to enable the textures or bind them to 0.
See this: http://lwjgl.org/forum/index.php?topic=4019.0

Related

LWJGL 3 Not rendering anything, while successfully creating display

The problem
I am having trouble rendering absolutely anything with LWJGL 3.
It will not render anything, whilst creating the GLFW display and clearing the
color successfully.
The tools
Eclipse Neon (Java IDE)
LWJGL 3.1.1 build 16 with GLFW, JAWT and OPENGL bindings, natives.
The code
package init;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.nio.IntBuffer;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryStack;
import exception.ExceptionHandler;
import util.Time;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
public class DisplayInstance {
public String title = "The SuperMatrix";
public GraphicsDevice gd;
public Game game;
public Config conf;
private long display;
private GLFWErrorCallback glfwerrorcallback;
public DisplayInstance(Game game) {
this.gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
this.game = game;
this.conf = Config.returnConfig(this);
this.glfwerrorcallback = GLFWErrorCallback.createPrint(System.err);
this.glfwerrorcallback.set();
this.start();
}
public void start() {
if (!glfwInit())
ExceptionHandler.handleException(new IllegalStateException("Cannot initialize GLFW"));
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
this.display = glfwCreateWindow(this.conf.width, this.conf.height, this.title, 0, 0);
if (this.display == 0L) {
ExceptionHandler.handleException(new RuntimeException("Cannot create GLFW window"));
}
System.out.println(this.display);
try (MemoryStack stack = stackPush()) {
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
glfwGetWindowSize(this.display, pWidth, pHeight);
GLFWVidMode mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(this.display,
(mode.width()-pWidth.get(0))/2,
(mode.height()-pHeight.get(0))/2
);
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
glfwMakeContextCurrent(this.display);
glfwSwapInterval(1);
glfwShowWindow(this.display);
this.loop();
}
public void loop() {
GL.createCapabilities();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,this.conf.width, 0, this.conf.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
Time time = new Time();
while(!glfwWindowShouldClose(this.display)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(this.display);
glfwPollEvents();
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1,0,1);
glVertex2f(0, 0);
glVertex2f(0, 64);
glVertex2f(64, 64);
glVertex2f(64, 0);
glEnd();
glPopMatrix();
float deltaSeconds = time.getDelta()/Time.SECOND;
float fps = deltaSeconds;
System.out.println(fps);
}
this.destroy();
}
public void destroy() {
glfwFreeCallbacks(this.display);
glfwDestroyWindow(this.display);
glfwTerminate();
this.glfwerrorcallback.free();
this.game.stopGame();
}
}
Thank you. Absolutely any help would be appreciated.
Alright, I finally found the answer.
The cause of the problem
The problem was that I called glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) right before I called glfwSwapBuffers(this.display):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(this.display);
This essentially means that I clear the buffers right before I show them.
The fix
To fix this, all I had to do is move glfwSwapBuffers(this.display) down to after the glPopMatrix() call. Here is how the loop() function looks like now:
GL.createCapabilities();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,this.conf.width, 0, this.conf.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
Time time = new Time();
while(!glfwWindowShouldClose(this.display)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1,0,1);
glVertex2f(0, 0);
glVertex2f(0, 64);
glVertex2f(64, 64);
glVertex2f(64, 0);
glEnd();
glPopMatrix();
glfwSwapBuffers(this.display);
float deltaSeconds = time.getDelta()/Time.SECOND;
float fps = deltaSeconds;
System.out.println(fps);
}
this.destroy();
Everybody, have a great day!
P.S. Please don't mind the FPS system, I'm still trying to figure it out.

Blurry text on GLCanvas

I have a simple custom Canvas
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.awt.TextRenderer;
import java.awt.*;
public class MyCanvas extends GLCanvas implements GLEventListener {
private GLU glu;
public MyCanvas() {
this.addGLEventListener(this);
}
TextRenderer renderer;
#Override
public void init(GLAutoDrawable drawable) {
renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 16));
GL2 gl = drawable.getGL().getGL2();
glu = new GLU();
gl.glClearColor(1f, 1f, 1f, 1f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glShadeModel(GL2.GL_SMOOTH);
}
#Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height == 0) height = 1;
float aspect = (float)width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, aspect, 0.1, 100.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
#Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -50f);
gl.glColor3f(0.5f, 0.5f, 0.5f);
gl.glBegin(GL2.GL_TRIANGLES);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 0.0f);
gl.glEnd();
String text = "Text";
renderer.setColor(Color.BLACK);
renderer.begin3DRendering();
gl.glPushMatrix();
gl.glTranslated(0, 0, 0);
renderer.draw(text, 0, 0);
renderer.flush();
gl.glPopMatrix();
renderer.end3DRendering();
}
#Override
public void dispose(GLAutoDrawable drawable) { }
}
And I have a simple JFrame for displaying this canvas.
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class App extends JFrame {
private static final int CANVAS_WIDTH = 640;
private static final int CANVAS_HEIGHT = 480;
private static final int FPS = 60;
public App() {
GLCanvas canvas = new MyCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
this.getContentPane().add(canvas);
this.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
new Thread() {
#Override
public void run() {
if (animator.isStarted()) animator.stop();
System.exit(0);
}
}.start();
}
});
this.setTitle("JOGL (GLCanvas)");
this.pack();
this.setVisible(true);
animator.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(App::new);
}
}
When I run programm I see very blurry text.
And when I reduce text size, then text became is more and more blurry. What should I do to get a clear text?
The text is blurry, because the font size doesn't match the rendered size on screen and bi-linear interpolation is used to magnify the font/font-texture.
The problem might happen because you are mixing 2D and 3D text rendering functions. You should either use begin3DRendering, draw3D and end3DRendering, where you can control the actual size of the text with the scaling parameter, or you use beginRendering, draw and endRendering where a orthographic projection is used to map pixels from the font to the screen.

Texture in LWJGL not displaying

I am trying to display a png as a texture in Eclipse using the LWJGL library. I made sure to bind the texture and to set the coordinates BEFORE drawing the vertexes, but the image still isn't displaying. What could be the problem?
package javagame;
import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
#SuppressWarnings("unused")
public class ImageLWJGL {
public static Texture p;
public static void main(String[] args) {
createDisplay();
createGL();
render();
cleanUp();
}
private static void createDisplay(){
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
}
}
public static void createGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glClearColor(0,0,0,1);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
private static void render(){
while(!Display.isCloseRequested()){
try {
p = TextureLoader.getTexture("PNG",
new FileInputStream(new File("res/wood.png")));
} catch (IOException e) {
e.printStackTrace();
}
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0.25f,0.75f,0.5f);
p.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0); // (origin, origin)
glTexCoord2f(0,1);
glVertex2f(0,p.getHeight()); // (origin, y-axis)-height
glTexCoord2f(1,1);
glVertex2f(p.getWidth(),p.getHeight()); // (x-axis, y-axis)
glTexCoord2f(1,0);
glVertex2f(p.getWidth(),0); // (x-axis, origin)-width
glEnd();
Display.update();
}
}
private static void cleanUp(){
Display.destroy();
}
}
Your problem is your Ortho Projection Matrix.
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
It goes from 0 to Display Width and 0 to Display Height, but you render your Quad from 0 to 1. So your Quad will be rendered, but to Small, that you can see it.
To solve the Problem Change the glOrtho to:
glOrtho(0, 1, 0, 1, -1, 1);

Java texture will not show

I am new to java programming and I am trying to draw a textured square but it only shows up in white.
if i add glEnable(texture2d) it distorts the color. I can get the texture to bind when not in "state format", but like i said, the glEnable(texture2d) distorts the color. Is there something im missing? Thanks!
Here is the main class
package main;
static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import static helper.Artist.*;
import helper.Artist.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class GameWindow {
public static enum states{
MAIN, GAME; }
public states state = states.MAIN;
public void render(){
switch(state){
case MAIN:
glColor3f(1.0f, 0, 0);
glRectf(0, 0, 800, 600);
break;
case GAME:
glColor3f(1,1,1);
DrawQuad(grass,5,5,64,64);
}
}
public void checkInput(){
switch(state){
case MAIN:
if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
state = states.GAME;
}
break;
case GAME:
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
state = states.MAIN;
}
}
}
public GameWindow(){
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Display.setTitle("Hello");
//initialization
grass = LoadTexture("dirt64");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, 1, -1 );
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while(!Display.isCloseRequested()){
//render code
checkInput();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args){
new GameWindow();
}}
The second class
package helper;
import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class Artist {
public static Texture grass;
public void load(){
try {
grass = TextureLoader.getTexture("PNG", new FileInputStream("res/dirt64.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Texture LoadTexture(String key){
try{
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void Draw(float x, float y, float width, float height){
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
public static void DrawQuad(Texture texture, float x, float y, float width, float height){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
texture.bind();
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
}

LWJGL and Slick textures display as black...?

I use eclipse. so in my workspace, under my project, i create a new folder "res" with a subfolder "images" that have all my png's for use as textures. so here is the method im using for loading the textures:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import org.lwjgl.input.Mouse;
import java.util.Random;
public class TextureDemo
{
public int count = 0;
private static Texture wood;
Random random = new Random();
public TextureDemo()
{
initGL(640, 480, "SLICK TEXTURES");
loadTexture("mozilla");
int x = 100, y = 100, count = 0, width = 0, height = 0, counter = 10;
while(true)
{
count++;
if(count == counter)
{
x--; y--; width++; height++; counter += random.nextInt(50) + 1;
}
render(x, y, width, height);
Display.update();
Display.sync(60);
if(Display.isCloseRequested())
{
Display.destroy();
System.exit(0);
}
}
}
private void initGL(int width, int height, String title)
{
try
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(title);
Display.create();
}
catch(LWJGLException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
GL11.glDisable(GL11.GL_COLOR_MATERIAL);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
public void loadTexture(String key)
{
try
{
wood = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./res/images/"+key+".png"));
System.out.println("working." + wood.getTextureID());
}
catch(FileNotFoundException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void render(int x, int y, int width, int height)
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
System.out.println("working." + wood.getTextureID());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, wood.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + wood.getImageWidth(), y);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + wood.getImageWidth(), y + wood.getImageHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x, y + wood.getImageHeight());
GL11.glEnd();
GL11.glDisable(GL11.GL_BLEND);
System.out.println(wood.getHeight()+ " " +wood.getWidth());
}
public static void main (String[] args)
{
new TextureDemo();
}
}
WHY WHY WHY are my textures black lol? I really don't understand how my code did this. Is it possible that my computer could have problems that are causing that?
Asking the same question twice won't help. However, you should make sure you first call glBindTexture, before you start glBegin.
I'm sorry, however there are many problems with your code. I would recommend taking a look at some opengl tutorials. The arcsynthesis one is very in-depth.
Your problem is that the texture "wood" is never bound to opengl.
In order to bind your texture to the opengl context you must call
glBindTexture(GL_TEXTURE_2D, id);
before glbegin()
The id is the texture id generated by glGentextures or in your case the slick util method.
Also what is wood? In the first code block it is the texture id but in the second block you are calling 'getHeght()' on type wood. I am confused as to what 'wood' is.
Either way, you need to bind the texture for opengl to use it and I again recommending looking at a few tutorials on the opengl basics.
You haven't enabled textures!
In your initGL method call GL11.glEnable(GL11.GL_TEXTURE_2D).

Categories

Resources