Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  Problem: Draw Framebuffer on Framebuffer  (Read 3382 times)
0 Members and 1 Guest are viewing this topic.
Offline pixinator

Senior Newbie


Exp: 1 year



« Posted 2016-08-11 13:04:38 »

I am new to OpenGL but I have some knowledge already about it.
My problem:
I created two FBOs with the same size. The first FBO was rendered to the second FBO and the second FBO was rendered to the screen. It seemed like there was no problem but when I changed the size of the first FBO to half size the drawn content on the first FBO was scaled by the value 0.5.

To better understand this I have some screenshots:
Without problems:
https://www.dropbox.com/s/qz79pz8swten32h/Screenshot%20%28129%29.png?dl=0

With scaling problem:
https://www.dropbox.com/s/2tv4nsm9ng8rw6k/Screenshot%20%28130%29.png?dl=0
red: second FBO
gray: first FBO
black: background
Offline theagentd
« Reply #1 - Posted 2016-08-11 14:28:04 »

You forgot to call glViewport() to update to the new resolutions?

Myomyomyo.
Offline elect

JGO Knight


Medals: 76



« Reply #2 - Posted 2016-08-11 14:45:27 »

Probably theagentd has the answer, however, more than thinking in terms or FBO, you should think in terms of attachments, that is, you set the size of the color attachment 0, you render the color att 0 of FBO a to the color att 1 of FBO b
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pixinator

Senior Newbie


Exp: 1 year



« Reply #3 - Posted 2016-08-11 20:16:37 »

theagentd I checked the code again and I was using glViewport before rendering to foreign FBO so that's not the problem.

elect what exactly do you mean?  Can you show me the lines of code so I can recheck?
Offline elect

JGO Knight


Medals: 76



« Reply #4 - Posted 2016-08-12 06:37:44 »

elect what exactly do you mean?

> I created two FBOs with the same size. The first FBO was rendered to the second FBO and the second FBO was rendered to the screen.

The FBO has no size, its attachments have. You don't render one FBO to another, you render/splash/blit one of its attachment to another attachment (that may belong to another FBO or not).

Can you show me the lines of code so I can recheck?

I guess it is better if you show us some of your code  Wink
Offline pixinator

Senior Newbie


Exp: 1 year



« Reply #5 - Posted 2016-08-12 08:08:36 »

Sure I can.

First of all this is how I set up my Display:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
Display.setDisplayMode(new DisplayMode(960, 640));
Display.create();
glViewport(0, 0, this.gameWindow.getWidth(), this.gameWindow.getHeight());

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, this.gameWindow.getFrameBufferSize().width, this.gameWindow.getFrameBufferSize().height, 0, -1, glMatrixMode(GL_MODELVIEW);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glDisable(GL_DEPTH_TEST);



Here is the code how I create/load a texture:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
int ID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, 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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

Texture texture = new Texture();
texture.width = image.getWidth();
texture.height = image.getHeight();
texture.ID = ID;
texture.bufferImage = image2;


To create a FBO and attach texture to it I do this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public SpriteBatch(Texture texture) {
   this.texture = texture;

   if (!GLContext.getCapabilities().GL_EXT_framebuffer_object) {
      System.exit(0);
   }
   // create frame buffer
   this.FBO = glGenFramebuffersEXT();
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this.FBO);

   // depth buffer
   this.depthBuffer = glGenRenderbuffersEXT();
   glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, this.depthBuffer);

   // allocate space for the renderBuffer
   glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texture.getWidth(), texture.getHeight());

   // attach depth buffer to FBO
   glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT,
      this.depthBuffer);

   // attach texture to the FBO
   glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture.getID(), 0);
}


To render a FBO(the texture which is attached to it) to another FBO I handle it like everey other texture:
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  
public void draw(Texture texture, Rectangle dstRect, Color color) {
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this.FBO);
   glPushAttrib(GL_VIEWPORT_BIT);
   glViewport(0, 0, this.frameBuffer.getWidth(), this.frameBuffer.getHeight());

   texture.bind();
   glTranslatef(position.x, position.y, 0);
   glBegin(GL_QUADS);
   glColor4f((float) color.r / 255F, (float) color.g / 255F, (float) color.b / 255F, (float) color.a / 255F);

   glTexCoord2f(0, 0);
   glVertex2f(0, 0);

   glTexCoord2f(0, 1);
   glVertex2f(0, texture.getHeight());

   glTexCoord2f(1, 1);
   glVertex2f(texture.getWidth(), texture.getHeight());

   glTexCoord2f(1, 0);
   glVertex2f(texture.getWidth(), 0);

   glEnd();
   glLoadIdentity();
   texture.unbind();

   glPopAttrib();
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}


And finally render to Display:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
glViewport(0, 0, Display.getWidth(), Display.getHeight());

// I use translate and scale because otherwise it would be rendered mirrored (maybe you have a solution for that)
this.frameBuffer.bind();
glTranslatef(0, this.frameBuffer.getHeight(), 0);
glScalef(1, -1, 0);
glBegin(GL_QUADS);
glColor4f(1, 1, 1, 1);

glTexCoord2f(0, 0);
glVertex2f(0, 0);

glTexCoord2f(0, 1);
glVertex2f(0, this.frameBuffer.getHeight());

glTexCoord2f(1, 1);
glVertex2f(this.frameBuffer.getWidth(), this.frameBuffer.getHeight());

glTexCoord2f(1, 0);
glVertex2f(this.frameBuffer.getWidth(), 0);

glEnd();
glLoadIdentity();


I think that should be all of what you need. If you still need something then let me know.  Wink
Offline theagentd
« Reply #6 - Posted 2016-08-12 11:27:45 »

If your framebuffer setup is correct, then it's probably your texture coordinates. I'm guessing that the texture you're rendering to is still full resolution.

This is my new guess:

1. You have a window-sized texture.
2. You set the viewport to only cover a small portion of the entire texture you draw to and render to it.
3. When reading the FBO, you read the entire texture instead of just the small portion you drew to.

Myomyomyo.
Offline pixinator

Senior Newbie


Exp: 1 year



« Reply #7 - Posted 2016-08-17 19:53:02 »

Thank you @theagentd and @elect for your help. Point 3 of your guesses was the problem. I fixed it and it works fine now. Wink
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (583 views)
2019-09-04 15:33:17

hadezbladez (5517 views)
2018-11-16 13:46:03

hadezbladez (2405 views)
2018-11-16 13:41:33

hadezbladez (5779 views)
2018-11-16 13:35:35

hadezbladez (1226 views)
2018-11-16 13:32:03

EgonOlsen (4664 views)
2018-06-10 19:43:48

EgonOlsen (5684 views)
2018-06-10 19:43:44

EgonOlsen (3199 views)
2018-06-10 19:43:20

DesertCoockie (4096 views)
2018-05-13 18:23:11

nelsongames (5116 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!