Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Rendering textures via VBO  (Read 23191 times)
0 Members and 1 Guest are viewing this topic.
Offline JayManHall
« Posted 2014-10-05 03:29:31 »

Well, I have a basic quad being drawn using VBOs, and I have no idea how to draw a texture. I've been trying to search for the video i saw on it a while back but never found it.

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #1 - Posted 2014-10-05 03:33:02 »

Well, first of all you need the geometry to be aware of which part of the texture corresponds to which part of the vertex. These are called "Texture Coordinates" and usually range from 0, to 1. (Or texture UVs)



Next, we need to import that into a buffer, then point to that when pointing to the vertices.

Can we have a snippet of code? VBOs have many implementations over many OpenGL versions...

Offline JayManHall
« Reply #2 - Posted 2014-10-05 03:34:27 »

I'm just using coding universe 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  
package RenderingModes;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import java.nio.FloatBuffer;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

/**
 * Renders a colored triangle using Vertex Buffer Objects
 *
 * @author Oskar
 */

public class VertexBufferObjectDemo {

    public static void main(String[] args) {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("Vertex Buffer Object Demo");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(1, -1, 1, -1, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        final int amountOfVertices = 3;
        final int vertexSize = 3;
        final int colorSize = 3;
        //ignor below
        float trans = 0.1f;
       
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{-0.5f + trans, -0.5f, 0, 0.5f + trans, -0.5f, 0, 0.5f + trans, 0.5f, 0});
        vertexData.flip();

        FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
        colorData.put(new float[]{1, 0, 0, 0, 1, 0, 0, 0, 1/**/});
        colorData.flip();

        int vboVertexHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        int vboColorHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
        glBufferData(GL_ARRAY_BUFFER, colorData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);
           
            vertexData.clear();


           
           
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

            glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
            glColorPointer(colorSize, GL_FLOAT, 0, 0L);

            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);

            Display.update();
            Display.sync(60);
        }
       
        glDeleteBuffers(vboVertexHandle);
        glDeleteBuffers(vboColorHandle);

        Display.destroy();
        System.exit(0);
    }
}

With great code comes even greater disorganization.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #3 - Posted 2014-10-05 03:45:36 »

I'm just using coding universe code.

...

Good old OpenGL 1.5 Wink

We really just need to pack the per-vertex texture coordinates into another buffer...
Here's a few snippets. And I hope you already know how to load and bind textures. persecutioncomplex
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  
//...

FloatBuffer textureUVData = BufferUtils.createFloatBuffer(amountOfVertices * 2); // amount of vertices * two texture coordinates per-vertex
textureUVData.put(new float[]{0, 0, 0, 1, 0, 0, 1, 1, 0});
textureUVData.flip();
       
//...

int vboTextureUVHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
glBufferData(GL_ARRAY_BUFFER, textureUVData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

//...

glBindTexture(GL_TEXTURE_2D, myTextureID);

//...

glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
// 2 Because we have 2 texture coordinates per-vertex
glTexCoordPointer(2, GL_FLOAT, 0, 0L);
       
//...

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

//...

Offline JayManHall
« Reply #4 - Posted 2014-10-05 03:59:07 »

I honestly don't remember how to bind a texture. I remember doing it in immediate mode.

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #5 - Posted 2014-10-05 04:51:13 »

I honestly don't remember how to bind a texture. I remember doing it in immediate mode.

Read ALL of this: https://open.gl/textures

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
BufferedImage image = ImageIO.read(new File("..."));

//...

//During setup ONLY!!!
int myTextureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, myTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(bufferedImage));

//...

//During render/draw arrays
glBindTexture(GL_TEXTURE_2D, myTextureID);


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
/**
 * Creates a flipped byte buffer from a given buffered image
 * @param source The buffered image source to load from
 * @return A byte buffer interpolated from source
 */

private ByteBuffer dataFromImage(BufferedImage source){
   int[] pixels = new int[source.getWidth() * source.getHeight()];
   source.getRGB(0, 0, source.getWidth(), source.getHeight(), pixels, 0, source.getWidth());
   ByteBuffer buffer = BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4);
   
   for (int y = 0; y < source.getHeight(); y++) {
      for (int x = 0; x < source.getWidth(); x++) {
         int pixel = pixels[y * source.getWidth() + x];
         buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
         buffer.put((byte) ((pixel >> 8) & 0xFF));  // Green component
         buffer.put((byte) (pixel & 0xFF));         // Blue component
         buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
      }
   }buffer.flip();
   
   return buffer;
}


Also, if you're doing this in a engine, you probably want a base texture class, then a seperate class for wrapping texture functions to loading image functions. I've got a good example of that here:
https://github.com/ecumene-software/LibEcu/tree/master/source/main/ecumene/opengl/texture

EDIT: I'm changing the license of the repo to the un-lisence soon also. So don't worry about copyright laws. Wink

Offline Opiop
« Reply #6 - Posted 2014-10-05 12:16:27 »

@Ecumene Textures coordinates always range from 0 to 1, not sometimes like you said. Vertex buffer objects also have not changed from version to version, there aren't many different versions of VBOs. Just to clear that up.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #7 - Posted 2014-10-05 16:11:38 »

@Ecumene Textures coordinates always range from 0 to 1, not sometimes like you said. Vertex buffer objects also have not changed from version to version, there aren't many different versions of VBOs. Just to clear that up.

Yeah, I've noticed that in my OP. VBOs can be made using the GL15 buffer & vertex pointer methods, or the 3.0 vertex attribute methods, is what I meant. I shouldn't have used the words 'Usually'.

As for the texture coordinates, you can do a matrix transform to convert your texture coordinates from coordinates on the image, to vertex texture coordinates in the vertex shader. I haven't done it but I'm sure its possible.

Offline JayManHall
« Reply #8 - Posted 2014-10-05 17:00:48 »

1  
lTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(bufferedImage));


Where did that last parameter come from? I might have this working.

With great code comes even greater disorganization.
Offline Opiop
« Reply #9 - Posted 2014-10-05 17:01:30 »

Can you explain what you mean by the conversion to vertex coordinates? Texels are normalized so that you can apply them to any size geometry, are you talking about vertex attributes? Honestly, I would like to know, I've never heard of that!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #10 - Posted 2014-10-05 17:10:43 »

1  
lTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(bufferedImage));


Where did that last parameter come from? I might have this working.

Look at the code snippet below my OP.

Can you explain what you mean by the conversion to vertex coordinates? Texels are normalized so that you can apply them to any size geometry, are you talking about vertex attributes? Honestly, I would like to know, I've never heard of that!

I mean:

Vertex Positions -> Vertex Shader : Transform with Matrices -> Primitive Assembly -> Rasterization...

Tex Coordinates -> Vertex Shader : Transform from 'image' space to normalized space using uniform texture dimensions -> Primative Assembly -> Rasterization...

It's a concept I've been thinking about lately. Using a 2D texture matrix to transform texture coordinates by translation / rotation / scaling done by the vertex shader. It could be broken in many ways. Like for example the texture coordinates are probably clamped by default...

Also, Texel = Pixel from Texture. But even then, OpenGL pixels are usually clamped to 1.0 - 0.0 also... So I guess that's still right in an ironic sense. Wink

Offline JayManHall
« Reply #11 - Posted 2014-10-05 17:26:22 »

Alright, I did some work and here is my code. It throws an exception where I put the TextureUVData.
Also, the glText2D method; I had to create one, so I don't really know what goes there or where it comes from.

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  
package RenderingModes;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.imageio.ImageIO;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

/**
 * Renders a colored triangle using Vertex Buffer Objects
 *
 * @author Oskar
 */

public class VertexBufferObjectDemo {
   
   
   /**
    * Creates a flipped byte buffer from a given buffered image
    * @param source The buffered image source to load from
    * @return A byte buffer interpolated from source
    */

   private static ByteBuffer dataFromImage(BufferedImage source){
      int[] pixels = new int[source.getWidth() * source.getHeight()];
      source.getRGB(0, 0, source.getWidth(), source.getHeight(), pixels, 0, source.getWidth());
      ByteBuffer buffer = BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4);
     
      for (int y = 0; y < source.getHeight(); y++) {
         for (int x = 0; x < source.getWidth(); x++) {
            int pixel = pixels[y * source.getWidth() + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
            buffer.put((byte) ((pixel >> 8) & 0xFF));  // Green component
            buffer.put((byte) (pixel & 0xFF));         // Blue component
            buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
         }
      }buffer.flip();
     
      return buffer;
   }
   
    public static void main(String[] args) {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("Vertex Buffer Object Demo");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(1, -1, 1, -1, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        final int amountOfVertices = 3;
        final int vertexSize = 3;
        final int colorSize = 3;
        //ignor below
        float trans = 0.1f;
       
        FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
        vertexData.put(new float[]{-0.5f + trans, -0.5f, 0, 0.5f + trans, -0.5f, 0, 0.5f + trans, 0.5f, 0});
        vertexData.flip();

        FloatBuffer colorData = BufferUtils.createFloatBuffer(amountOfVertices * colorSize);
        colorData.put(new float[]{1, 0, 0, 0, 1, 0, 0, 0, 1/**/});
        colorData.flip();
       
        FloatBuffer textureUVData = BufferUtils.createFloatBuffer(amountOfVertices * 2); // amount of vertices * two texture coordinates per-vertex
        textureUVData.put(new float[]{0, 0, 0, 1, 0, 0, 1, 1, 0});
        textureUVData.flip();
       
       
       

        int vboVertexHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glBufferData(GL_ARRAY_BUFFER, vertexData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        int vboColorHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
        glBufferData(GL_ARRAY_BUFFER, colorData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
       
       
         
         BufferedImage image = null;
      try {
         image = ImageIO.read(new File("ufo.png"));
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
         
      //During setup ONLY!!!
        int myTextureID = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, myTextureID);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE);
       
        int vboTextureUVHandle = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
        glBufferData(GL_ARRAY_BUFFER, textureUVData, GL_DYNAMIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);  
       
       
        glGenTextures();
           
       

     

        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);
           
            vertexData.clear();


           
           
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

            glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
            glColorPointer(colorSize, GL_FLOAT, 0, 0L);
           
            glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
         // 2 Because we have 2 texture coordinates per-vertex
         glTexCoordPointer(2, GL_FLOAT, 0, 0L);

            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glBindTexture(GL_TEXTURE_2D, myTextureID);
            glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);

            Display.update();
            Display.sync(60);
        }
       
        glDeleteBuffers(vboVertexHandle);
        glDeleteBuffers(vboColorHandle);

        Display.destroy();
        System.exit(0);
    }

   private static void glTexImage2D(int glTexture2d, int i, int glRgb8,
         int width, int height, int j, int glRgba, int glUnsignedByte) {
      // TODO Auto-generated method stub
     
   }
}


EXCEPTION:
1  
2  
3  
4  
Exception in thread "main" java.nio.BufferOverflowException
   at java.nio.DirectFloatBufferU.put(Unknown Source)
   at java.nio.FloatBuffer.put(Unknown Source)
   at RenderingModes.VertexBufferObjectDemo.main(VertexBufferObjectDemo.java:87)

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #12 - Posted 2014-10-05 17:30:15 »

Hehehe, oops
1  
textureUVData.put(new float[]{0, 0, 1, 0, 1, 1});


*SMOKEBOMB*

Offline JayManHall
« Reply #13 - Posted 2014-10-05 17:34:29 »

The area where I load the buffered image. 1. It cant load my file from the res folder. 2. That glTexImage2D, where is that method coming from?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
BufferedImage image = null;
      try {
         image = ImageIO.read(new File("ufo.png"));
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
         
      //During setup ONLY!!!
        int myTextureID = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, myTextureID);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE);
       



This was made by default.
1  
2  
3  
4  
5  
   private static void glTexImage2D(int glTexture2d, int i, int glRgb8,
         int width, int height, int j, int glRgba, int glUnsignedByte) {
      // TODO Auto-generated method stub
     
   }

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #14 - Posted 2014-10-05 17:41:38 »

The area where I load the buffered image. 1. It cant load my file from the res folder. 2. That glTexImage2D, where is that method coming from?

1  
2  
3  
int myTextureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, myTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE);


You forgot the byte buffer holding your pixel data after 'GL_UNSIGNED_BYTE'. You can get a ByteBuffer from a buffered image through this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
/**
 * Creates a flipped byte buffer from a given buffered image
 * @param source The buffered image source to load from
 * @return A byte buffer interpolated from source
 */

private ByteBuffer dataFromImage(BufferedImage source){
   int[] pixels = new int[source.getWidth() * source.getHeight()];
   source.getRGB(0, 0, source.getWidth(), source.getHeight(), pixels, 0, source.getWidth());
   ByteBuffer buffer = BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4);
   
   for (int y = 0; y < source.getHeight(); y++) {
      for (int x = 0; x < source.getWidth(); x++) {
         int pixel = pixels[y * source.getWidth() + x];
         buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
         buffer.put((byte) ((pixel >> 8) & 0xFF));  // Green component
         buffer.put((byte) (pixel & 0xFF));         // Blue component
         buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
      }
   }

   buffer.flip();
   
   return buffer;
}


So...
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));

Offline JayManHall
« Reply #15 - Posted 2014-10-05 17:47:51 »

Thought I did that already, but I get syntax error
1  
The method glTexImage2D(int, int, int, int, int, int, int, int) in the type VertexBufferObjectDemo is not applicable for the arguments (int, int, int, int, int, int, int, int, ByteBuffer)

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #16 - Posted 2014-10-05 17:52:35 »

Remove the method from the base class and make sure it's imported statically. Or use:

GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));

Offline JayManHall
« Reply #17 - Posted 2014-10-05 18:03:48 »

Okay, now I think that it can't find my image.
1  
2  
3  
4  
5  
javax.imageio.IIOException: Can't read input file!
   at javax.imageio.ImageIO.read(Unknown Source)
   at RenderingModes.VertexBufferObjectDemo.main(VertexBufferObjectDemo.java:107)
Exception in thread "main" java.lang.NullPointerException
   at RenderingModes.VertexBufferObjectDemo.main(VertexBufferObjectDemo.java:116)



is my image in the right place? In the same package.
Click to Play

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #18 - Posted 2014-10-05 18:08:33 »

Put it here:

For:
new File("ufo.png")

1  
2  
3  
./project/ufo.png
./project/source/
./project/binaries/

Offline JayManHall
« Reply #19 - Posted 2014-10-05 18:12:51 »

Put it here:

For:
new File("ufo.png")

1  
2  
3  
./project/ufo.png
./project/source/
./project/binaries/


I don't get what you mean by this but I tried dragging the image into the project itself and it didn't work. I also tried putting it in the src folder alone, and still, it could not find it.

With great code comes even greater disorganization.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #20 - Posted 2014-10-05 18:14:10 »

http://www.java-gaming.org/topics/faq-cannot-find-the-resource-file/33226/view.html
Offline JayManHall
« Reply #21 - Posted 2014-10-05 19:29:41 »


Forgot about that post, but everything is working except I don't see the image/texture drawn.
My rendering 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  
glClear(GL_COLOR_BUFFER_BIT);
           
            vertexData.clear();


           
           
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

            glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
            glColorPointer(colorSize, GL_FLOAT, 0, 0L);
           
            glBindBuffer(GL_ARRAY_BUFFER, vboTextureUVHandle);
         // 2 Because we have 2 texture coordinates per-vertex
         glTexCoordPointer(2, GL_FLOAT, 0, 0L);

            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
            glBindTexture(GL_TEXTURE_2D, myTextureID);
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #22 - Posted 2014-10-05 19:39:02 »

1  
2  
glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
glBindTexture(GL_TEXTURE_2D, myTextureID);


to

1  
2  
glBindTexture(GL_TEXTURE_2D, myTextureID);
glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);


Also,

1  
2  
3  
4  
GL11.glEnable(GL11.GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, myTextureID);
glTexImage2D(...);

Offline JayManHall
« Reply #23 - Posted 2014-10-05 19:44:15 »

http://pastebin.java-gaming.org/6e10a2291051d
Still not rendered. Am I supposed to repeat some things that I call in the render method?
EX:
1  
glBindTexture();
I call it before and in the render loop?

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #24 - Posted 2014-10-05 19:49:34 »

Un comment the drawArrays Cranky

Also, I forgot this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
//Specifies how the texture will be handled when out - of - range
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

//Specifies how the texture should interpolate scaling (GL_NEAREST for nearest-neighbour, and GL11.GL_LINEAR for blurry stuffs)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

int myTextureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, myTextureID);        
GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));


Offline JayManHall
« Reply #25 - Posted 2014-10-05 20:01:55 »

It works, now how do I make this triangle a quad. I tried changing the vertex data.
1  
2  
3  
4  
5  
6  
7  
                            -0.5f, -0.5f, 0,
                             0.5f, -0.5f, 0,
                             0.5f, 0.5f, 0,
                             
                             -0.5f, -0.5f, 0,
                             0.5f, 0.5f, 0,
                             0.5f, -0.5f, 0,

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #26 - Posted 2014-10-05 20:03:59 »

It works, now how do I make this triangle a quad. I tried changing the vertex data.
1  
2  
3  
4  
5  
6  
7  
                            -0.5f, -0.5f, 0,
                             0.5f, -0.5f, 0,
                             0.5f, 0.5f, 0,
                             
                             -0.5f, -0.5f, 0,
                             0.5f, 0.5f, 0,
                             0.5f, -0.5f, 0,


Nice! Now the texture coordinates should be proportional to the vertex locations. Like this:

1  
2  
3  
4  
5  
6  
7  
0, 0,
1, 0,
1, 1,

0, 0,
1, 1,
1, 0

Offline JayManHall
« Reply #27 - Posted 2014-10-05 20:10:23 »

It didn't work right. I'll just figure out how to arrange the data to make a quad, but can I get a brief lecture on how the textcoords work?

what does this stand for.
1  
2  
3  
4  
5  
6  
7  
0, 0,
1, 0,
1, 1,

0, 0,
1, 1,
1, 0


and is there a way I can change the ortho so I don't have to use floats for drawing shapes and such

With great code comes even greater disorganization.
Offline Ecumene

JGO Kernel


Medals: 200
Projects: 4
Exp: 8 years


I did not hit her! I did not!


« Reply #28 - Posted 2014-10-05 20:18:47 »

It didn't work right. I'll just figure out how to arrange the data to make a quad, but can I get a brief lecture on how the textcoords work?
and is there a way I can change the ortho so I don't have to use floats for drawing shapes and such

Use the 0s and 1s I gave you in your tex-coord buffer.

Texture coordinates basically state which part of the texture corresponds to which part of the geometry (Your vertices). The UV system is just a way of representing that relationship.

Well, first of all you need the geometry to be aware of which part of the texture corresponds to which part of the vertex. These are called "Texture Coordinates" and usually range from 0, to 1. (Or texture UVs)



As for the 'ortho': Get a book about OpenGL (OpenGL Super-Bible 6th edition is good). Also learn vector/matrix math, SUPER useful in graphics programming / game development

1  
2  
3  
4  
5  
6  
7  
8  
//The matrix used to multiply vector locations in a projection sense
glMatrixMode(GL_PROJECTION);
//Loads the identity matrix into projection (google Identity Matrix)
glLoadIdentity();
//Set the current matrix (projection) to have an ortho projection
glOrtho2D(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
//Model-view: The model and view matrix together
glMatrixMode(GL_MODELVIEW);

Offline Longarmx
« Reply #29 - Posted 2014-10-05 20:21:45 »

Un comment the drawArrays Cranky

Also, I forgot this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
//Specifies how the texture will be handled when out - of - range
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

//Specifies how the texture should interpolate scaling (GL_NEAREST for nearest-neighbour, and GL11.GL_LINEAR for blurry stuffs)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

int myTextureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, myTextureID);        
GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, dataFromImage(image));



You shouldn't be doing this every frame. Do it once when you load the texture. I noticed that it was in your loop JayManHall.

It didn't work right. I'll just figure out how to arrange the data to make a quad, but can I get a brief lecture on how the textcoords work?

what does this stand for.
1  
2  
3  
4  
5  
6  
7  
0, 0,
1, 0,
1, 1,

0, 0,
1, 1,
1, 0


These are the UV coordinates of the texture that you are using. Each coordinate (each line for U, V) says what part of the texture that vertex should represent. (0, 0) starts in the top-left, and (1, 1) is in the bottom right. They're basically like Cartesian coordinates for the texture.

So, this code is saying that the first vertex should take the top-left corner of the texture, the 2nd vertex should be the top-right corner of the texture, the 3rd should be the bottom-right, and so on.

and is there a way I can change the ortho so I don't have to use floats for drawing shapes and such

You can multiply your vertex positions by an orthographic projection matrix, which will let you use units like pixels, and will normalize those coordinates for opengl to use.

1  
2  
3  
4  
5  
6  
7  
8  
//The matrix used to multiply vector locations in a projection sense
glMatrixMode(GL_PROJECTION);
//Loads the identity matrix into projection (google Identity Matrix)
glLoadIdentity();
//Set the current matrix (projection) to have an ortho projection
glOrtho2D(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
//Model-view: The model and view matrix together
glMatrixMode(GL_MODELVIEW);


Don't use this. Instead, jump straight into modern opengl. Don't try to tie the deprecated and programmable pipelines together.

Pages: [1] 2
  ignore  |  Print  
 
 

 
Riven (208 views)
2019-09-04 15:33:17

hadezbladez (4950 views)
2018-11-16 13:46:03

hadezbladez (1828 views)
2018-11-16 13:41:33

hadezbladez (5228 views)
2018-11-16 13:35:35

hadezbladez (1034 views)
2018-11-16 13:32:03

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

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

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

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

nelsongames (4314 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

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