Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  [Solved] Render to texture problem  (Read 928 times)
0 Members and 1 Guest are viewing this topic.
Offline elect

Junior Member





« Posted 2012-02-20 15:04:16 »

Hi,

basically when I am rendering to texture, it looks like some part of the texture go lost..

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  
package org.yourorghere;

import com.jogamp.opengl.util.GLBuffers;
import java.awt.Component;
import java.nio.ByteBuffer;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;


public class GLRenderer implements GLEventListener {

    int[] textureID = new int[1];
    private int floorWidth=48, floorHeight=48;
    int[] frameBufferID = new int[1];
    int[] depthRenderBufferID = new int[1];
    ByteBuffer pixels;
    GLU glu;
   
    public void init(GLAutoDrawable drawable) {
        glu = new GLU();
       
        System.out.println("init");
       
        GL2 gl = drawable.getGL().getGL2();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Setup the drawing area and shading mode
       gl.glShadeModel(GL2.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
       
        renderShadowsToTexture(gl);
       
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    }

    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        System.out.println("display");
       
        float a = 1.0f;
       
        gl.glMatrixMode(GL2.GL_PROJECTION);
        // Reset the current matrix to the "identity"
       gl.glLoadIdentity();
        glu.gluPerspective(60.0f, (((Component)drawable).getWidth()/
                ((Component)drawable).getHeight()), 1.0f, 50.0f);

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(0.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f,
                      0.0f, 1.0f, 0.0f);
       
        // Clear the drawing area
       gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

        gl.glTranslatef(-2.5f, 0.0f, 0.0f);
       
        gl.glEnable(GL2.GL_TEXTURE_2D);
       
        gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
       
            gl.glColor3f(1.0f, 1.0f, 1.0f);
            gl.glBegin(GL2.GL_QUADS);
                gl.glTexCoord2f(0, 0);
                gl.glVertex3f(-1.0f,-1.0f, 0.0f);
                gl.glTexCoord2f(0, a);
                gl.glVertex3f(-1.0f, 1.0f, 0.0f);
                gl.glTexCoord2f(1.0f, 1.0f);
                gl.glVertex3f( 1.0f, 1.0f, 0.0f);
                gl.glTexCoord2f(a, 0);
                gl.glVertex3f( 1.0f,-1.0f, 0.0f);
            gl.glEnd();
        gl.glDisable(GL2.GL_TEXTURE_2D);
       
        gl.glRasterPos2d(3, -2);
        gl.glDrawPixels(floorWidth, floorHeight, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, pixels);
       
    }
   
    private void renderShadowsToTexture(GL2 gl) {
        gl.glGenTextures(1, textureID, 0);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
       
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
       
        // null means reserve texture memory, but texels are undefined
       gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, floorWidth, floorHeight,
                                                    0, GL2.GL_RGB, GL2.GL_FLOAT, null);
       
        gl.glGenFramebuffers(1, frameBufferID, 0);
        gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID[0]);
       
        //Attach 2D texture to this FBO
       gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0,
                                        GL2.GL_TEXTURE_2D, textureID[0], 0);
       
        // depth buffer
       gl.glGenRenderbuffers(1, depthRenderBufferID, 0);
        gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
        gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT,
                                                floorWidth, floorHeight);
        gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT,
                                            GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
       
        if(gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER) == GL2.GL_FRAMEBUFFER_COMPLETE)
            System.out.println("[Viewer] GL_FRAMEBUFFER_COMPLETE!!");
        else
            System.out.println("..cazzo ^^");
       
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glPushMatrix();
        gl.glLoadIdentity();
       
        gl.glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
       
        gl.glPointSize(10.0f);
        gl.glBegin(GL2.GL_POINTS);
            gl.glColor3f(0.0f, 1.0f, 0.0f);
            gl.glVertex2d(1.0f, 1.0f);      // THIS IS NOT SHOWN
           gl.glColor3f(0.0f, 0.0f, 1.0f);
            gl.glVertex2d(-1.0f, -1.0f);
            gl.glVertex2d(-0.9f, -0.9f);
        gl.glEnd();
       
        gl.glPopMatrix();

        pixels = GLBuffers.newDirectByteBuffer(floorWidth*floorHeight*4);

        gl.glReadPixels(0, 0, floorWidth, floorHeight, GL2.GL_RGBA,
                                                    GL2.GL_UNSIGNED_BYTE, pixels);
                                                   
                                                   
        System.out.println("glIsTexture: "+gl.glIsTexture(textureID[0]));
       
        //  bind the back buffer for rendering
       gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, 0);
    }

    public void dispose(GLAutoDrawable glad) {
//        throw new UnsupportedOperationException("Not supported yet.");
       System.out.println("dispose");
    }

}


Starting from the left, the triangle and the first quad are rendered normally using the <i>display()</i> while the last quad on the right and the one below are respectively the quad rendered with the texture mapped on it and the quad showing what is inside the texture itself

Basically I do not see the red point, only the blue ones



Why?
Offline elect

Junior Member





« Reply #1 - Posted 2012-02-20 15:35:50 »

Solved:

1  
2  
3  
4  
5  
6  
gl.glPushAttrib(GL2.GL_VIEWPORT_BIT); 
gl.glViewport(0, 0, floorWidth, floorHeight);

...

gl.glPopAttrib();
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (72 views)
2013-05-17 21:29:12

alaslipknot (83 views)
2013-05-16 21:24:48

gouessej (113 views)
2013-05-16 00:53:38

gouessej (108 views)
2013-05-16 00:17:58

theagentd (119 views)
2013-05-15 15:01:13

theagentd (108 views)
2013-05-15 15:00:54

StreetDoggy (153 views)
2013-05-14 15:56:26

kutucuk (176 views)
2013-05-12 17:10:36

kutucuk (173 views)
2013-05-12 15:36:09

UnluckyDevil (182 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.381 seconds with 21 queries.