Problem applying the textures with VBO (LWJGL)

Hello,

I my game has a tiled based top to down view.
I’ve been using immediate rendering which I think might be slower…
Now that I’m woried about performance and I want to make everything fast and bug free before I move on, I want to make it better… My thoughts using VBO…

I’m not sure if this is the best Idea or the right one to use but as far as I know it is… if not tell me please.

My code that doesn’t work:


public class QuadRender {

	protected int vao, vbo, vio, vto;

	protected float[] vertices;
	protected byte[] indices = new byte[] {
			0, 1, 2, //
			2, 3, 0 //
	};
	protected byte[] texCoords = new byte[] {
			0, 0, //
			1, 0, //
			1, 1, //
			1, 1, //
			0, 1, //
			0, 0 //
	};

	public QuadRender(int size) {
		GeneralSettings.quads.add(this);
		vertices = new float[] {
				0.0f, 0.0f, 0.0f, //
				size, 0.0f, 0.0f, //
				size, size, 0.0f, //
				0.0f, size, 0.0f //
		};
		compile();
	}

	protected void compile() {
		vao = glGenVertexArrays();
		glBindVertexArray(vao);
		{
			vbo = glGenBuffers();
			glBindBuffer(GL_ARRAY_BUFFER, vbo);
			{
				glBufferData(GL_ARRAY_BUFFER, Buffer.createFloatBuffer(vertices), GL_STATIC_DRAW);
				glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
			}
			glBindBuffer(GL_ARRAY_BUFFER, 0);

			vio = glGenBuffers();
			glBindBuffer(GL_ARRAY_BUFFER, vio);
			{
				glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(indices), GL_STATIC_DRAW);
			}
			glBindBuffer(GL_ARRAY_BUFFER, 0);

			glDeleteBuffers(vto);

			vto = glGenBuffers();
			glBindBuffer(GL_ARRAY_BUFFER, vto);
			{
				glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(texCoords), GL_STATIC_DRAW);
				glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, false, 0, 1);
			}
			glBindBuffer(GL_ARRAY_BUFFER, 0);
		}
		glBindVertexArray(0);
	}

	public void render(int x, int y, int texture, Shader shade) {
		glTranslatef(x, y, 0);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture);
		glUniform1i(glGetUniformLocation(shade.getShader(), "texture"), 0);

		glBindVertexArray(vao);
		glEnableVertexAttribArray(0);
		glEnableVertexAttribArray(1);
		{
			glBindBuffer(GL_ARRAY_BUFFER, vio);
			glVertexPointer(2, GL_FLOAT, 0, 0);

			glBindBuffer(GL_ARRAY_BUFFER, vto);
			glTexCoordPointer(2, GL_FLOAT, 0, 0);

			glDrawArrays(GL_QUADS, 0, 4);
		}
		glEnableVertexAttribArray(1);
		glEnableVertexAttribArray(0);
		glBindVertexArray(0);
		glTranslatef(-x, -y, 0);

		glBindTexture(GL_TEXTURE_2D, 0);
	}

}

My code that does work:


public class QuadRender {

	public QuadRender(int size) {
		GeneralSettings.quads.add(this);
	}

	public void render(int x, int y, int texture, Shader shade) {
		glTranslatef(x, y, 0);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture);
		glUniform1i(glGetUniformLocation(shade.getShader(), "texture"), 0);

		glBegin(GL_QUADS);
		{
			glTexCoord2f(0, 0);
			glVertex2f(0, 0);

			glTexCoord2f(0, 1);
			glVertex2f(0, 64);

			glTexCoord2f(1, 1);
			glVertex2f(64, 64);

			glTexCoord2f(1, 0);
			glVertex2f(64, 0);
		}
		glEnd();
		glBindTexture(GL_TEXTURE_2D, 0);
		glTranslatef(-x, -y, 0);
	}

}

With the code that works it looks like this:

http://www.joaolourenco.net/Img1.png

With the code that doesn’t work it looks like this:

http://www.joaolourenco.net/Img2.png

PS: the wooden bit uses another render engine for now thats why its not affected by the changes.

What the problem on the textures?

Thanks, Joao Lourenco.

Ok there are a lot of things wrong with this code. I’m in a good mood (Happy Holidays!) and decided to re-write it for practice


public class QuadRender {

   protected int vao, vbo, vio, vto;

   protected float[] vertices;
   protected byte[] indices = new byte[] {
         0, 1, 2, //
         2, 3, 0 //
   };

   //Index based rendering 
   protected float[] texCoords = new float[] {
         0, 0, //0 indx
         1, 0, //1 indx
         1, 1, //2 indx
         1, 0, //3 indx
   };

   public QuadRender(int size) {
      GeneralSettings.quads.add(this);
      vertices = new float[] {
            0.0f, 0.0f, 0.0f, //
            size, 0.0f, 0.0f, //
            size, size, 0.0f, //
            0.0f, size, 0.0f //
      };
      compile();
   }

   protected void compile() {
      vao = glGenVertexArrays();
      glBindVertexArray(vao);
      {
         vbo = glGenBuffers();
         glBindBuffer(GL_ARRAY_BUFFER, vbo);
         {
            glBufferData(GL_ARRAY_BUFFER, Buffer.createFloatBuffer(vertices), GL_STATIC_DRAW);
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
         }

         vio = glGenBuffers();
         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vio);   //BIND TO THE ELEMENT ARRAY BUFFER for indices
         {
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, Buffer.createByteBuffer(indices), GL_STATIC_DRAW);
         }

         //glDeleteBuffers(vto); I dont understand

         vto = glGenBuffers();
         glBindBuffer(GL_ARRAY_BUFFER, vto);
         {
            glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(texCoords), GL_STATIC_DRAW);
            glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); //size is 2, float is a better idea, offset is 0
         }
         glEnableVertexAttribArray(0); //Enable attribs in the VAO
         glEnableVertexAttribArray(1);
      }
      glBindVertexArray(0);
      //Moved outside loop
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, texture);
      glUniform1i(glGetUniformLocation(shade.getShader(), "texture"), 0);
   }


   public void render(int x, int y, int texture, Shader shade) {
      //glTranslatef(x, y, 0); This is not immediate mode

      glBindVertexArray(vao);
      {
         /*glBindBuffer(GL_ARRAY_BUFFER, vio);
         glVertexPointer(2, GL_FLOAT, 0, 0);

         glBindBuffer(GL_ARRAY_BUFFER, vto);  You are mixing gl 1.5 and gl2.0 this is gl1.5
         glTexCoordPointer(2, GL_FLOAT, 0, 0);*/

         glDrawElements(GL_TRIANGLES, 6); //indexed drawing use elements, use triangles as you set up your indices for triangles
      }
      glBindVertexArray(0);
      //glTranslatef(-x, -y, 0); still not immediate mode ;)

   }

}

I probably still missed a lot of things or messed up somewhere (I actually editted this on the JGO post so no highlighting :wink: )

Moral of the story, you are obviously copy and pasting a lot of code you don’t understand. Go back and understand whats going on, or do it in baby steps first and add small parts at a time, using Version Control to be able to roll back if you break anything.

edit:
Also your use of protected scares me. I can’t think of any good program design that should have a class like this as protected. Favor composition over weird inheritance.