Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing On Textures  (Read 2433 times)
0 Members and 1 Guest are viewing this topic.
Offline sgthale

Senior Newbie





« Posted 2012-03-07 23:25:49 »

In normal Java, id have a bufferedImage to draw on, have fun with it, and then paste it on the screen. How do i go about to do about the same in JOGL?
Offline davedes
« Reply #1 - Posted 2012-03-08 08:50:04 »

There are numerous ways to modify textures, but generally FBO or shaders are accepted as the best practices for this kind of thing.

FBOs will probably be easier to begin with. In brief:
- create a new texture for your FBO
- bind your FBO to "begin drawing offscreen"
- draw operations will now affect the FBO texture
- unbind your FBO, returning the rendering to normal
- render your now-modified FBO texture to the screen

Here's a JOGL example:
https://github.com/demoscenepassivist/SocialCoding/blob/master/code_demos_jogamp/src/framework/base/BaseFrameBufferObjectRendererExecutor.java

If you're targeting very old systems, you might need to look into PBuffers as a fallback.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-03-08 09:38:15 »

If you're targeting very old systems, you might need to look into PBuffers as a fallback.
To get started, I'd advice to simply render on the framebuffer and copy the pixels to a texture using glCopyTexSubImage.

It might not be as fast as FBOs, but at least you'll have something that works quickly (and can be uses as an extreme fallback).

Once you've got it to work, you can look at FBOs, they are not trivial to setup though.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sgthale

Senior Newbie





« Reply #3 - Posted 2012-04-09 03:41:59 »

There are numerous ways to modify textures, but generally FBO or shaders are accepted as the best practices for this kind of thing.

FBOs will probably be easier to begin with. In brief:
- create a new texture for your FBO
- bind your FBO to "begin drawing offscreen"
- draw operations will now affect the FBO texture
- unbind your FBO, returning the rendering to normal
- render your now-modified FBO texture to the screen

Here's a JOGL example:
https://github.com/demoscenepassivist/SocialCoding/blob/master/code_demos_jogamp/src/framework/base/BaseFrameBufferObjectRendererExecutor.java

If you're targeting very old systems, you might need to look into PBuffers as a fallback.
Yes thank you very much thats what I needed. I understand bits of it but I have one question regarding that code. Does the init() function fire first? Or do you need to throw in arguments to some function for it to start. If so, what function, I can't seem to find it.
Offline theagentd
« Reply #4 - Posted 2012-04-09 05:25:04 »

...

1  
private BaseFrameBufferObjectRendererInterface mBaseFrameBufferObjectRendererInterface;


Hmmm...



Quote
Got extraordinary long code dumps? Use our pastebin to keep your thread readable. (link opens in new tab)

So that's what it is for! Or someone was payed per character instead of line? (And don't ask what the cat came up with, I have no idea.)

Myomyomyo.
Offline sgthale

Senior Newbie





« Reply #5 - Posted 2012-04-10 05:09:15 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
package framework.base;

/**
 **   __ __|_  ___________________________________________________________________________  ___|__ __
 **  //    /\                                           _                                  /\    \\  
 ** //____/  \__     __ _____ _____ _____ _____ _____  | |     __ _____ _____ __        __/  \____\\
 **  \    \  / /  __|  |     |   __|  _  |     |  _  | | |  __|  |     |   __|  |      /\ \  /    /  
 **   \____\/_/  |  |  |  |  |  |  |     | | | |   __| | | |  |  |  |  |  |  |  |__   "  \_\/____/  
 **  /\    \     |_____|_____|_____|__|__|_|_|_|__|    | | |_____|_____|_____|_____|  _  /    /\    
 ** /  \____\                       http://jogamp.org  |_|                              /____/  \    
 ** \  /   "' _________________________________________________________________________ `"   \  /    
 **  \/____.                                                                             .____\/    
 **
 ** Utility wrapper class wich encapsulates the framebuffer-object feature (also known as render-
 ** 2-texture) of OpenGL in a more convenient way. Handles all the boilerplate initialization,
 ** runtime and cleanup code needed to use the FBO. The rendering calls must be provided as
 ** an implementation of the BaseFrameBufferObjectRendererInterface.
 **
 **/


import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.jogamp.common.nio.*;
import com.jogamp.opengl.util.gl2.*;
import static javax.media.opengl.GL2.*;

public class BaseFrameBufferObjectRendererExecutor {

    private int mFrameBufferObjectID;
    private int mColorTextureID;
    private int mDepthTextureID;
    private int mTextureWidth;
    private int mTextureHeight;
    private BaseFrameBufferObjectRendererInterface mBaseFrameBufferObjectRendererInterface;

    public BaseFrameBufferObjectRendererExecutor(int inTextureWidth,int inTextureHeight,BaseFrameBufferObjectRendererInterface inBaseFrameBufferObjectRendererInterface) {
        mTextureWidth = inTextureWidth;
        mTextureHeight = inTextureHeight;
        mBaseFrameBufferObjectRendererInterface = inBaseFrameBufferObjectRendererInterface;
    }

    public int getColorTextureID() { return mColorTextureID; }
    public int getDepthTextureID() { return mDepthTextureID; }
    public int getWidth() { return mTextureWidth; }
    public int getHeight() { return mTextureHeight; }

    public void init(GL2 inGL,GLU inGLU,GLUT inGLUT) {
        BaseLogging.getInstance().info("INITIALIZING BaseFrameBufferObjectRendererExecutor ... "+mTextureWidth+"x"+mTextureHeight);
        //allocate the framebuffer object ...
        int[] result = new int[1];
        inGL.glGenFramebuffers(1, result, 0);
        mFrameBufferObjectID = result[0];
        inGL.glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObjectID);
        //allocate the colour texture ...
        inGL.glGenTextures(1, result, 0);
        mColorTextureID = result[0];
        inGL.glBindTexture(GL_TEXTURE_2D, mColorTextureID);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
        inGL.glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,mTextureWidth,mTextureHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,null);
        //allocate the depth texture ...
        inGL.glGenTextures(1, result, 0);
        mDepthTextureID = result[0];
        inGL.glBindTexture(GL_TEXTURE_2D, mDepthTextureID);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
        inGL.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
        inGL.glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT32,mTextureWidth,mTextureHeight,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_INT,null);
        //attach the textures to the framebuffer
        inGL.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,mColorTextureID,0);
        inGL.glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,mDepthTextureID,0);
        inGL.glBindFramebuffer(GL_FRAMEBUFFER, 0);
        //check if fbo is set up correctly ...
        checkFrameBufferObjectCompleteness(inGL);
        if (mBaseFrameBufferObjectRendererInterface!=null) {
            //initialize the assigned fbo renderer ...
            mBaseFrameBufferObjectRendererInterface.init_FBORenderer(inGL,inGLU,inGLUT);
        } else {
            BaseLogging.getInstance().warning("BaseFrameBufferObjectRendererInterface FOR THIS EXECUTOR IS NULL! init_FBORenderer() SKIPPED!");
        }
    }

    public void renderToFrameBuffer(int inFrameNumber,GL2 inGL,GLU inGLU,GLUT inGLUT) {
        inGL.glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
            //bind the framebuffer ...
            inGL.glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObjectID);
            inGL.glPushAttrib(GL_VIEWPORT_BIT);
            inGL.glViewport(0,0,mTextureWidth,mTextureHeight);
            if (mBaseFrameBufferObjectRendererInterface!=null) {
                mBaseFrameBufferObjectRendererInterface.mainLoop_FBORenderer(inFrameNumber,inGL,inGLU,inGLUT);
            } else {
                BaseLogging.getInstance().warning("BaseFrameBufferObjectRendererInterface FOR THIS EXECUTOR IS NULL! renderToFrameBuffer() SKIPPED!");
            }
            inGL.glPopAttrib();
            //unbind the framebuffer ...
            inGL.glBindFramebuffer(GL_FRAMEBUFFER, 0);
        inGL.glPopAttrib();
    }

    public void prepareForColouredRendering(GL2 inGL, int inTextureUnitID) {
        inGL.glPushAttrib(GL_TEXTURE_BIT);
        inGL.glActiveTexture(inTextureUnitID);
        inGL.glBindTexture(GL_TEXTURE_2D, mColorTextureID);
        //set the texture up to be used for painting a surface ...
        int textureTarget = GL_TEXTURE_2D;
        inGL.glEnable(textureTarget);
        inGL.glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
        inGL.glTexParameteri(textureTarget,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        inGL.glTexParameteri(textureTarget,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        inGL.glTexParameteri(textureTarget,GL_TEXTURE_WRAP_S,GL_REPEAT);
        inGL.glTexParameteri(textureTarget,GL_TEXTURE_WRAP_T,GL_REPEAT);
    }

    public void stopColouredRendering(GL2 inGL) {
        inGL.glBindTexture(GL_TEXTURE_2D, 0);
        //restore the active texture ...
        inGL.glPopAttrib();
    }
   
    public void renderFBOAsFullscreenBillboard(GL2 inGL,GLU inGLU,GLUT inGLUT) {
        renderFBOAsFullscreenBillboard(inGL,inGLU,inGLUT,false);
    }

    public void renderFBOAsFullscreenBillboard_FLIPPED(GL2 inGL,GLU inGLU,GLUT inGLUT) {
        renderFBOAsFullscreenBillboard(inGL,inGLU,inGLUT,true);
    }

    public void renderFBOAsFullscreenBillboard(GL2 inGL,GLU inGLU,GLUT inGLUT,boolean inFlipped) {
        //reset frustum to default state ...
        BaseRoutineRuntime.resetFrustumToDefaultState(inGL,inGLU,inGLUT);
        inGL.glShadeModel(GL_SMOOTH);
        inGL.glDisable(GL_LIGHTING);
        inGL.glFrontFace(GL_CCW);
        inGL.glDisable(GL_CULL_FACE);
        //disable depth test so that billboards can be rendered on top of each other ...
        inGL.glDisable(GL_DEPTH_TEST);        
        inGL.glMatrixMode(GL_PROJECTION);
        inGL.glLoadIdentity();
        inGL.glOrtho(0, BaseGlobalEnvironment.getInstance().getScreenWidth(), BaseGlobalEnvironment.getInstance().getScreenHeight(), 0, -1, 1);
        inGL.glMatrixMode(GL_MODELVIEW);
        inGL.glLoadIdentity();
        this.prepareForColouredRendering(inGL,GL_TEXTURE0);
        inGL.glBegin(GL_QUADS);
        if (inFlipped) {
            //flipped billboard
            inGL.glTexCoord2f(0.0f, 0.0f);
            inGL.glVertex2f(0.0f, 0.0f);
            inGL.glTexCoord2f(1.0f, 0.0f);
            inGL.glVertex2f(BaseGlobalEnvironment.getInstance().getScreenWidth(), 0.0f);
            inGL.glTexCoord2f(1.0f, 1.0f);
            inGL.glVertex2f(BaseGlobalEnvironment.getInstance().getScreenWidth(), BaseGlobalEnvironment.getInstance().getScreenHeight());
            inGL.glTexCoord2f(0.0f, 1.0f);
            inGL.glVertex2f(0.0f, BaseGlobalEnvironment.getInstance().getScreenHeight());
        } else {
            inGL.glTexCoord2f(0.0f, 1.0f);
            inGL.glVertex2f(0.0f, 0.0f);
            inGL.glTexCoord2f(1.0f, 1.0f);
            inGL.glVertex2f(BaseGlobalEnvironment.getInstance().getScreenWidth(), 0.0f);
            inGL.glTexCoord2f(1.0f, 0.0f);
            inGL.glVertex2f(BaseGlobalEnvironment.getInstance().getScreenWidth(), BaseGlobalEnvironment.getInstance().getScreenHeight());
            inGL.glTexCoord2f(0.0f, 0.0f);
            inGL.glVertex2f(0.0f, BaseGlobalEnvironment.getInstance().getScreenHeight());    
        }
        inGL.glEnd();
        this.stopColouredRendering(inGL);
    }

    public void cleanup(GL2 inGL,GLU inGLU,GLUT inGLUT) {
        inGL.glDeleteFramebuffers(1, Buffers.newDirectIntBuffer(mFrameBufferObjectID));
        inGL.glDeleteTextures(1, Buffers.newDirectIntBuffer(mColorTextureID));
        inGL.glDeleteTextures(1, Buffers.newDirectIntBuffer(mDepthTextureID));
        if (mBaseFrameBufferObjectRendererInterface!=null) {
            mBaseFrameBufferObjectRendererInterface.cleanup_FBORenderer(inGL,inGLU,inGLUT);
        } else {
            BaseLogging.getInstance().warning("BaseFrameBufferObjectRendererInterface FOR THIS EXECUTOR IS NULL! cleanup() SKIPPED!");
        }
    }

    private void checkFrameBufferObjectCompleteness(GL2 inGL) {
        BaseLogging.getInstance().info("CHECKING FRAMEBUFFEROBJECT COMPLETENESS ...");
        int tError = inGL.glCheckFramebufferStatus(GL_FRAMEBUFFER);
        switch(tError) {
            case GL_FRAMEBUFFER_COMPLETE:
                BaseLogging.getInstance().info("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_COMPLETE_EXT");
                break;
            case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT");
                break;
            case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT");
                break;
            case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT");
                break;
            case GL_FRAMEBUFFER_INCOMPLETE_FORMATS:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT");
                break;
            case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT");
                break;
            case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT");
                break;
            case GL_FRAMEBUFFER_UNSUPPORTED:
                BaseLogging.getInstance().error("FRAMEBUFFEROBJECT CHECK RESULT=GL_FRAMEBUFFER_UNSUPPORTED_EXT");
                break;
            default:
                BaseLogging.getInstance().error("FRAMEBUFFER CHECK RETURNED UNKNOWN RESULT ...");
        }
    }

}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!