Skip to content

Commit

Permalink
Tweaked FBO a bit to support different texture sizes.
Browse files Browse the repository at this point in the history
Billboard entity renderer will use FBO-based font texture.
  • Loading branch information
bloodrizer committed Sep 17, 2013
1 parent ceeec29 commit 887de88
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nuclearunicorn.negame.client.game.world.NEWorldModel;
import com.nuclearunicorn.negame.client.game.world.NEWorldView;
import com.nuclearunicorn.negame.client.generators.NEGroundChunkGenerator;
import com.nuclearunicorn.negame.client.render.BillboardEntityRenderer;
import com.nuclearunicorn.negame.client.render.TilesetVoxelRenderer;
import com.nuclearunicorn.negame.client.render.VoxelEntityRenderer;
import com.nuclearunicorn.negame.client.render.overlays.NEDebugOverlay;
Expand Down Expand Up @@ -256,7 +257,8 @@ public static void spawnPlayer(Point location, String uid){

playerEnt.setName("Player");
playerEnt.setEnvironment(clientGameEnvironment);
playerEnt.setRenderer(new VoxelEntityRenderer());
//playerEnt.setRenderer(new VoxelEntityRenderer());
playerEnt.setRenderer(new BillboardEntityRenderer());

//TODO: extract player information from the event
//clientGameEnvironment.getEntityManager().add(player_ent, Player.get_zindex());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.nuclearunicorn.negame.client.render;


import com.nuclearunicorn.libroguelike.game.world.WorldView;
import com.nuclearunicorn.libroguelike.render.EntityRenderer;
import com.nuclearunicorn.libroguelike.render.Render;
import com.nuclearunicorn.libroguelike.render.WindowRender;
import com.nuclearunicorn.libroguelike.render.overlay.OverlaySystem;
import com.nuclearunicorn.negame.client.render.utils.FBO;
import org.lwjgl.opengl.*;
import org.newdawn.slick.Color;

public class BillboardEntityRenderer extends EntityRenderer {

private static FBO fbo = new FBO(32, 32); //smalllll texture to fit ascii characters

@Override
public void render(){


//OverlaySystem.ttf.drawString(0,0, "@", Color.red);
fbo.render_begin();

GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

OverlaySystem.ttf.drawString(4, 8, "@", Color.white);

fbo.render_end();

WindowRender.set3DMode();
TilesetVoxelRenderer.camera.setMatrix();

int tileHeight = WorldView.getYOffset(ent.tile);


//point sprites

GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL20.GL_POINT_SPRITE);
GL11.glEnable(GL20.GL_VERTEX_PROGRAM_POINT_SIZE);

//-----------------------------------------------------------------
/*
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SPRITE);
glActiveTexture(GL_TEXTURE0);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
//Activate shader program here
// Send pointSize to shader program
glBegin(GL_POINTS);
// Render points here
glVertex3f(...);
glEnd(GL_POINTS);
*/
//-----------------------------------------------------------------

GL11.glEnable(GL20.GL_VERTEX_PROGRAM_POINT_SIZE); //_ARB?
GL11.glHint(GL11.GL_POINT_SMOOTH_HINT, GL11.GL_NICEST);
GL11.glEnable(GL20.GL_POINT_SPRITE);

//Render.bind_texture("some texture");


GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo.fbo_texture_id);



GL11.glTexEnvi(GL20.GL_POINT_SPRITE, GL20.GL_COORD_REPLACE, GL11.GL_TRUE);

//Replace me with shader
GL11.glPointSize(tileHeight/2.0f);


GL11.glEnable(GL11.GL_POINT_SMOOTH);
GL11.glBegin(GL11.GL_POINTS);

GL11.glColor3f(1.0f, 1.0f, 1.0f);
//GL11.gl

//GL20.glUniform1f(20, 20f);
GL11.glVertex3f(ent.origin.getX() * 1.00005f, (tileHeight + 20 ) * 0.05f, ent.origin.getY() * 1.00005f);


GL11.glEnd();

WindowRender.set2DMode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.nuclearunicorn.negame.client.render.utils;

import org.lwjgl.opengl.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

import static org.lwjgl.opengl.EXTFramebufferObject.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL14.*;

/**
*
* @author Administrator
*/
public class FBO {

/*
* Sometimes fbo minimap rendering can result in buggy blinking screen.
* In such case we must disable fbo minimap directly from the config file
*/

public static boolean fbo_enabled = false;
private static String use_fbo = null;

static
{
/*try {
Properties p = new Properties();
p.load(new FileInputStream("client.ini"));
use_fbo = p.getProperty("fbo_enabled");
if (use_fbo == null){
use_fbo = "1";
}
} catch (Exception ex) {
ex.printStackTrace();
}
fbo_enabled = GLContext.getCapabilities().GL_EXT_framebuffer_object && (!use_fbo.equals("0"));
*/
fbo_enabled = GLContext.getCapabilities().GL_EXT_framebuffer_object;
}

int fbo_id;
public int fbo_texture_id;

private int textureW;
private int textureH;

public FBO(int textureW, int textureH){

this.textureW = textureW;
this.textureH = textureH;

if (fbo_enabled){
//create texture
fbo_texture_id = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, fbo_texture_id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);

glTexImage2D(GL_TEXTURE_2D, 0,
GL_RGBA8, textureW, textureH, 0, GL_RGBA,
GL_UNSIGNED_BYTE, (ByteBuffer)null);

glBindTexture(GL_TEXTURE_2D, 0);

//now create framebuffer
fbo_id = EXTFramebufferObject.glGenFramebuffersEXT();
EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo_id );

//attach a texture to FBO collor channel
EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
GL11.GL_TEXTURE_2D, fbo_texture_id, 0);

//------------------------------------------------------------------
//check FBO status and shit
int status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status != GL_FRAMEBUFFER_COMPLETE_EXT) {
throw new RuntimeException("Unsupported fbo status:"+status);
}

// switch back to window-system-provided framebuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
}

/*
* Prepares FBO for rendering path
*/
public void render_begin(){

EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbo_id );

glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT | GL_COLOR_BUFFER_BIT | GL_SCISSOR_BIT);
glDisable(GL_SCISSOR_TEST);
glViewport(0, 0, textureW, textureH);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, textureW, textureH, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_SCISSOR_TEST);
}

public void render_end(){
EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void subscribe(IEventListener listener){
*/
for (IEventListener regListener: listeners){
if (regListener.getClass().equals(listener.getClass())){
throw new RuntimeException("Trying to subscribe non-unique instance of a class " + regListener.getClass().getCanonicalName());
//throw new RuntimeException("Trying to subscribe non-unique instance of a class " + regListener.getClass().getCanonicalName());
}
}
listeners.add(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public WorldLayer getWorldLayer(int layerId){

public void reset() {
getEventManager().reset();
entManager.reset();
clientWorld.reset();
getEntityManager().reset();
getWorld().reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import com.nuclearunicorn.libroguelike.game.world.WorldTimer;
import com.nuclearunicorn.libroguelike.game.world.WorldView;
import com.nuclearunicorn.libroguelike.game.world.WorldViewCamera;
import com.nuclearunicorn.libroguelike.render.Render;
import com.nuclearunicorn.libroguelike.render.WindowRender;
import com.nuclearunicorn.libroguelike.utils.Timer;
import org.lwjgl.util.Point;
import org.newdawn.slick.Color;

import java.awt.*;
import java.io.InputStream;


public class DebugOverlay {

Expand All @@ -43,9 +47,12 @@ public static void render(){
if (WorldTimer.is_night()){
timePostifx = " (night)";
}
OverlaySystem.ttf.drawString(10, 25, "time: "
+ WorldTimer.datetime.getTime() + timePostifx
, Color.white);

try {

} catch (Exception ex){
//do nothing
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ public OverlaySystem() {
ttf = new TrueTypeFont(font, true);
}

public static TrueTypeFont precache_font(int size){
return precache_font(size, FONT_PATH);
public static TrueTypeFont precacheFont(int size){
return precacheFont(size, FONT_PATH);
}

public static TrueTypeFont precache_font(int size, String fontPath){

public static TrueTypeFont precacheFont(int size, String fontPath){
System.out.println("trying to precache font '"+ fontPath + "'");

Font _font = null;
try {
_font = Font.createFont(Font.TRUETYPE_FONT, OverlaySystem.class.getResourceAsStream(FONT_PATH));
Expand Down Expand Up @@ -133,7 +134,7 @@ public static TrueTypeFont precache_font(String filename, int size){
}
catch(Exception e){
System.err.println(filename + " not loaded. Using serif font.");
return precache_font(size);
return precacheFont(size);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NE_GUI_FrameModern extends NE_GUI_Frame {
public String title = "undefined";

static final int TITLE_SIZE = 12;
public static TrueTypeFont title_ttf = OverlaySystem.precache_font(TITLE_SIZE);
public static TrueTypeFont title_ttf = OverlaySystem.precacheFont(TITLE_SIZE);

public void set_title(String title){
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public NE_GUI_TextLine(String message, Color color){
boolean alignBottom = false;

public NE_GUI_Text(){
chat_ttf = OverlaySystem.precache_font(FONT_SIZE);
chat_ttf = OverlaySystem.precacheFont(FONT_SIZE);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions libroguelike/src/resources/billboard_point.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
uniform float pointSize;
void main() {
gl_Position = ftransform();
gl_PointSize = pointSize / gl_Position.w;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public class Main {

public static SkillerGame game;

public static InGameMode inGameMode = new InGameMode();
public static InGameMode inGameMode;

public static void main(String[] args) {
inGameMode = new InGameMode();

game = new SkillerGame();

Expand Down

0 comments on commit 887de88

Please sign in to comment.