Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  [lwjgl] Rendering to texture with framebuffer object  (Read 806 times)
0 Members and 1 Guest are viewing this topic.
Offline Hanksha

Junior Newbie





« Posted 2014-04-22 14:00:09 »

Hi,

I'm trying to render to a texture with a frame buffer object like in this tutorial (i'm using lwjgl): FBO

I guess I made some mistakes because it's not working well.

Here is my code:
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  
import static org.lwjgl.opengl.EXTFramebufferObject.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class FBO {

   //window size
  final int WIDTH = 640;
   final int HEIGHT = 480;
   
   //texture
  Texture box;
   Texture light;
   
   //frame buffer
  int fbo;
   int depthbuffer;
   int fb_texture;
   
   public void start(){
      setUpDisplay();
      setUpGL();
      loadTextures();
      createFBO();
     
      while(!Display.isCloseRequested()){
         render();
         Display.update();
         Display.sync(60);
      }
   }
   
   private void render(){
      //render to fbo
     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
      glPushAttrib(GL_VIEWPORT_BIT);
      glViewport(0, 0, WIDTH, HEIGHT);
     
      glBindTexture(GL_TEXTURE_2D, light.getTextureID());
      drawTexture(0, 0, 64, 64);
      glBindTexture(GL_TEXTURE_2D, 0);
     
      glPopAttrib();
     
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
     
      //render the texture
     glBindTexture(GL_TEXTURE_2D, fb_texture);
      drawTexture(0, 0, WIDTH, HEIGHT);
      glBindTexture(GL_TEXTURE_2D, 0);
     
   }
   
   private void drawTexture(float x, float y, int width, int height){
      glBegin(GL_QUADS);
      glTexCoord2f(0f, 0f);
      glVertex2f(x, y);
     
      glTexCoord2f(1f, 0f);
      glVertex2f(x + width, y);
     
      glTexCoord2f(1f, 1f);
      glVertex2f(x + width, y + height);
     
      glTexCoord2f(0f, 1f);
      glVertex2f(x, y + height);
      glEnd();
   }
   
   private void createFBO() {
     
      //frame buffer
     fbo = glGenFramebuffersEXT();
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
     
      //depth buffer
     depthbuffer = glGenRenderbuffersEXT();
      glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer);
     
      //allocate space for the renderbuffer
     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, WIDTH, HEIGHT);
     
      //attach depth buffer to fbo
     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer);
     
      //create texture to render to
     fb_texture = glGenTextures();
      glBindTexture(GL_TEXTURE_2D, fb_texture);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, (java.nio.ByteBuffer)null);
     
      //attach texture to the fbo
     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fb_texture, 0);
     
      //check completeness
     if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT){
         System.out.println("Frame buffer created sucessfully.");
      }
      else
         System.out.println("An error occured creating the frame buffer.");
     
   }
   
   
   

   private void loadTextures(){
      try {
         box = TextureLoader.getTexture("PNG", getClass().getResourceAsStream("/box.png"), GL_NEAREST);
         light = TextureLoader.getTexture("PNG", getClass().getResourceAsStream("/light.png"), GL_NEAREST);
      } catch (IOException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
   }
   private void setUpGL() {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glViewport(0, 0, Display.getWidth(), Display.getHeight());
      glEnable(GL_TEXTURE_2D);
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glClearColor(0f, 0f, 0f, 0f);
     
   }


   private void setUpDisplay() {
      try {
         Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
         Display.setTitle("FBO Test");
         Display.create();
      } catch (LWJGLException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
     
   }


   public static void main(String[] args){
      new FBO().start();
   }
}


I just obtain a white screen (meaning the rendered to texture is empty).
Any idea what am I doing wrong?
 
Offline Hanksha

Junior Newbie





« Reply #1 - Posted 2014-04-23 23:56:23 »

Finally fixed it.
I was missing glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   after line 100 when I bind the texture.
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

UprightPath (18 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59: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!