I'm creating a buffer object helper class which is usable by a user with little or no knowledge of OpenGL. I have a helper class for creating and handling buffers called NIOBuffer. No solution is going to be pretty but I'm wondering which solution would be best. The first problem is how to deal with inheritance as you cannot pass Buffer to e.g glBufferData - I could either:

1. Eliminate inheritance and create five different classes for each data type.

2. Cast to the correct buffer type.

I'd assume the latter is preferred for ease of use purposes but for design purposes they're both ugly. Now lets consider the actual design of that. Here I have one of three choices:

So... have fun answering. Please give some other design alternatives too. Oh and if anyone has any better methods of casting stuff please post them below. This is just what I could think from off the top of my head.

If your library is bloated and over-engineered, and doesn't even provide any high-level features, then it sounds pretty useless. I would steer away from trying to wrap GL concepts exactly 1:1, and instead aim to provide a useful library. If a user needs to provide some obscure and specific parameters to accomplish a particular optimization, they can just call GL directly. See LibGDX's glutils for reference.

If your library is bloated and over-engineered, and doesn't even provide any high-level features, then it sounds pretty useless. I would steer away from trying to wrap GL concepts exactly 1:1, and instead aim to provide a useful library. If a user needs to provide some obscure and specific parameters to accomplish a particular optimization, they can just call GL directly. See LibGDX's glutils for reference.

That's the exact opposite of what I'm trying to do. I'm trying to create a library which is usable by someone without any past experience of OpenGL. I'm not trying to make them have to pass obscure and specific parameters but to simply pass a buffer of any kind.

NIOBuffer holds a java.nio.[datatype]Buffer. A BufferObject is an object which holds data on the GPU. LWJGL has several overloaded methods which pass either a ByteBuffer, ShortBuffer, IntBuffer, FloatBuffer or DoubleBuffer to the GPU. What I'm trying to do is allow the user to use one of the helper methods in NIOBuffer which returns a sub-class of NIOBuffer that holds the buffer data and then the user can use that as the generic type parameter of BufferObject and pass that subclass to the constructor which uses that to obtain the buffer data.

Since LWJGL cannot accept java.nio.Buffer as a parameter (since LongBuffer is not supported) I have to cast that Buffer to the correct buffer type. That however requires the horrible bloated code which I posted above. What I want is to either find a more efficient and easier way of casting the Buffer to the correct buffer type which I don't think is possible or an alternative design for my library which is still usable by a user with no knowledge of how to use OpenGL.

what has this to do with pass by value in your mind?Take a look at the docs:

Quote

asFloatBufferCreates a view of this byte buffer as a float buffer.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.

The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

NIOBuffer holds a java.nio.[datatype]Buffer. A BufferObject is an object which holds data on the GPU. LWJGL has several overloaded methods which pass either a ByteBuffer, ShortBuffer, IntBuffer, FloatBuffer or DoubleBuffer to the GPU. What I'm trying to do is allow the user to use one of the helper methods in NIOBuffer which returns a sub-class of NIOBuffer that holds the buffer data and then the user can use that as the generic type parameter of BufferObject and pass that subclass to the constructor which uses that to obtain the buffer data.

Take a step back, re-read by yourself, and think whether this concept is suitable for OpenGL newbies and simplifies its usage.

According to the docs the result should be 3.0, 3.0, 1, 1 should it not?

Quote

changes to this buffer's content will be visible in the new buffer, and vice versa

@65K: Do you have any other alternative designs which allow the user to create a buffer object with no knowledge of using OpenGL? It seems simple enough to me. Anyone with a good knowledge of the Java language should grasp the idea quite easily if it's well documented.

@65K: Do you have any other alternative designs which allow the user to create a buffer object with no knowledge of using OpenGL? It seems simple enough to me. Anyone with a good knowledge of the Java language should grasp the idea quite easily if it's well documented.

What kind of knowledge do we not need when we use this buffer stuff ?Give us a line of code what you would users like to type to use it.

Everything would be fully documented and I'd include method chaining. Overall my main purpose is to create a library that someone familiar with Java but not familiar with OpenGL could use.

I never used VBO's, and thus don't know how to use them correctly, and the pure LWJGL-Code seems much simpler than your code.You are making the interfacing with OpenGL even harder than before!

You should better make a class that lets you create a 'Mesh', to which you can then add vertices in any way you wan't.The class itself then handles the VBO stuff in the background, so the user will only have to call simple 'addVertex' 'addTriangle' 'addQuad' 'renderMesh' methods.

@Longor1996 Buffer objects aren't just for vertices. That's just one usage of them. They have many uses; their purpose is to store data on the GPU. They can be used for other things like texture coordinates. I do however like your idea of adding helper methods for creating simple shapes. I'll find a way to implement that in the future.

@Troubleshoots: you should drop the idea of writing a library, and reconsider it after at least a year, when you have more experience. You can only build tools for others when you have a solid understanding of the underlying mechanics. My impression is that you're just starting out and still discover new things and concepts every day. My advice to you is to use libraries, not make them. By the time you understand how these libraries work, and more importantly, why they were designed that way, you will have enough information to roll your own - if you still feel like it's the way to go.

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

@Troubleshoots: you should drop the idea of writing a library, and reconsider it after year, when you have more experience. You can only build tools for others when you have a solid understanding of the overlaying mechanics. My impression is that you're just starting out and still discover new things and concepts every day. My advice to you is to use libraries, not make them. By the time you understand how these libraries work, and more importantly, why they were designed that way, you will have enough information to roll your own - if you still feel like it's the way to go.

Writing my own library will help me learn. Design can be changed. I don't see the point of using a library if my goal is to create a library. Using a library is a short-cut. I feel that I'll learn more by going the longer route; after all, I have the time.

When I visit China, I enter the realm of secluded Chinese poets, to learn to express myself with the subtleties of the language. Getting food on my plate and finding shelter is tough as I struggle with verbs, but this particular route will help me in the long run. I also repeatedly told some local English teacher in my best Chinese to get off my lawn.

So long, and thanks for all the fish!

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

Everyone has their own opinion of which affects their views. Their views affect the way that they interpret things which is driven more by assumption than by understanding.

Quote

I don't see the point of using a library if my goal is to create a library. Using a library is a short-cut. I feel that I'll learn more by going the longer route

Saying that not using a library does not relate to disregarding knowledge. A library can be thought of as a mask; it hides required knowledge. By creating my own library I gather that hidden knowledge, thus I learn more.

When writing the book, you always find yourself being held back by your current knowledge. You'll barely make progress, and if any, often in the wrong direction. When reading the book, you find yourself quickly gaining knowledge, allowing you to write a much better book much later.

TLDR: don't write libraries for things you don't grasp yet, if you want to get anywhere.

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

When writing the book, you always find yourself being held back by your current knowledge. You'll barely make progress, and often in the wrong direction. When reading the book, you find yourself quickly gaining knowledge, allowing you to write a much better book much later.

TLDR: don't write libraries for things you don't grasp yet, if you want to get anywhere.

Actually you wouldn't be able to read the book.I'll learn the way I decide to learn.

Its a huge misconception that you learn faster by disregarding knowledge gathered by others.

It doesn't sound like he is disregarding existing libraries. A tremendous amount of understanding and (especially) confidence can be gained by implementing one's own approach to a solution and then comparing it with what is considered to be the accepted solution. For example, if I am writing a function that mostly mimics the behavior of a function in a given library, then through comparison I can see what it was about my approach that works and meanwhile isolate whatever it was that I was trying to do differently. Does another function exist in the library that will cover this extra utility? If so then great. If not then you just uncovered some valuable work to be done.

It is incredibly foolish to be learning a language and expect that you will be able to improve what the general purpose libraries have to offer--they benefit from the input and review of numerous people, each with ample experience, and are there for a reason. This seems to be the argument that most of you are making, but that doesn't seem to be what is going on here: he is creating his own personal set of tools which he will refine as he gains more experience. If they serve some utility then he will keep using them. If not then he will toss them out and either use that which has already been made available by others or fine tune his approach and start again.

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