Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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]OpenGL Context  (Read 3804 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-12-02 01:20:01 »

Hi,
I recently added the screenshot feature to my LWJGL game but noticed that there was a slight delay when taking a screenshot. To fix the issue I created a second thread that will take care of screenshots to hopefully remove the tiny delay but I get the following error ->
1  
2  
3  
4  
5  
Exception in thread "Thread-1" java.lang.RuntimeException: No OpenGL context found in the current thread.
   at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
   at org.lwjgl.opengl.GL11.glReadBuffer(GL11.java:2463)
   at com.Vlad.AuFait.AuFait.screenShot(AuFait.java:785)
   at com.Vlad.AuFait.ScreenShot.run(ScreenShot.java:30)

The only thing I know about OpenGL context is that "Display.create()" creates it.
Line 30 calls this function.

This function works. I am getting the no context error when my screenshot thread tries to call 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  
// Code from LWJGL.org
  public void screenShot() {
      GL11.glReadBuffer(GL11.GL_FRONT);
      int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
     ByteBuffer buffer = BufferUtils.createByteBuffer(DISPLAY_CURRENT_WIDTH * DISPLAY_CURRENT_HEIGHT * bpp);
      GL11.glReadPixels(0, 0, DISPLAY_CURRENT_WIDTH, DISPLAY_CURRENT_HEIGHT, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

      File file = new File("Screenshot.PNG"); // The file to save to.
     String format = "PNG"; // Example: "PNG" or "JPG"
     BufferedImage image = new BufferedImage(DISPLAY_CURRENT_WIDTH, DISPLAY_CURRENT_HEIGHT, BufferedImage.TYPE_INT_RGB);
     
      for (int x = 0; x < DISPLAY_CURRENT_WIDTH; x++) {
         for (int y = 0; y < DISPLAY_CURRENT_HEIGHT; y++) {
            int i = (x + (DISPLAY_CURRENT_WIDTH * y)) * bpp;
            int r = buffer.get(i) & 0xFF;
            int g = buffer.get(i + 1) & 0xFF;
            int b = buffer.get(i + 2) & 0xFF;
            image.setRGB(x, DISPLAY_CURRENT_HEIGHT - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
         }
      }
     
      try {
         ImageIO.write(image, format, file);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


My question: How do I share the OpenGL context? What should I pass onto my second thread?

Note: Yeah GL11 is deprecated, I'll work on using more updated OpenGL.

Thanks for your time.
Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #1 - Posted 2012-12-02 01:38:59 »

As far as I know, there can only be one OpenGL context per thread if there is any at all.

You would have to call Display.create() in the new thread to get another OpenGL context, but that obviously won't work as it has to be the same context for the screenshot to work.

I have no idea if you can achieve this. I don't believe it's even possible.

What you could do to reduce the lag is start a new Thread once you've read the data from the screen. Then you can convert the data in the buffer to a BufferedImage and write that in the separate Thread.
This way, the only lag caused will be from reading the screen to a buffer.

EDIT: GL11 is deprecated?? Since when? Any why? And what should I be using?  Huh

By the way, is there a difference between the BACK buffer and the FRONT buffer when taking screenshots?

Offline Vladiedoo
« Reply #2 - Posted 2012-12-02 01:45:21 »

What you could do to reduce the lag is start a new Thread once you've read the data from the screen. Then you can convert the data in the buffer to a BufferedImage and write that in the separate Thread.
This way, the only lag caused will be from reading the screen to a buffer.

EDIT: GL11 is deprecated?? Since when? Any why? And what should I be using?  Huh

Thanks I'll try that, oh and about the GL11 I believe I remember reading it in a post here, I am not sure what is better, I have a lot to learn.

EDIT: I found the post.
http://www.java-gaming.org/topics/lwjgl-struggling-with-vbo-s/27926/view.html

"Did you import LWJGL and why are you using OGL3? I think you could maybe do it with GL11 wthout much problems." (masteryoom)

"Why use OpenGL at all? He could probably use something even more deprecated.

+1 to OGL3." (theagentd)

EDIT 2: I am not sure on the exact difference between BACK and FRONT buffer either, sorry.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-12-02 02:02:48 »

1. The lag is likely because you are using setRGB which is very slow. For modifying pixels, you should grab the pixel array from the DataBuffer of a BufferedImage and change it directly.

2. The lag may also be due to ImageIO output, which can easily be put into a separate thread as it does not use any OpenGL.

3. LWJGL includes some features for multiple contexts and calling GL from different threads, but it would be overkill for a simple screenshot function.

4. GL 1.1 is not deprecated, although many "fixed function" methods from 1.1 (e.g. glVertex2f) are deprecated in favour of shaders and the programmable pipeline (GL 3+). Things like glReadPixels are not deprecated.

Offline Vladiedoo
« Reply #4 - Posted 2012-12-02 02:10:58 »

Awesome, thanks for all the information.

I actually do not need to modify pixels though, I should have mentioned my purpose behind this is so that I can take the screenshot and use it as a wall texture (it serves game-play functionality).
1  
2  
// This will then become binded to a quad.
screenShotTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("Screenshot.PNG"));


I'll make sure to give my second thread the ImageIO output.

Thanks again.
Offline davedes
« Reply #5 - Posted 2012-12-02 02:19:51 »

Why do you need to output the image to a file? No wonder you are getting lag, if you are saving the file, and then loading it back... persecutioncomplex

Just upload the pixels to a texture.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
Image outImage;

... init ... {
      outImage = new Image(SCREEN_WIDTH, SCREEN_HEIGHT);
}

public void screenShot() {
      GL11.glReadBuffer(GL11.GL_FRONT);    
      ByteBuffer buffer = BufferUtils.createByteBuffer(SCREEN_WIDTH * SCREEN_HEIGHT * 4);
      GL11.glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

      //upload data to our image
     outImage.bind();
      GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
}

Offline Vladiedoo
« Reply #6 - Posted 2012-12-02 02:29:20 »

That is much faster, thank you so much Grin!

Yeah I thought it was a bit silly to do that whole cycle, I didn't know a way around it.
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.

xsi3rr4x (34 views)
2014-04-15 18:08:23

BurntPizza (30 views)
2014-04-15 03:46:01

UprightPath (45 views)
2014-04-14 17:39:50

UprightPath (28 views)
2014-04-14 17:35:47

Porlus (45 views)
2014-04-14 15:48:38

tom_mai78101 (66 views)
2014-04-10 04:04:31

BurntPizza (126 views)
2014-04-08 23:06:04

tom_mai78101 (226 views)
2014-04-05 13:34:39

trollwarrior1 (191 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!