Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
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  
  Generating Mipmap Problems  (Read 9546 times)
0 Members and 1 Guest are viewing this topic.
Offline sunsett

Senior Devvie




ribbit!


« Posted 2009-07-20 02:27:11 »

I'm doing my on MIPmap generation and I've gotten content to display on the screen at lower resolutions it doesn't appear quite right:



If I expand the window to be larger so it doesn't use the mipmapping then I get the correctly displaying image:



The following is the code I'm using and though I know it's incredibly inefficient I am simply trying to learn how to generate mipmaps on my own:

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  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;

import com.sun.opengl.util.BufferUtil;

public class TestMipmapping extends TestBasic {
   private int[] vertexes;
   private int[] textures;
   private int[] textureVbo;
   
   private int programId;
   private int vertexShaderId;
   private int fragmentShaderId;
   
   private FloatBuffer textureCoordinates;
   
   public void initialize(GL gl) {
      super.initialize(gl);
     
      BufferedImage image = null;
     
      try {
         image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
      } catch(Throwable t) {
         t.printStackTrace();
      }
     
      // Allocate my buffer to hold my two triangles to make up a quad
      FloatBuffer buffer = BufferUtil.newFloatBuffer(12); {
         buffer.put(0.0f);      // Bottom-Left
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(image.getWidth());      // Bottom-Right
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(image.getWidth());      // Top-Right
         buffer.put(image.getHeight());
         buffer.put(0.0f);
         
         buffer.put(0.0f);      // Top-Left
         buffer.put(image.getHeight());
         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
     
      textureCoordinates = BufferUtil.newFloatBuffer(8); {
         textureCoordinates.put(0.0f);      // Top-Left
         textureCoordinates.put(1.0f);
         
         textureCoordinates.put(1.0f);      // Top-Right
         textureCoordinates.put(1.0f);
         
         textureCoordinates.put(1.0f);      // Bottom-Right
         textureCoordinates.put(0.0f);
         
         textureCoordinates.put(0.0f);      // Bottom-Left
         textureCoordinates.put(0.0f);
         
         textureCoordinates.rewind();
      }
     
      textureVbo = new int[1];
      gl.glGenBuffersARB(1, textureVbo, 0);
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
      gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, textureCoordinates.capacity() * 4, textureCoordinates, GL.GL_STATIC_DRAW_ARB);
     
      // Create and bind texture
      textures = new int[1];
      gl.glGenTextures(1, textures, 0);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
      try {
         DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
         byte[] data = dbb.getData();
         ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
         pixels.put(data);
         pixels.flip();
         
         int mode = 3;         // Modes: 1 = no mipmapping, 2 = auto-mipmapping, 3 = custom mipmapping
         
         gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
         gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, mode == 1 ? GL.GL_LINEAR : GL.GL_LINEAR_MIPMAP_LINEAR);
         if (mode == 2) {
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
         }
         gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, image.getWidth(), image.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
         if (mode == 3) {
            // Generate mipmaps
            int w = image.getWidth();
            int h = image.getHeight();
            int position = 1;
            while ((w > 1) || (h > 1)) {
               w /= 2;
               h /= 2;
               
               if (w == 0) {
                  w = 1;
               } else if (h == 0) {
                  h = 1;
               }
               
               BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
               Graphics2D g = bi.createGraphics();
               g.drawImage(image, 0, 0, w, h, null);
               g.dispose();
               
               DataBufferByte dbb2 = (DataBufferByte)bi.getRaster().getDataBuffer();
               byte[] data2 = dbb2.getData();
               ByteBuffer pixels2 = BufferUtil.newByteBuffer(data2.length);
               pixels2.put(data2);
               pixels2.flip();
               
               gl.glTexImage2D(GL.GL_TEXTURE_2D, position, GL.GL_RGB, bi.getWidth(), bi.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels2);
               
               position++;
            }
           
            System.out.println("Positions Created: " + position);
         }
      } 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!");
      }
     
      // Validate log results
      gl.glValidateProgramARB(programId);
      gl.glGetObjectParameterivARB(programId, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, result, 0);
      int length = result[0];
      if (length > 0) {
         byte[] log = new byte[length];
         gl.glGetInfoLogARB(programId, log.length, new int[length], 0, log, 0);
         System.out.println("Log Result: " + new String(log) + " (" + length + ")");
      }
     
      gl.glUseProgramObjectARB(programId);
     
      int myTexture = gl.glGetUniformLocationARB(programId, "myTexture");
      gl.glUniform1iARB(myTexture, 0);
   }
   
   public void draw(GL gl) {
      gl.glTranslatef(-400.0f, 0.0f, -800.0f);
     
      gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
      gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

      gl.glEnableClientState(GL.GL_VERTEX_ARRAY); {
         gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);
         gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);      
         gl.glDrawArrays(GL.GL_QUADS, 0, 4);
      }
      gl.glDisableClientState(GL.GL_VERTEX_ARRAY);  
   }
   
   public static void main(String[] args) throws Exception {
      new TestMipmapping();
   }
}


Any help is greatly appreciated.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2009-07-20 03:49:43 »

My guess is that you're looping through the layers incorrectly so that not all image levels are configured, and the weird image is referencing bad texture data.

You can calculate the total number of mipmaps in a 2d texture using:
1  
(int) Math.floor(Math.log(Math.max(width, height)) / Math.log(2)) + 1;


Then change your loop to:
1  
2  
3  
4  
5  
6  
for (int i = 1; i < numMipmaps; i++) {
   w = Math.max(1, w / 2);
   h = Math.max(1, h / 2);

   ... then proceed as before, except use i in glTexImage2D instead of position
}

Make sure to start the loop at 1 since you make the 0th level image outside of it in your code.

Let me know if that changes anything.

Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2009-07-20 06:12:54 »

This one is quite obvious: in your 2nd mipmap level, your pixels are off by -1 pixel per row.

Probably caused by:
1  
2  
               w /= 2;
               h /= 2;


You might want to try to round up.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sunsett

Senior Devvie




ribbit!


« Reply #3 - Posted 2009-07-20 16:19:07 »

This one is quite obvious: in your 2nd mipmap level, your pixels are off by -1 pixel per row.

Probably caused by:
1  
2  
               w /= 2;
               h /= 2;


You might want to try to round up.

Rounding up seemed to cause me to end up with one too few mipmap entries.

My guess is that you're looping through the layers incorrectly so that not all image levels are configured, and the weird image is referencing bad texture data.

You can calculate the total number of mipmaps in a 2d texture using:
1  
(int) Math.floor(Math.log(Math.max(width, height)) / Math.log(2)) + 1;


Then change your loop to:
1  
2  
3  
4  
5  
6  
for (int i = 1; i < numMipmaps; i++) {
   w = Math.max(1, w / 2);
   h = Math.max(1, h / 2);

   ... then proceed as before, except use i in glTexImage2D instead of position
}

Make sure to start the loop at 1 since you make the 0th level image outside of it in your code.

Let me know if that changes anything.

This approach left me with the exact same results.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2009-07-20 17:23:20 »

Rounding up seemed to cause me to end up with one too few mipmap entries.
You don't need to specify the maximum number of mipmap levels.


This approach left me with the exact same results.
Ofcourse.

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

Senior Devvie




ribbit!


« Reply #5 - Posted 2009-07-20 18:38:47 »

You don't need to specify the maximum number of mipmap levels.

Well, I guess more relevantly, nothing displays if I do it that way.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #6 - Posted 2009-07-20 20:09:32 »

I don't know what the actual dimensions are for your images, but try adding gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1) before you start calling glTexImage2D.

Offline sunsett

Senior Devvie




ribbit!


« Reply #7 - Posted 2009-07-20 23:43:42 »

Hey thanks, that seems to have fixed that problem, but caused a new one. Smiley



As I make the window smaller it becomes more blue, and as I make it larger it gets closer to the correct yellow color my duck should be. Shocked  I output the generated images and they all appear yellow as they should.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2009-07-21 00:49:21 »

Hm...


correct => Yellow = (255,255,0), Red = (255,0,0)
incorrect => Aqua = (0,255,255), Blue = (0,0,255)


So the Red and Blue channel are swapped.

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

Senior Devvie




ribbit!


« Reply #9 - Posted 2009-07-21 12:42:13 »

Hm...

correct => Yellow = (255,255,0), Red = (255,0,0)
incorrect => Aqua = (0,255,255), Blue = (0,0,255)

So the Red and Blue channel are swapped.

That makes sense, but I'm at a loss as to how to fix it.  I tried switching to GL_BGR but that stops anything from appearing.  I'm matching my mipmapping exactly to my original 0 call so I don't understand why the mipmaps would be incorrect?

Here is the current 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  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;

import com.sun.opengl.util.BufferUtil;

public class TestMipmapping extends TestBasic {
   private int[] vertexes;
   private int[] textures;
   private int[] textureVbo;
   
   private int programId;
   private int vertexShaderId;
   private int fragmentShaderId;
   
   private FloatBuffer textureCoordinates;
   
   public void initialize(GL gl) {
      super.initialize(gl);
     
      BufferedImage image = null;
     
      try {
         image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
      } catch(Throwable t) {
         t.printStackTrace();
      }
     
      // Allocate my buffer to hold my two triangles to make up a quad
      FloatBuffer buffer = BufferUtil.newFloatBuffer(12); {
         buffer.put(0.0f);      // Bottom-Left
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(image.getWidth());      // Bottom-Right
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(image.getWidth());      // Top-Right
         buffer.put(image.getHeight());
         buffer.put(0.0f);
         
         buffer.put(0.0f);      // Top-Left
         buffer.put(image.getHeight());
         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
     
      textureCoordinates = BufferUtil.newFloatBuffer(8); {
         textureCoordinates.put(0.0f);      // Top-Left
         textureCoordinates.put(1.0f);
         
         textureCoordinates.put(1.0f);      // Top-Right
         textureCoordinates.put(1.0f);
         
         textureCoordinates.put(1.0f);      // Bottom-Right
         textureCoordinates.put(0.0f);
         
         textureCoordinates.put(0.0f);      // Bottom-Left
         textureCoordinates.put(0.0f);
         
         textureCoordinates.rewind();
      }
     
      textureVbo = new int[1];
      gl.glGenBuffersARB(1, textureVbo, 0);
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
      gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, textureCoordinates.capacity() * 4, textureCoordinates, GL.GL_STATIC_DRAW_ARB);
     
      // Create and bind texture
      textures = new int[1];
      gl.glGenTextures(1, textures, 0);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
      try {
         DataBufferByte dbb = (DataBufferByte)image.getRaster().getDataBuffer();
         byte[] data = dbb.getData();
         ByteBuffer pixels = BufferUtil.newByteBuffer(data.length);
         pixels.put(data);
         pixels.flip();
         
         int mode = 3;         // Modes: 1 = no mipmapping, 2 = auto-mipmapping, 3 = custom mipmapping
         
         gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
         gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, mode == 1 ? GL.GL_LINEAR : GL.GL_LINEAR_MIPMAP_LINEAR);
         if (mode == 2) {
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
         }
         gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
         gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, image.getWidth(), image.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
         if (mode == 3) {
            // Generate mipmaps
            int mipmaps = (int)Math.floor(Math.log(Math.max(image.getWidth(), image.getHeight())) / Math.log(2)) + 1;
            int w = image.getWidth();
            int h = image.getHeight();
            for (int i = 1; i < mipmaps; i++) {
               w = Math.max(1, w / 2);
               h = Math.max(1, h / 2);
               
               if (w == 0) {
                  w = 1;
               } else if (h == 0) {
                  h = 1;
               }
               
               System.out.println(w + "x" + h + " - " + i);
               
               BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
               Graphics2D g = bi.createGraphics();
               g.drawImage(image, 0, 0, w, h, null);
               g.dispose();
               
               DataBufferByte dbb2 = (DataBufferByte)bi.getRaster().getDataBuffer();
               byte[] data2 = dbb2.getData();
               ByteBuffer pixels2 = BufferUtil.newByteBuffer(data2.length);
               pixels2.put(data2);
               pixels2.flip();
               
               gl.glTexImage2D(GL.GL_TEXTURE_2D, i, GL.GL_RGB, bi.getWidth(), bi.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels2);
            }
           
            System.out.println("Positions Created: " + mipmaps);
         }
      } 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!");
      }
     
      // Validate log results
      gl.glValidateProgramARB(programId);
      gl.glGetObjectParameterivARB(programId, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, result, 0);
      int length = result[0];
      if (length > 0) {
         byte[] log = new byte[length];
         gl.glGetInfoLogARB(programId, log.length, new int[length], 0, log, 0);
         System.out.println("Log Result: " + new String(log) + " (" + length + ")");
      }
     
      gl.glUseProgramObjectARB(programId);
     
      int myTexture = gl.glGetUniformLocationARB(programId, "myTexture");
      gl.glUniform1iARB(myTexture, 0);
   }
   
   public void draw(GL gl) {
      gl.glTranslatef(-400.0f, 0.0f, -800.0f);
     
      gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
      gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

      gl.glEnableClientState(GL.GL_VERTEX_ARRAY); {
         gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);
         gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);      
         gl.glDrawArrays(GL.GL_QUADS, 0, 4);
      }
      gl.glDisableClientState(GL.GL_VERTEX_ARRAY);  
   }
   
   public static void main(String[] args) throws Exception {
      new TestMipmapping();
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2009-07-21 13:58:56 »

It just occured to me that having swapped Red and Blue channels, is exactly the same as a completely swapped pixel, as green is the same because it is in the middle of the 3 bytes.

maybe this will fix it...
glPixelStorei(GL_PACK_SWAP_BYTES, GL_TRUE);
or...
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);

http://pyopengl.sourceforge.net/documentation/manual/glPixelStore.3G.html

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

Senior Devvie




ribbit!


« Reply #11 - Posted 2009-07-21 15:48:51 »

Tried each individually and both at the same time but made no difference.
Offline Jake R

Junior Newbie





« Reply #12 - Posted 2009-07-21 16:16:24 »

(First post here ever:)

My personal guess, although I am very unexperienced so don't hurt me if I'm wrong, is that this line is the problem:

1  
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);


Note the 'BGR' at the end, opposed to the more common 'RGB', in effect having the R(ed) and B(lue) channel swapped...
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2009-07-21 16:46:45 »

Doh! Shocked


In my defense... I didn't see it.


 Lips Sealed persecutioncomplex

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

Senior Devvie




ribbit!


« Reply #14 - Posted 2009-07-21 16:54:29 »

I thought that as well, but why does it work fine when not using mipmapping?

If I don't enable mipmapping at all (or set mode = 1 in that code) everything looks perfectly fine.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2009-07-21 17:02:47 »

Because

1  
2  
3  
4  
5  
6  
7  
BufferedImage image = null;
     
      try {
         image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
      } catch(Throwable t) {
         t.printStackTrace();
      }


returns a BufferedImage that is RGB..? Only your scaled versions are BGR.

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

Junior Newbie





« Reply #16 - Posted 2009-07-21 17:24:33 »

I thought that as well, but why does it work fine when not using mipmapping?

If I don't enable mipmapping at all (or set mode = 1 in that code) everything looks perfectly fine.

Because:

1  
2  
3  
4  
5  
6  
7  
8  
9  
if (mode == 3)
{
   // Generate mipmaps
   <snip>
               
      BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);

   <snip>
}


In other words: the typo is only called in 'mode 3', or 'custom mipmap mode'. In mode 1 or 2, it isn't called at all, and thus the typo is never reached =)

[edit:] Clean-up and clarification. [/edit]
Offline sunsett

Senior Devvie




ribbit!


« Reply #17 - Posted 2009-07-21 18:00:48 »

Hey, changing it fixed the problem.  I swapped out for the alpha mode I know is supported and everything works correctly now.

Thanks guys.  Just in case anyone is interested, here is the final source 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  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;

import com.sun.opengl.util.BufferUtil;

public class TestMipmapping extends TestBasic {
   private int[] vertexes;
   private int[] textures;
   private int[] textureVbo;
   
   private int programId;
   private int vertexShaderId;
   private int fragmentShaderId;
   
   private FloatBuffer textureCoordinates;
   
   public void initialize(GL gl) {
      super.initialize(gl);
     
      BufferedImage image = null;
     
      try {
         image = ImageIO.read(getClass().getClassLoader().getResource("resource/testing.png"));
         BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
         Graphics2D g = tmp.createGraphics();
         g.drawImage(image, 0, 0, null);
         g.dispose();
         image = tmp;
      } catch(Throwable t) {
         t.printStackTrace();
      }
     
      // Allocate my buffer to hold my two triangles to make up a quad
      FloatBuffer buffer = BufferUtil.newFloatBuffer(12); {
         buffer.put(0.0f);      // Bottom-Left
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(image.getWidth());      // Bottom-Right
         buffer.put(0.0f);
         buffer.put(0.0f);
         
         buffer.put(image.getWidth());      // Top-Right
         buffer.put(image.getHeight());
         buffer.put(0.0f);
         
         buffer.put(0.0f);      // Top-Left
         buffer.put(image.getHeight());
         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
     
      textureCoordinates = BufferUtil.newFloatBuffer(8); {
         textureCoordinates.put(0.0f);      // Top-Left
         textureCoordinates.put(1.0f);
         
         textureCoordinates.put(1.0f);      // Top-Right
         textureCoordinates.put(1.0f);
         
         textureCoordinates.put(1.0f);      // Bottom-Right
         textureCoordinates.put(0.0f);
         
         textureCoordinates.put(0.0f);      // Bottom-Left
         textureCoordinates.put(0.0f);
         
         textureCoordinates.rewind();
      }
     
      textureVbo = new int[1];
      gl.glGenBuffersARB(1, textureVbo, 0);
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
      gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, textureCoordinates.capacity() * 4, textureCoordinates, GL.GL_STATIC_DRAW_ARB);
     
      // Create and bind texture
      textures = new int[1];
      gl.glGenTextures(1, textures, 0);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
      try {
         DataBufferInt dbb = (DataBufferInt)image.getRaster().getDataBuffer();
         int[] data = dbb.getData();
         IntBuffer pixels = BufferUtil.newIntBuffer(data.length);
         pixels.put(data);
         pixels.flip();
         
         int mode = 3;         // Modes: 1 = no mipmapping, 2 = auto-mipmapping, 3 = custom mipmapping
         
         gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
         gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, mode == 1 ? GL.GL_LINEAR : GL.GL_LINEAR_MIPMAP_LINEAR);
         if (mode == 2) {
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP, GL.GL_TRUE);
         }
         gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
         gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL.GL_BGRA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
         if (mode == 3) {
            // Generate mipmaps
            int mipmaps = (int)Math.floor(Math.log(Math.max(image.getWidth(), image.getHeight())) / Math.log(2)) + 1;
            int w = image.getWidth();
            int h = image.getHeight();
            for (int i = 1; i < mipmaps; i++) {
               w = Math.max(1, w / 2);
               h = Math.max(1, h / 2);
               
               if (w == 0) {
                  w = 1;
               } else if (h == 0) {
                  h = 1;
               }
               
               System.out.println(w + "x" + h + " - " + i);
               
               BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
               Graphics2D g = bi.createGraphics();
               g.drawImage(image, 0, 0, w, h, null);
               g.dispose();
               
               DataBufferInt dbb2 = (DataBufferInt)bi.getRaster().getDataBuffer();
               int[] data2 = dbb2.getData();
               IntBuffer pixels2 = BufferUtil.newIntBuffer(data2.length);
               pixels2.put(data2);
               pixels2.flip();
               
               gl.glTexImage2D(GL.GL_TEXTURE_2D, i, GL.GL_RGBA, bi.getWidth(), bi.getHeight(), 0, GL.GL_BGRA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, pixels2);
            }
           
            System.out.println("Positions Created: " + mipmaps);
         }
      } 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!");
      }
     
      // Validate log results
      gl.glValidateProgramARB(programId);
      gl.glGetObjectParameterivARB(programId, GL.GL_OBJECT_INFO_LOG_LENGTH_ARB, result, 0);
      int length = result[0];
      if (length > 0) {
         byte[] log = new byte[length];
         gl.glGetInfoLogARB(programId, log.length, new int[length], 0, log, 0);
         System.out.println("Log Result: " + new String(log) + " (" + length + ")");
      }
     
      gl.glUseProgramObjectARB(programId);
     
      int myTexture = gl.glGetUniformLocationARB(programId, "myTexture");
      gl.glUniform1iARB(myTexture, 0);
   }
   
   public void draw(GL gl) {
      gl.glTranslatef(-400.0f, 0.0f, -800.0f);
     
      gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
      gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
      gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, textureVbo[0]);
      gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

      gl.glEnableClientState(GL.GL_VERTEX_ARRAY); {
         gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vertexes[0]);
         gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);      
         gl.glDrawArrays(GL.GL_QUADS, 0, 4);
      }
      gl.glDisableClientState(GL.GL_VERTEX_ARRAY);  
   }
   
   public static void main(String[] args) throws Exception {
      new TestMipmapping();
   }
}
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2009-07-21 18:15:31 »

1  
2  
3  
4  
5  
6  
7  
8  
               w = Math.max(1, w / 2);
               h = Math.max(1, h / 2);
               
               if (w == 0) {
                  w = 1;
               } else if (h == 0) {
                  h = 1;
               }


w and h will never be 0



Also, call flush in the BufferedImages you're not going to use anymore. Otherwise you'll run out of memory pretty quickly.

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

Senior Devvie




ribbit!


« Reply #19 - Posted 2009-07-21 19:16:56 »

I'm going to revise this significantly now that I finally have something that works. Smiley

This seems to appear on the screen exactly as it should...is there anything else I need to do in order to properly support NPOT textures?

Now the only hurdle I have left is PBOs.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #20 - Posted 2009-07-21 21:33:30 »

Don't foget that it currently works on 1 card, on 1 sytem, with 1 driver version.

Welcome to support hell.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

Solater (886 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

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
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!