Render to texture

Started by Jonbca1, August 01, 2004, 05:27:22

Previous topic - Next topic

Jonbca1

Hi everyone, I'm pretty new to GL/LWJGL  and Im having a bit of trouble rendering to a texture. What im basically trying to do is render an ocasional frame to a texture, then use that to make a transition to the next section. What I have setup right now is working fine (just rendering a tri and a quad), but the texture is either A) not being created, or b) not being applied. I'll post my code for you, hopefully someone can give a hand. BTW, im following the code from the NeHe site for this, just so you know.

public static void main(String [] args){
   //init GL etc
    while(!done){
        logic();
        renderToTexture();
        render();
        Window.update();
    }
}

public void renderToTexture() {
    GL11.glViewport(0, 0, 128, 128);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, screenTexture);
    GL11.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0,        GL11.GL_LUMINANCE, 0, 0, 128, 128, 0);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glViewport(0, 0, 640, 480);
}


The jist of the code is to draw a rotating tri and a quad. the quad has the screen rendered onto it whenever the user presses space. This is just a test to get stuff running, just so you know. Thanks

Chman

You must use the Pbuffer class to render to a texture. There's a demo in the official lwjgl demo package... Take a look at it :)

Chman

Fool Running

For my game engine, I'm using basically the same setup that you are using...

I don't know what is in your render() function, but you might make sure you have something on the screen to copy to your texture when the copy function is called. :)

Also I noticed that you are setting up the texture type as GL_LUMINANCE.  Are you sure you don't want GL_RGB? :?

Quote
You must use the Pbuffer class to render to a texture. There's a demo in the official lwjgl demo package... Take a look at it  

Is there a reason we should use the Pbuffer class?  Does it gain us speed/manage resources better/etc.?  Or is it just a suggestion?  :?
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

PeterM

Hello,

As far as I know, the reason you should use P-Buffers are to avoid OpenGL having to process the frame buffer while copying it to the texture.

Have a look at the glPixel* functions in MSDN or the Red Book for more info.

Pete

Fool Running

I've heard (or read  :lol: ) that the P-Buffer for LWJGL creates a new context if used in Windows and so all the textures/display lists/etc. need to be created twice.  Is this true?  It seems that that would cause a waste of video memory which would cause a performance hit.  

Am I completely off base?  :wink:
Are there any other downsides to using P-Buffers?

Am I just writing because I have nothing else to do?   :roll:
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Jonbca1

Hi...I'm still playing around withthis and i was just wondering if anyone knows why this might now be working. thanks

public class ScreenCapture2 {
    public boolean done = false;

    public boolean capture = false;

    public int txtr = -1;

    public Pbuffer buf;

    public ScreenCapture2() {
        init();

        while (!done) {
            Keyboard.poll();
            for (; Keyboard.next();) {
                if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE)
                    done = true;
                if (Keyboard.getEventKey() == Keyboard.KEY_SPACE)
                    capture = true;

            }
            if (capture)
                renderToTexture();
            render();
            Window.update();
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OpenGL.cleanup();
    }

    public void init() {
        OpenGL.init("GL Test", 800, 600, false);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
    }

    public void renderToTexture() {
        GL11.glViewport(0, 0, 256, 256);
        render();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, txtr);
        GL11.glCopyTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 0, 0, 256,
                256, 0);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glViewport(0, 0, 800, 600);
    }

    public void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glDisable(GL11.GL_TEXTURE_2D);

        GL11.glTranslatef(0.0f, 0.0f, -15.0f);
        GL11.glColor3f(0.656465f, 0.365498f, 0.9871f);
        GL11.glBegin(GL11.GL_TRIANGLES);
        {
            GL11.glVertex3f(0.0f, 1.0f, 0.0f);
            GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
            GL11.glVertex3f(1.0f, -1.0f, 0.0f);
        }
        GL11.glEnd();

        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, txtr);
        GL11.glLoadIdentity();
        GL11.glTranslatef(3.2f, 3.2f, -10.0f);
        GL11.glColor3f(1.0f, 1.0f, 1.0f);
        GL11.glBegin(GL11.GL_QUADS);
        {
            GL11.glTexCoord2f(0.0f, 1.0f);
            GL11.glVertex3f(0.0f, 1.0f, 0.0f);
            GL11.glTexCoord2f(0.0f, 0.0f);
            GL11.glVertex3f(0.0f, 0.0f, 0.0f);
            GL11.glTexCoord2f(1.0f, 0.0f);
            GL11.glVertex3f(1.0f, 0.0f, 0.0f);
            GL11.glTexCoord2f(1.0f, 1.0f);
            GL11.glVertex3f(1.0f, 1.0f, 0.0f);
        }
        GL11.glEnd();

    }

    public static void main(String[] args) {
        new ScreenCapture2();
    }
}


note: the OpenGL class is just to create the window, and init openGL to relativly generic settings.

elias

The Pbuffer in LWJGL uses a separate context, but it shares textures, display lists etc. with the Display.

- elias

Jonbca1

Thanks everyone, I just got it working perfectly.