Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  How to add vertices to VBO?  (Read 1542 times)
0 Members and 1 Guest are viewing this topic.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #30 - Posted 2013-12-29 09:16:23 »

Your texture coordinates don't seem to be correct. Try this;

1  
2  
3  
4  
5  
6  
7  
8  
9  
float[] datatex = {
               100f, 100f, 0f,
               100f, 500f, 0f,
               500f, 100f, 0f,
               500f, 500f, 0f,
               0f, 0f,
               0f, 1f,
               1f, 0f,
               1f, 1f};

Why are all OpenGL tutorials written in Brainf**k?
Offline Doubstract
« Reply #31 - Posted 2013-12-29 09:20:50 »

still nothing.

maybe you need other part of code?
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #32 - Posted 2013-12-29 09:23:07 »

Oh your texture co-ordinates would have been correct before if you have a top-left coordinate system. Can you show the code you use for intermediate mode? The code you provided should work perfectly well.

Why are all OpenGL tutorials written in Brainf**k?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Doubstract
« Reply #33 - Posted 2013-12-29 09:27:05 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.id);
      GL11.glBegin(GL_QUADS);
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2f(0, 0);
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex2f(100, 0);
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex2f(100, 100);
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex2f(0, 100);
      GL11.glEnd();
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);


And it appears in top left corner
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #34 - Posted 2013-12-29 09:42:59 »

Post your code for setting up the texture. I've not come across a problem like this before.

Why are all OpenGL tutorials written in Brainf**k?
Offline Doubstract
« Reply #35 - Posted 2013-12-29 09:50:30 »

I am using this texture load class:
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  
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

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

/**
 * An OpenGL Texture.
 *
 * @author Sri Harsha Chilakapati
 */

public class Texture
{
   
    /**
     * Texture ID.
     */

    public int id;
   
    /**
     * Texture width.
     */

    public int width;
   
    /**
     * Texture height.
     */

    public int height;
   
    /**
     * Creates a texture.
     */

    private Texture(int id, int width, int height)
    {
        this.id = id;
        this.width = width;
        this.height = height;
    }
   
    /**
     * Loads a texture from a file.
     *
     * @param name The name of the file.
     */

    public static Texture loadTexture(String name)
    {
        // Load the image
       BufferedImage bimg = null;
        try
        {
            bimg = ImageIO.read(Texture.class.getClassLoader().getResourceAsStream(name));
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.out.println("Unable to load Texture: " + name);
            Game.end();
        }

        // Gather all the pixels
       int[] pixels = new int[bimg.getWidth() * bimg.getHeight()];
        bimg.getRGB(0, 0, bimg.getWidth(), bimg.getHeight(), pixels, 0, bimg.getWidth());

        // Create a ByteBuffer
       ByteBuffer buffer = BufferUtils.createByteBuffer(bimg.getWidth() * bimg.getHeight() * 4);

        // Iterate through all the pixels and add them to the ByteBuffer
       for(int y = 0; y < bimg.getHeight(); y++)
        {
            for(int x = 0; x < bimg.getWidth(); x++)
            {
                // Select the pixel
               int pixel = pixels[y * bimg.getWidth() + x];
                // Add the RED component
               buffer.put((byte) ((pixel >> 16) & 0xFF));
                // Add the GREEN component
               buffer.put((byte) ((pixel >> 8) & 0xFF));
                // Add the BLUE component
               buffer.put((byte) (pixel & 0xFF));
                // Add the ALPHA component
               buffer.put((byte) ((pixel >> 24) & 0xFF));
            }
        }

        // Reset the read location in the buffer so that GL can read from beginning.
       buffer.flip();

        // Generate a texture ID
       int textureID = glGenTextures();
        // Bind the ID to the context
       glBindTexture(GL_TEXTURE_2D, textureID);

        //Setup texture scaling filtering
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        //Send texture data to OpenGL
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, bimg.getWidth(), bimg.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        // Return a new Texture.
       return new Texture(textureID, bimg.getWidth(), bimg.getHeight());
    }

}





1  
2  
3  
  glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


1  
  texture = Texture.loadTexture("simpletex.png");


1  
Texture texture;


Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #36 - Posted 2013-12-29 10:05:00 »

Enabling blending might be causing the problem. Try commenting it out and see if that fixes it. If it doesn't, I have no other suggestions. The code your using seems to me like it should work perfectly.

Why are all OpenGL tutorials written in Brainf**k?
Offline Doubstract
« Reply #37 - Posted 2013-12-29 11:31:29 »

still not working, but when i am using this tutorial code, textures working perfect.

And i want to ask, should i use two VBOs in one chunk (one for textures, one for vertices). I want each tile in chunk have different textures. And i think i must use spritesheet, or am i wrong?

Offline StumpyStrust
« Reply #38 - Posted 2013-12-29 12:07:55 »

Spritesheets make it so you you can only bind one texture and render different parts of said texture. Analogues to how a normal sprite animations work but with different tiles instead of frames. Binding a new texture can be expensive

With modern opengl this is no longer a problem really.

without spritesheets.

Build chunks with all tiles using same texture.

Render chunks.
bindTexture for chunk
renderChunk()
bind
renderChunk()
.
.
.
etc

With one giant texture(2048x2048)

bind texture

render all chunks


For building VBO I say to pass a bytebuffer around and inteleave the VBO like such
V = vertex
C = color
T = texture
VCTVCTVCT
Then flip and upload.

Here is some code snippets. Warning not using modern stuff.
1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void build(ByteBuffer verts, int size)
   {
      if (vboID == -1)
         genVBO();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verts, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
      this.size = size;
   }

Uploads data to gpu after bytebuffer is built.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
   public void render()
   {
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
      GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
      GL11.glVertexPointer(3, GL11.GL_FLOAT, 27, 0);
      GL11.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 27, 12);
      GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 27, 16);
      GL11.glNormalPointer(GL11.GL_BYTE, 27, 24);

      GL11.glDrawArrays(GL11.GL_QUADS, 0, size * 4);

      GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
      GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);

      GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
   }

Rendering using interleaved VBO without indices.

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  
   private void addFront(ByteBuffer buff)
   {
      buff.putFloat(pos.x + .5f); // vertex
     buff.putFloat(pos.y + .5f);
      buff.putFloat(pos.z - .5f);
      addColor(buff, v1); // simply calls more buff.putStuff
     buff.putFloat(tex[0]); // texture coords using atlas
     buff.putFloat(tex[1]);
      buff.put((byte) 0); // normals
     buff.put((byte) 0);
      buff.put((byte) 127);
      buff.putFloat(pos.x - .5f); //repeat
     buff.putFloat(pos.y + .5f);
      buff.putFloat(pos.z - .5f);
      addColor(buff, v2);
      buff.putFloat(tex[2]);
      buff.putFloat(tex[3]);
      buff.put((byte) 0);
      buff.put((byte) 0);
      buff.put((byte) 127);
      buff.putFloat(pos.x - .5f);
      buff.putFloat(pos.y + .5f);
      buff.putFloat(pos.z + .5f);
      addColor(buff, v3);
      buff.putFloat(tex[4]);
      buff.putFloat(tex[5]);
      buff.put((byte) 0);
      buff.put((byte) 0);
      buff.put((byte) 127);
      buff.putFloat(pos.x + .5f);
      buff.putFloat(pos.y + .5f);
      buff.putFloat(pos.z + .5f);
      addColor(buff, v4);
      buff.putFloat(tex[6]);
      buff.putFloat(tex[7]);
      buff.put((byte) 0);
      buff.put((byte) 0);
      buff.put((byte) 127);
   }

Rendering a single face of a cube. Same as a quad but with a z.

Hope this helps some. If you do not understand an opengl call, really just google, "opengl name of call". Like, "opengl vertex arrays" "opengl vbo" "opengl occlusion culling" "opengl etc" It cannot be useless as this is how I learned and I cannot be that bad since everything renders properly at except able speeds. Though, I really need to learn more on shaders and modern pipeline.


Offline Doubstract
« Reply #39 - Posted 2013-12-29 12:27:32 »

I found why that texture not worked! i just forgot to do this:
1  
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Pages: 1 [2]
  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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!