Although you can learn a lot from using Google, I find that some of the jargon you come across isn’t explained properly, which leads to having to search up a lot more terms. Also, most of the code you find is C or C++ relate which can be a problem if you don’t know those languages.
Going back to your question on glDrawArrays:
Here you’re telling it to start rendering from position 4 in the buffer object. However, you’re still telling it to draw 8 vertices.
glDrawArrays(GL_TRIANGLE_STRIP,4,8);
Here however you are telling it to start rendering from position 4 in the buffer and you are telling it to draw 4 vertices.
glDrawArrays(GL_TRIANGLE_STRIP,4,4);
So yes, that’s the correct way to do it.
Textures:
Since I can’t be bothered writing out all the code for creating a texture, let me point you to this great tutorial on textures for LWJGL. It teaches you how to decode a PNG into a byte buffer and how to create a texture. It also teaches you about texture coordinates and texture wrapping. If you don’t want to use a library for decoding a texture, I’ll lead you to this tutorial by SHC which has the code for decoding a PNG image with the Java API.
If you want to add textures it’s probably best to use an interleaved buffer object. An interleaved buffer object is a buffer object where the data for both the vertices, and in this case the texture co-ordinates, is held in the same buffer. A texture coordinate is defined as a vector in tanget space, much like a vertex is defined as a vector in 3D space. The difference is however that tangent space represents direction and has a coordinate system of [s, t] or [u, v], whereas 3D space represents a position and is defined as [x, y, z]. Texture coordinates should be defined as 0 >= 1.
So first of all lets set up a basic interleaved buffer object which holds vertices and texture coordinates. If you have a co-ordinate system where (0, 0) is the top left you will need to change the texture coordinates, with [0, 0] being the top left.
float[] data = {
-0.5f, -0.5f, 0f, // bottom left
0f, 0f, // bottom left
-0.5f, 0.5f, 0f, // top left
0f, 1f, // top left
0.5f, -0.5f, 0f, // bottom right
1f, 0f, // bottom right
0.5f, 0.5f, 0f, // top right
1f, 1f}; // top right
glEnable(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D); // enable textures
bufferId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBufferData(GL_ARRAY_BUFFER, (FloatBuffer) BufferUtils.createFloatBuffer(20).put(data).flip(), GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0);
If you haven’t yet set up your texture, read the tutorial I linked to above.
Now for the render method.
glClearColor(0f, 0f, 0f, 1f); // set the clear colour as usual
glClear(GL_COLOR_BUFFER_BIT); // clear the colour buffer (screen)
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBindTexture(GL_TEXTURE_2D, textureId);
glVertexPointer(3, GL_FLOAT, 5 << 2, 0); // there is a stride of 5 << 2, i.e every 5 float values there is a new vertex position
glTexCoordPointer(2, GL_FLOAT, 5 << 2, 3 << 2); // every 2 float values is a texture coordinate. there is a stride of 5 << 2 and an offset of 3 << 2, i.e after 3 float values the first texture coordinate begins
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // there are 4 vertices to draw
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Note: glTexCoordPointer has the same parameters as glVertexPointer. If you don’t understand something just ask.