This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2015/1/13

My lwjgl buffer to texture code

Entity1037 Entity1037

2015/1/13

#
I posted without finishing the title -_- It's suppose to say "My lwjgl buffer to texture code in eclipse won't correctly render the texture, but gives no errors" I made this method that renders text from a text sprite sheet to a texture, but it doesn't write any text to it, and is instead completely blank. I looked through the code and positioning of vertexs, but they all appear correct. The only thing I could see being wrong is the code that sets up the buffer, but I don't know what's wrong with it. Could someone please help me?
private static Texture textSpriteSheet;

private static String[][] textMatrix = {
    {"","","!","(",")","%","#","'","\"","^","+","_",",","-",".","/"},
    {"0","1","2","3","4","5","6","7","8","9",":"," ","","=","","?"},
    {"[c]","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"},
    {"P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e"},
    {"f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u"},
    {"v","w","x","y","z","\"",";","<",">"," "," "," "," "," "," "," "}
};

/**
 * 
 * @param texture Texture to be written to
 * @param x X coordinate of the top left pixel of the area to write to on the texture
 * @param y Y coordinate of the top left pixel of the area to write to on the texture
 * @param untilWrap How many characters until the text writes down to the next line
 * @param text What text to write into texture
 * @return Inputed texture with the specified text written into it.
 */
public static Texture WriteToTexture(Texture texture, int dimension, int x, int y, int untilWrap, String text){

    boolean FBOEnabled = GLContext.getCapabilities().GL_EXT_framebuffer_object;

    if (!FBOEnabled){
        System.out.println("The FBO extension is not supported!");
        return texture;
    }

    if (textSpriteSheet==null){
        textSpriteSheet = QuickLoad("Mega_Man_Font_8x8");
        System.out.println("textSpriteSheet is null!");
    }

    int textX = x;
    int textY = y;

    /**Set up FBO to render to texture**/

    //Generally a good idea to enable texturing first
    GL11.glEnable(GL11.GL_TEXTURE_2D);

    //Create Frame Buffer Object (FBO)
    IntBuffer buffer = ByteBuffer.allocateDirect(1*4).order(ByteOrder.nativeOrder()).asIntBuffer(); // allocate a 1 int byte buffer
    EXTFramebufferObject.glGenFramebuffersEXT(buffer); // generate
    int myFBOId = buffer.get();

    //Bind Texture to FBO
    EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, myFBOId);
    EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,GL11.GL_TEXTURE_2D, texture.getTextureID(), 0);

    //Completeness check
    int framebuffer = EXTFramebufferObject.glCheckFramebufferStatusEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT);
    switch (framebuffer) {
        case EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT:
            break;
        case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
            throw new RuntimeException( "FrameBuffer: " + myFBOId + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception" );
        case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
            throw new RuntimeException( "FrameBuffer: " + myFBOId + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception" );
        case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
            throw new RuntimeException( "FrameBuffer: " + myFBOId + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception" );
        case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
            throw new RuntimeException( "FrameBuffer: " + myFBOId + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception" );
        case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
            throw new RuntimeException( "FrameBuffer: " + myFBOId + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception" );
        case EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
            throw new RuntimeException( "FrameBuffer: " + myFBOId + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception" );
        default:
            throw new RuntimeException( "Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer );
    }

    //Bind FBO
    EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, myFBOId );
    GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
    GL11.glViewport( 0, 0, 128, 128);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

    //Render text

    while (!text.equals("")){

        String character = text.substring(0,1);
        text = text.substring(1);
        boolean typed = false;

        for (int i = 0; i<textMatrix.length; i++){
            for (int j = 0; j<textMatrix[i].length; j++){

                if (character.equals("@")){
                    textX = x;
                    textY+=8;
                    typed = true;
                    break;
                }

                if (character.equals(textMatrix[i][j])){
                    //DrawQuadTex(textSpriteSheet, (int) textX, (int) textY, 8, 8, 128, 128, j*8, i*8, 8, "");

                    textSpriteSheet.bind();
                    glTranslatef(textX, textY, 0);
                    glBegin(GL_QUADS);
                    glTexCoord2f(j*8/dimension, i*8/dimension);//top left corner
                    glVertex2f(0,0);
                    glTexCoord2f((j*8+8)/dimension, i*8/dimension);//top right corner
                    glVertex2f(8,0);
                    glTexCoord2f((j*8+8)/dimension, (i*8+8)/dimension);//bottom right corner
                    glVertex2f(8,8);
                    glTexCoord2f(j*8/dimension, (i*8+8)/dimension);//bottom left corner
                    glVertex2f(0,8);
                    glEnd();

                    glLoadIdentity();

                    System.out.println("WROTE TO TEXTURE!\nX: "+textX+"\nY: "+textY);

                    textX+=8;

                    if ((textX-x)/8>untilWrap){
                        textX = x;
                        textY+=8;
                    }
                    typed = true;
                    break;
                }
            }
            if (typed){break;}
        }
    }
    //Unbind Texture
    EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
    GL11.glPopAttrib();

    //Return texture
    return texture;
}
Here is the class that's calling the method:
package data;

import static helpers.Artist.*; import org.newdawn.slick.opengl.Texture;

public class WeaponSelection extends Actor{
    private double x;
    private double y;
    private int iconSpriteX;
    private int iconSpriteY;
    private int untilWrap;
    private String text;
    private Texture texture = QuickLoad("blank_128x128");
    //private static Texture iconSpriteSheet = QuickLoad("mega_man_weapon_icons_by_blistalk-d5mybk8");

    public WeaponSelection(double x, double y, int iconSpriteX, int iconSpriteY, int untilWrap, String text){
        this.x = x;
        this.y = y;
        this.iconSpriteX = iconSpriteX;
        this.iconSpriteY = iconSpriteY;
        this.untilWrap = untilWrap;
        this.text = text;
    }

    boolean first = true;
    public void Draw(){

        if (first){
            texture = WriteToTexture(texture, 128, 20, 5, untilWrap, text);
            first = false;
        }

        //insert weapon icon drawing code here
        DrawQuadTex(texture, (int)x, (int) y, 128, 128, 128, 128, 0, 0, 128, "");
    }

 }
Here is the info that's being inputted into the instance of WeaponSelection.class
WeaponSelection textTest = new WeaponSelection(0,0,0,0,10,"B. CRACKER");
And here is the console output for the first cycle: Tue Jan 13 14:53:14 CST 2015 INFO:Use Java PNG Loader = true Frame Skipping: true Loading Default World... xscroll: 0 yscroll: 0 Done! textSpriteSheet is null! WROTE TO TEXTURE! X: 20 Y: 5 WROTE TO TEXTURE! X: 28 Y: 5 WROTE TO TEXTURE! X: 36 Y: 5 WROTE TO TEXTURE! X: 44 Y: 5 WROTE TO TEXTURE! X: 52 Y: 5 WROTE TO TEXTURE! X: 60 Y: 5 WROTE TO TEXTURE! X: 68 Y: 5 WROTE TO TEXTURE! X: 76 Y: 5 WROTE TO TEXTURE! X: 84 Y: 5 WROTE TO TEXTURE!
DesaGrammer DesaGrammer

2015/1/13

#
I'm not sure how to do something as advanced as this sorry but it could be a simple as when the class is calling the method in line ten you put " }; " not sure if that's going to make a big difference but it probably will if you get erase the " ; " in the 10 it's made a big difference to me before Hope it work
DesaGrammer DesaGrammer

2015/1/13

#
But that's just from my variety of languages not from lwjgl (never tried the language sorry) (just so you know)
DesaGrammer DesaGrammer

2015/1/13

#
Also when the class is calling the method in line 4 under " textMatrix = { " you put " ,"\"", " and I'm guessing there should only be one " but that's all that I can guess where you've gone wrong without knowing the language
DesaGrammer DesaGrammer

2015/1/13

#
Well I could gues a few from line 4-9 but I say again I dont know the language so I don't know if it should be done
danpost danpost

2015/1/14

#
DesaGrammer wrote...
I'm not sure how to do something as advanced as this sorry but it could be a simple as when the class is calling the method in line ten you put " }; " not sure if that's going to make a big difference but it probably will if you get erase the " ; " in the 10 it's made a big difference to me before Hope it work
@DesaGrammer, this IS java; and all java statements are to end with a semi-colon; so, it should not be removed.
DesaGrammer wrote...
Also when the class is calling the method in line 4 under " textMatrix = { " you put " ,"\"", " and I'm guessing there should only be one " but that's all that I can guess where you've gone wrong without knowing the language
There are some characters that need to be written that way to include them within strings. Please refer to the section called 'Character and String Literals' at the lower part of this page of the Java turorials.
You need to login to post a reply.