Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Screen Capture To Texture (OpenGL / Slick2D)  (Read 795 times)
0 Members and 1 Guest are viewing this topic.
Offline fuzmeister

Junior Member


Medals: 1
Projects: 1
Exp: 12 years


I make music and games.


« Posted 2014-07-04 16:30:54 »

Alright, so I'm trying to capture the current screen to a Texture object that I can render in all kinds of fun ways... I'm using the LWJGL tutorial on taking a screenshot, but rather than save that ByteBuffer to a file, I'm trying to create a Slick2D Texture from it.

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  
   public static org.newdawn.slick.opengl.Texture getTextureFromScreen()
   {
      // Copies the current screen.
     GL11.glReadBuffer(GL11.GL_FRONT);
      int width = Display.getDisplayMode().getWidth();
      int height= Display.getDisplayMode().getHeight();
     
      java.nio.ByteBuffer buffer = org.lwjgl.BufferUtils.createByteBuffer(width * height * 4);      
      GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);            
     
      int textureId = GL11.glGenTextures();
      TextureImpl tex = new TextureImpl("screen", GL11.GL_TEXTURE_2D, textureId);
     
      // produce a texture from the byte buffer
     GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);            
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL11.GL_TEXTURE_MIN_FILTER,GL11.GL_LINEAR);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL11.GL_TEXTURE_MAG_FILTER,GL11.GL_LINEAR);
      GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);            
     
      return tex;        
   }

   public static void drawTexture(float x, float y, Texture texture, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight, float red, float green, float blue, float alpha, float angle)
   {      
      // If the provided texture string is null or empty, don't try to draw it. :)
     if(texture != null)
      {                                
         texture.bind();
         
         float fSrcX = ((float)srcX / texture.getTextureWidth());
         float fSrcY = ((float)srcY / texture.getTextureHeight());
         float fSrcWidth = (((float)srcX + (float)srcWidth) / texture.getTextureWidth());
         float fSrcHeight = (((float)srcY + (float)srcHeight) / texture.getTextureHeight());        
         
         GL11.glPushMatrix();            
                     
         // Rotation
        if(angle != 0)
         {
            GL11.glTranslatef(x + (width / 2), y + (height / 2), 0); // move to the proper position
           GL11.glRotatef(angle, 0, 0, 1); // now rotate
           GL11.glTranslatef(-1 *(x+ (width / 2)), -1 * (y+(height  / 2)), 0);            
         }
         
         GL11.glColor4f(red, green, blue, alpha);
         GL11.glBegin(GL11.GL_QUADS);
            // Top Left
           GL11.glTexCoord2f(fSrcX, fSrcY);
            GL11.glVertex2f(x,y);
            // Top Right
           GL11.glTexCoord2f(fSrcWidth, fSrcY);
            GL11.glVertex2f(x + width, y);
            // Bottom Right
           GL11.glTexCoord2f(fSrcWidth,fSrcHeight);
            GL11.glVertex2f(x + width,y + height);
            // Bottom Left
           GL11.glTexCoord2f(fSrcX,fSrcHeight);
            GL11.glVertex2f(x,y + height);
         GL11.glEnd();
         
         GL11.glPopMatrix();
      }        
   }


I've had some weird results thus far including the resulting texture only capturing one pixel's color (at least that's how it renders) and getting some errors about the ByteBuffer size. Any ideas??

- Steve

Offline fuzmeister

Junior Member


Medals: 1
Projects: 1
Exp: 12 years


I make music and games.


« Reply #1 - Posted 2014-07-04 16:36:38 »

I tried the drawTexture method with another Texture already loaded in and it worked fine, so the issue is definitely with the getTextureFromScreen method.

- Steve

Offline theagentd
« Reply #2 - Posted 2014-07-04 16:44:20 »

Just render the scene to a texture using an FBO instead of reading back the data to RAM and then reuploading it again, which is a magnitude or so slower.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fuzmeister

Junior Member


Medals: 1
Projects: 1
Exp: 12 years


I make music and games.


« Reply #3 - Posted 2014-07-04 16:50:16 »

I need a way to call back to the captured screen image though while I'm in a screen transition loop, so I figured having a Texture object to reference would be the best way.

Capturing the screen basically helps me avoid redrawing every object each step through the transition loop; instead I just draw the captured screen.

- Steve

Offline fuzmeister

Junior Member


Medals: 1
Projects: 1
Exp: 12 years


I make music and games.


« Reply #4 - Posted 2014-07-04 17:06:08 »

I'm also wondering if this issue has anything to do with the resulting texture's dimensions not being a power of 2.

 Huh Huh Huh

- Steve

Offline theagentd
« Reply #5 - Posted 2014-07-04 17:41:15 »

I need a way to call back to the captured screen image though while I'm in a screen transition loop, so I figured having a Texture object to reference would be the best way.

Capturing the screen basically helps me avoid redrawing every object each step through the transition loop; instead I just draw the captured screen.

- Steve
You're missing my point. You don't have to "capture" the screen to a texture. You can render everything onto a texture in the first place instead of rendering to the screen.

Myomyomyo.
Offline fuzmeister

Junior Member


Medals: 1
Projects: 1
Exp: 12 years


I make music and games.


« Reply #6 - Posted 2014-07-04 18:17:47 »

I understand what you're saying, but I've already got a ton of code in place for this and the most pain-free way to integrate this functionality in is to copy the screen to a Texture. Otherwise, I'll have to completely change how everything I'm doing is rendered.

Thanks for the advice either way.

- Steve

Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #7 - Posted 2014-07-04 18:22:19 »

Otherwise, I'll have to completely change how everything I'm doing is rendered.
Do you know how FBOs work?
You just bind the FBO and then do everything as you would normally do, but the output will be rendered into the FBO's texture instead of the back buffer. No extra code needed for anything. Smiley

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Online kevglass

JGO Kernel


Medals: 171
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #8 - Posted 2014-07-04 18:23:27 »

Infact, Slick should do most of that for you. Just bind to an offscreen graphics context.

Cheers

Kev

Offline fuzmeister

Junior Member


Medals: 1
Projects: 1
Exp: 12 years


I make music and games.


« Reply #9 - Posted 2014-07-08 00:59:59 »

Hey guys! I got it! Thank you! Smiley

- Steve

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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

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

BurntPizza (54 views)
2014-09-19 03:14:18
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!