trouble with slick 2d unicode font - java

ok so I have determined that if I change my orthographic matrix coordinate system to
GL11.glOrtho(0, Strings.DISPLAY_WIDTH, Strings.DISPLAY_HEIGHT, 0, -1, 1);
instead of this
GL11.glOrtho(0, Strings.DISPLAY_WIDTH, 0, Strings.DISPLAY_HEIGHT, -1, 1);
I can draw strings with slick right side up however the game im building uses this system
GL11.glOrtho(0, Strings.DISPLAY_WIDTH, 0, Strings.DISPLAY_HEIGHT, -1, 1);
and slick draws things with the top of the matrix being 0 and the bottom being the height of the display is there anyway I can rotate or draw the strings differently without having to change my games coordinate system?
here is my string drawing class
private Font javaFont;
private UnicodeFont uniFont;
public TextHandler(int letterSize) {
this.initHandler(letterSize);
}
private void initHandler(int size) {
this.javaFont = new Font("Times New Roman", Font.BOLD, size);
this.uniFont = new UnicodeFont(this.javaFont);
this.uniFont.getEffects().add(new ColorEffect(java.awt.Color.white));
this.uniFont.addAsciiGlyphs();
try {
this.uniFont.loadGlyphs();
} catch (SlickException e) {
e.printStackTrace();
}
}
public void drawString(String string, float x, float y) {
this.uniFont.drawString(x, y, string, Color.white);
}
and my openGl initialization code if it helps
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Strings.DISPLAY_WIDTH, 0, Strings.DISPLAY_HEIGHT, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0, 0, 1, 0);
GL11.glDisable(GL11.GL_DEPTH_TEST);

Yes, you could set up a rendering system where the first glOrtho call sets up the Slick rendering, and then you render the strings. Then you call glOrtho again, but render the rest of your game. glOrtho simply tells OpenGL to setup an orthographic projection, and what corner of the screen to start rendering from. Slick uses the top left hand corner whereas you are using the bottom left hand corner. OpenGL is a state based machine, so you must render the Slick stuff, switch to the new projection, and then render the rest of your game.
You also don't need that call to glDisable(GL_DEPTH_TEST);. Depth testing is turned off by default.

Related

Can I use glScale on GL_POINTS?

I am programming a GUI framework in lwjgl (opengl for java). I've recently implemented rounded rectangles by rendering a couple of normal rectangles surrounded by circles. To render the circles I used GL11.GL_POINTS. I now reached the point, where I am trying to implement animations and for a window open animation, I decided to GL11.glScaled() it from small to normal. That works fine, but unfortunately my circles don't get resized.
I tried changing my GL_POINTS circle render method against a method that uses TRIANGLE_FANs and that worked fine. My problem there was, that the circles didn't look smooth and round at all and if I increase the rendered triangles it starts to lag very quick. Even though my computer isn't bad at all.
This is the code I've used to render circles with GL_POINTS.
GL11.glEnable(GL11.GL_POINT_SMOOTH);
GL11.glHint(GL11.GL_POINT_SMOOTH_HINT, GL11.GL_NICEST);
GL11.glPointSize(radius);
GL11.glBegin(GL11.GL_POINTS);
GL11.glVertex2d(x, y);
GL11.glEnd();
GL11.glDisable(GL11.GL_POINT_SMOOTH);
This is the code I've used to scale the circles
GL11.glPushMatrix();
GL11.glTranslated(x, y, 0);
GL11.glScaled(2.0f, 2.0f, 1);
GL11.glTranslated(-x, -y, 0);
render circles
GL11.glPopMatrix();
I expect the circles to scale accordingly to the number I've put into glScaled()
Currently they aren't rescaling at all, just rendered at their normal size.
Here's a demonstration of how to properly render a circle using triangle fans:
public void render() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Coordinate system starts out as screen space coordinates
glOrtho(0, 400, 300, 0, 1, -1);
glColor3d(1, 0.5, 0.5);
renderCircle(120, 120, 100);
glColor3d(0.5, 1, 0.5);
renderCircle(300, 200, 50);
glColor3d(0.5, 0.5, 1);
renderCircle(200, 250, 30);
}
private void renderCircle(double centerX, double centerY, double radius) {
glPushMatrix();
glTranslated(centerX, centerY, 0);
glScaled(radius, radius, 1);
// Another translation here would be wrong
renderUnitCircle();
glPopMatrix();
}
private void renderUnitCircle() {
glBegin(GL_TRIANGLE_FAN);
int numVertices = 100;
double angle = 2 * Math.PI / numVertices;
for (int i = 0; i < numVertices; ++i) {
glVertex2d(Math.cos(i*angle), Math.sin(i*angle));
}
glEnd();
}
Output image:
The GL_POINT_SIZE value is actually the size of the point in pixels onscreen, not current coordinate units. For that reason your circles were unaffected by GL_SCALE. That's one reason not to use GL_POINTS to render circles. The other (arguably more important) reason being that GL_POINT_SIZE is severely deprecated and unsupported in newer OpenGL profiles.

LWJGL/OpenGL scaling textures weirdly

I am making a platformer which I started developing with default java functions, but now am switching to OpenGL for everything. I have done everything like I always do with OpenGL, and what I did works fine in my other OpenGL projects. Now my problem is that LWJGL/OpenGL is scaling my textures in a very strange way.
It seems to be related to my screen's aspect ratio. (8:5)
I already had to flip the screen to make it the right way round, but as you can see the text is working fine, it's just the textured rect, and it isn't even straight on the bottom.
Here are the most important snippets from the two classes which actually use OpenGL:
Metamorph.java (main class)
public static void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
}
public void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(1.0f, -1.0f, 1.0f);
glTranslatef(0f, -720f, 0f);
//glScalef(1280f/800, 720f/500, 1f);
renderer.render();
Display.update();
Display.sync(60);
}
Renderer.java (rendering stuff)
private void renderMainMenuWithGL()
{
//System.out.println("Main Menu!");
glColor4f(1, 1, 1, 1);
try
{
Texture bg = loadTexture("mockery");
bg.bind();
} catch (Exception e)
{
e.printStackTrace();
}
//drawQuad(0, 0, 1280, 720, 0, 0, 1280, 720);
glPushMatrix();
{
glBegin(GL_QUADS);
glTexCoord2f(0, 0);glVertex2f(0, 0);
glTexCoord2f(0, 1);glVertex2f(0, 720);
glTexCoord2f(1, 1);glVertex2f(1280, 720);
glTexCoord2f(1, 0);glVertex2f(1280, 0);
glEnd();
}
glPopMatrix();
TrueTypeFont f = loadFont(MAINFONT, Font.PLAIN, 50);
TrueTypeFont fb = loadFont(MAINFONT, Font.PLAIN, 48);
int sel = -1;
if(Mouse.getX() > 1000 && Mouse.getX() < 1240 && Mouse.getY() > 282.5F && Mouse.getY()< 737.5F)
sel = Math.round((Mouse.getY() - 337.5F)/75F);
if(sel == 0)
drawStringRight(fb, 1240, 350, "Story", new Color(0xff516b6b));
else
drawStringRight(f, 1240, 350, "Story", new Color(0xff516b6b));
}
private void drawStringRight(TrueTypeFont f, int x, int y, String s, Color c)
{
glPushMatrix();
f.drawString(x-f.getWidth(s), y, s, c);
glPopMatrix();
}
I am also open to advice on file structure/what I did wrong elsewhere, but keep in mind this is heavily WIP
The only possible problem I can see from what you have posted is the scaling and translating you are doing prior to rendering. You should not need to do this with your projection matrix setup the way it is. Other possibilities are that either the dimensions are not really 1280x720 as you think or you have modified one of the matrices further in the code you have not posted. I would try setting both the modelview and projection matrices to the identity matrix and then use glOrtho as you have above immediatly before drawing your quad, and use Display.getWidth and Display.getHeight instead of 1280/720 for the vertex coords.
This works fine for me:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2i(0, 0);
glTexCoord2f(1,0);
glVertex2i(Display.getWidth(), 0);
glTexCoord2f(1,1);
glVertex2i(Display.getWidth(), Display.getHeight());
glTexCoord2f(0,1);
glVertex2i(0, Display.getHeight());
glEnd();
If this still doesnt work, make sure that the viewport is also set to the entire display: glViewport(0,0,Display.getWidth(),Display.getHeight()).
Aswell, I notice that you are using glPushMatrix() and glPopMatrix before and after drawing, which does nothing and is not needed. Push and pop are used to save the current projection or modelview matrix and then reload it later, so that you can apply transformations inbetween and undo them when needed.
You're setting the projection matrix such that the coordinates of the corners of the window are (0,0) through (1680, 1050), and those are mapped into an area that covers 1280x800 pixels. Then you're drawing a 1280x720 image into it, so the screen coordinates of the image come out to span only 800x500 pixels. If you use 1280x720 in your glOrtho() call, I believe it will fix the issue. That is, you want the size of the window, not the size of the display in that call.

Drawn Image In OpenGL Comes Out Wrong Size And With Black Boxes In It

Im currently trying to work on my Java project which includes LWJGL.
I have come so far with my "game" that im now drawing an image to the screen.
The problem im getting is that the drawn image gets drawn with black boxes in it and its not the correct size.
here is an image of how it looks visually.
Here is how the actualy red square image should look like:
Here is the code i use for rendering with OpenGL, I cannot figure out what im doing wrong.
public class Renderer {
//Integers used for player cordinates, Taken from player class by using Static variables
int playerX;
int playerY;
SpriteSheetLoader spriteLoader;
Texture player;
public Renderer(){
}
public void initRenderer(){
//Initialize OpenGL
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity(); // Resets any previous projection matrices
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
try {
player = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("res/PaddleTemp.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(){
GL11.glClear( GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT );
playerX = Player.playerX; //Gets player x and y from the player class using static variables
playerY = Player.playerY;
player.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); //top left
GL11.glVertex2f(playerX, playerY);
GL11.glTexCoord2f(1,0); //Top right
GL11.glVertex2f(playerX + 50, playerY);
GL11.glTexCoord2f(1, 1); //Bottom right
GL11.glVertex2f(playerX + 50, playerY + 150);
GL11.glTexCoord2f(0, 1); //bottom left
GL11.glVertex2f(playerX, playerY + 150);
GL11.glEnd();
}
Sorry, I'm not absolutely sure what the problem is. I can't seem to reproduce it afterwards, unfortunately. Yet I figured it should be solved by one of the following:
Unsupported Image Size
OpenGL relies heavily on so images, whose resolution's width and height are powers of 2 (when width/height=n*2). If the images files aren't up to that specification, LWJGL might act oddly. Also, don't worry about the image files being squished. That's dependent on the vertex input, not on the texture input.
Unsupported Image Extension
Try saving your image files as non-interlaced PNG files. Slick_Util, or whatever you use for loading the image files, might not fully support the images you're giving to it.
Correction Hints
As a last resort, you could add the following lines of code to your initialization code:
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Hopefully one of these tips helps you.
Source: LWJGL not rendering textures correctly?
I have a really easy fix for you. Your main problem is that you are using an unsupported image size, but OpenGL is good when it just expands the actual size of the sprite to be correct. It also records the exact float value for the width and height of the image so that you can in fact use images that are any size you want. Just replace your current rendering code with this:
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); //top left
GL11.glVertex2f(playerX, playerY);
GL11.glTexCoord2f(player.getWidth(),0); //Top right
GL11.glVertex2f(playerX + 50, playerY);
GL11.glTexCoord2f(player.getWidth(), player.getHeight()); //Bottom right
GL11.glVertex2f(playerX + 50, playerY + 150);
GL11.glTexCoord2f(0, player.getHeight()); //bottom left
GL11.glVertex2f(playerX, playerY + 150);
GL11.glEnd();
Also, you can disregard the other answer as this one should be your solution.

Blended textures show up fine, but ordinary coloured shapes do not

For some reason enabling alpha blending results in me not being able to draw run-of-the-mill coloured shapes. The order in which everything is drawn makes no difference. Even if the only thing being drawn is the coloured shape, it still won't show.
Disabling alpha blending fixes this, but disables alpha blending (obviously). This leads me to believe the problem is in how I'm initializing openGL.
The textured objects are contained in the world, which is commented out. Commenting "world.run();" out makes no difference, only disabling alpha blending does.
public class Core {
int width=800, height=600;
//World world;
public void Start(){
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
initGL();
System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));
boolean Close = Display.isCloseRequested();
//world = new World(width, height);
while(!Close){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) || Display.isCloseRequested())
Close = true;
//world.run();
GL11.glColor4d(1, 0, 0, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(0, 0);
GL11.glVertex2d(0, 50);
GL11.glVertex2d(50, 50);
GL11.glVertex2d(50, 0);
GL11.glEnd();
Display.update();
//Display.sync(60);
}
}
public void initGL(){
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void main(String args[]){
Core m = new Core();
m.Start();
}
}
This is for a 2D app where I'm trying to draw metaballs behind the texture of a black-and-white world map.
Run-of-the-mill coloured shapes refers to the following,
GL11.glColor4d(1, 0, 0, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(0, 0);
GL11.glVertex2d(0, 50);
GL11.glVertex2d(50, 50);
GL11.glVertex2d(50, 0);
GL11.glEnd();
Even if drawn on its own, as long as alpha blending is enabled, it won't show up.
UPDATE:
The constructor for world was loading (but not drawing) a texture. Removing that part of the code lets the coloured square show up. I have deduced that the problem will occur as long as a texture is loaded, regardless of whether it is displayed or not.
You've got glEnable(GL_TEXTURE_2D) in initGL, but I don't see it disabled anywhere.
You know you have to disable texturing if you want to draw an untextured object, right?

Affine Transform Compatibility

public void draw(Graphics2D g) {
// draw background
g.drawImage(bgImage, 0, 0, null);
g.drawImage(bgImage, 700, 0, null);
g.drawImage(bgImage, 0, 500, null);
g.drawImage(bgImage, 700, 500, null);
AffineTransform transform = new AffineTransform();
for (int i = 0; i < NUM_SPRITES; i++) {
Sprite sprite = sprites[i];
// translate the sprite
transform.setToTranslation(sprite.getX(),
sprite.getY());
// if the sprite is moving left, flip the image
if (sprite.getVelocityX() < 0) {
transform.scale(-1, 1);
transform.translate(-sprite.getWidth(), 0);
}
// draw it
g.drawImage(sprite.getImage(), transform, null);
}
}
}
I was looking at a draw method in this class. The method is called with an animation loop, it draws a set of animated sprites on the screen that pass the method the correct frame in their animation depending on a timer. The sprites are also moving around the screen at a randomly generated velocity bouncing off the edges of the frame. Depending on whether they are traveling left or right the draw method transforms the image so the sprite is facing in the correct direction. All that transform information is contained in the transform for the drawImage method which I'm used to seeing as g.drawImage(Image, x, y, null); though obviously you can't do that here as you would lose the image flipping capabilities. Is there a way to do it though? Is there a way to transform the image scale but set its location by coordinate?
I ask because transforming is a more processors intensive activity and if lots of sprites need to be active at once it might really slow everything down. BONUS QUESTION: Is this a legitimate concern or should I not be too worried considering the strength of modern systems.

Categories

Resources