Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (764)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  BufferedImage + glTexImage2D  (Read 8249 times)
0 Members and 1 Guest are viewing this topic.
Offline sunsett

Senior Devvie




ribbit!


« Posted 2009-06-27 22:02:05 »

As prescribed by Riven I've been going down the "direct OpenGL" path and avoiding using the built-in helper features of JOGL at least until I understand the internals of OpenGL.  To this end I've been progressing well, but I keep finding myself stuck in certain Java-specific cases where an OpenGL book that references C-specific code does not help me to move forward.  Specifically, I am currently trying to understand texturing in OpenGL, and though I have been able to use the Texture and TextureData objects in JOGL I would like to directly make use of glTexImage2D to load BufferedImage data into my textures and have dug into the TextureData and Texture classes to further understand, but it seems like there's an awful lot of code there to handle all difference BufferedImage scenarios.

Does someone have a relatively simple example I can look at that converts the BufferedImage into the NIO Buffer necessary to pass to glTextImage2D?
Offline Riven
Administrator

« JGO Overlord »


Medals: 1342
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2009-06-27 22:51:01 »

Google: LWJGL texture upload

First hit - just skip everything about TGA and all that's remaining is how to upload bytes as a texture

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline sunsett

Senior Devvie




ribbit!


« Reply #2 - Posted 2009-06-27 23:45:54 »

Ah, that seems to have cleared up a lot of confusion. Thank you very much.

I am attempting to use GLSL shaders to map the texture but believe I'm missing something:

init:
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  
      // Allocate my buffer to hold my two triangles to make up a quad
      FloatBuffer buffer = FloatBuffer.allocate(18); {
         buffer.put(-1.0f);      // Top-Left
         buffer.put(1.0f);
         buffer.put(0.0f);
         
         buffer.put(1.0f);
         buffer.put(1.0f);
         buffer.put(0.0f);
         
         buffer.put(-1.0f);
         buffer.put(-1.0f);
         buffer.put(0.0f);
         
         buffer.put(-1.0f);
         buffer.put(-1.0f);
         buffer.put(0.0f);
         
         buffer.put(1.0f);
         buffer.put(-1.0f);
         buffer.put(0.0f);
         
         buffer.put(1.0f);
         buffer.put(1.0f);
         buffer.put(0.0f);
         
         buffer.rewind();
      }
     
      vertexes = new int[1];
      gl.glGenBuffersARB(1, vertexes, 0);                                                      // Generate a buffer id
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);                                    // Bind the buffer
      gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, buffer.capacity() * 4, buffer, GL.GL_STATIC_DRAW_ARB);      // Send the vertex buffer data to the video card
     
      // Create and bind texture
      textures = new int[1];
      gl.glGenTextures(1, textures, 0);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
      try {
         BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("resource/crate.png"));
         DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
         byte[] data = dbb.getData();
         ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
         pixels.put(data);
         pixels.flip();
         gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 256, 256, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
      } catch(Throwable t) {
         t.printStackTrace();
      }
     
      // Load the GLSL texturing functionality
      programId = gl.glCreateProgramObjectARB();
     
      // Vertex Shader
      vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
      String source = getShaderSource("texture_coordinates.vert");
      gl.glShaderSourceARB(vertexShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
      gl.glCompileShaderARB(vertexShaderId);
      int[] result = new int[1];
      gl.glGetShaderiv(vertexShaderId, GL.GL_COMPILE_STATUS, result, 0);
      if (result[0] != GL.GL_TRUE) {
         throw new RuntimeException("Compile of GLSL Shader failed!");
      }
      gl.glAttachObjectARB(programId, vertexShaderId);
     
      // Fragment Shader
      fragmentShaderId = gl.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
      source = getShaderSource("texture_coordinates.frag");
      gl.glShaderSourceARB(fragmentShaderId, 1, new String[] {source}, new int[] {source.length()}, 0);
      gl.glCompileShaderARB(fragmentShaderId);
      result = new int[1];
      gl.glGetShaderiv(fragmentShaderId, GL.GL_COMPILE_STATUS, result, 0);
      if (result[0] != GL.GL_TRUE) {
         throw new RuntimeException("Compile of GLSL Shader failed!");
      }
      gl.glAttachObjectARB(programId, fragmentShaderId);
     
      // Link program
      gl.glLinkProgramARB(programId);
      gl.glGetProgramiv(programId, GL.GL_LINK_STATUS, result, 0);
      if (result[0] != GL.GL_TRUE) {
         throw new RuntimeException("Linking of GLSL Shader failed!");
      }
      gl.glUseProgramObjectARB(programId);
      int texture0 = gl.glGetUniformLocationARB(programId, "texture0");
      gl.glUniform1iARB(texture0, 0);


draw:
1  
2  
3  
4  
5  
6  
7  
      gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);                                                   // Make it white
     
      gl.glEnableClientState(GL.GL_VERTEX_ARRAY);                                                // Enable vertex arrays
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);                                    // Bind the vertex buffer by id
      gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);                                                // Tell where to start on the vertex buffer
      gl.glDrawArrays(GL.GL_TRIANGLES, 0, 6);                                                   // Draw the vertex buffer data to the screen
      gl.glDisableClientState(GL.GL_VERTEX_ARRAY);                                             // Disable vertex arrays


Everything loads and the GLSL doesn't throw any errors. The shaders are as follows:

texture_coordinates.vert:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
#version 130

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

in vec3 a_Vertex;
in vec3 a_Color;
in vec2 a_TexCoord0;
out vec4 color;
out vec2 texCoord0;

void main(void) {
   texCoord0 = a_TexCoord0;
   color = vec4(a_Color, 1.0);
   vec4 pos = modelview_matrix * vec4(a_Vertex, 1.0);
   gl_Position = projection_matrix * pos;
}


texture_coordinates.frag:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
#version 130

uniform sampler2D texture0;

in vec4 color;
in vec2 texCoord0;

out vec4 outColor;

void main(void) {
   outColor = color * texture(texture0, texCoord0.st);
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1342
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2009-06-28 00:00:52 »

Google it.



Then you'll at least have a shader that stores a value in gl_FragColor.


Seriously.. if you'd copy & paste code you'd be better off.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline sunsett

Senior Devvie




ribbit!


« Reply #4 - Posted 2009-06-28 01:03:20 »

The problem is that I can't find a complete example that actually works.  Everything I'm finding is either snippets or code for a larger project. I did search before I posted and have tried several variations but cannot seem to narrow down why it's not working.

Does anyone have some sample code I can look at to understand this better?
Offline Riven
Administrator

« JGO Overlord »


Medals: 1342
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2009-06-28 11:44:23 »

Google: glsl tutorial


First hit. (lighthouse3d.com/......./glsl) for the full story.
Second hit: (lighthouse3d.com/........?shaders) to copy & paste



Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline sunsett

Senior Devvie




ribbit!


« Reply #6 - Posted 2009-06-28 13:21:49 »

I've actually already gone through that exact tutorial and copied the shader source exactly but to no avail.  I believe it has to do with my JOGL code, not the GLSL source that I'm stuck at this point.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1342
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2009-06-28 13:31:53 »

Then you'll at least have a shader that stores a value in gl_FragColor.

Don't you see that you don't write anything into gl_FragColor, while you should?

And every code snippet overthere has it, except if they only change a few lines of the previous shader, and show their code like:

1  
2  
3  
4  
5  
...

outColor = vec4(1,1,1,1);

...


I mean, the describing text there is very clear that it left stuff out that was already in the previous X provided samples.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline sunsett

Senior Devvie




ribbit!


« Reply #8 - Posted 2009-06-28 14:26:19 »

Yes I know, and I was able to correct my code to include reference to gl_FragColor, but that still has not solved my problem.  Because I've copied directly from other shader examples is why I believe that it's something in my JOGL-specific code that is the problem at this point.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #9 - Posted 2009-06-28 16:43:44 »

Try not using the ARB versions of the functions.  In a lot of cases you can just remove ARB, but a few use slightly different names.

Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

nelsongames (1026 views)
2018-04-24 18:15:36

nelsongames (1083 views)
2018-04-24 18:14:32

ivj94 (1664 views)
2018-03-24 14:47:39

ivj94 (590 views)
2018-03-24 14:46:31

ivj94 (1500 views)
2018-03-24 14:43:53

Solater (568 views)
2018-03-17 05:04:08
Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46

Deployment and Packaging
by philfrei
2018-08-19 23:53:08

Deployment and Packaging
by philfrei
2018-08-19 23:50:04

Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39
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!