3 Replies - 1766 Views - Last Post: 22 November 2013 - 09:44 PM Rate Topic: -----

#1 Brian_Entei  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 13

OpenGL Java Game - Help with how to render 3D Objects

Posted 18 November 2013 - 04:26 PM

Hello, I am Brian Entei, and I'm posting here wondering if you guys could help me. I have been following this tutorial on youtube, and have understood it pretty well. The only thing I'm really stuck with is how to actually render and make a 3D object. He, at one point, renders a spinning triangle, and at the end, a floor. The floor is about as far as I got, but I also haven't gotten as far as collision detection. Rest assured I have been mulling over my options for quite some time, and am not looking for a quick answer, rather a step-by-step howto in order to get me to understand how to do this by myself. If you need a copy of the game I have coded, I will gladly upload it to my server as a .zip so that you could help me along the way. I hope I was not too vague, as I don't usually type as well with other people. Thanks for reading, and have a nice day!

-Brian Entei

P.S. I use Eclipse Juno to code and compile my game. I am aware that it would be better performance-wise to code in c++, but I would like to make my first game in Java + OpenGL, as that's what some of my favorite games are coded in.

Is This A Good Question/Topic? 0
  • +

Replies To: OpenGL Java Game - Help with how to render 3D Objects

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: OpenGL Java Game - Help with how to render 3D Objects

Posted 20 November 2013 - 05:45 AM

Like always with OpenGL, I start out by saying I don't know OpenGL.

Your question is pretty broad but I'll try and talk a little bit about how you make and render a 3D object.

So, to start out with, everything in 3D is drawn with triangles. Two triangles can make a rectangle and then the shapes just get more complicated from there until you are drawing single objects with a million polygons. Triangles, of course, are one type of polygon. "In general", the terms triangle, poly, and polygon are pretty much interchangeable in 3D game programing. A quad, or quadrilateral, is another type of polygon and is composed of two triangles, but that may or may not even come up.

So, basically, if you can draw triangles you can draw anything in 3D.

You draw the triangles by defining the vertices of a triangle. The triangles themselves are not defined. Instead the 3 vertices of each triangle are defined. You can kind-of think of a vertex as being the corner of a triangle. A vertex always has a 3D position, but it can also contain a tremendous amount of other information. Color is one piece of information it can contain, although color is not used in this way as much as it once was before texturing was invented. Texture coordinates is one of the big pieces of information likely stored in a vertex. Texture coordinates are used somewhat like pinning a photograph to your triangle. The texture coordinates basically say which part of the picture is pinned and stretched between the 3 vertices of the triangle.

Pretty much every piece of information in the vertex is optional except position. The position is often stored and manipulated as a vector object although it is not really a vector and so that just confuses things although there is a reason it is done this way.

Anyway, you make objects by defining the vertices of the object and putting those vertices into a vertex buffer which is sent to the graphics card and drawn.

You may use indices and store them in an index buffer. Indices are a numerical list of what order to draw the vertices in. This allows you to do things like re-use a vertex from one triangle in another triangle when they have a shared vertex (which is extremely common). Without indices, you would be drawing the vertices in the order they are defined and would probably not have a way to draw them out of order or re-use a vertex.

Anyway, if you hardcode a simple object like a cube this way, you will quickly discover that it is ridiculously difficult to code even the most simple objects imaginable this way. So, basically no one codes anything more complex than a cube this way. Instead, they use modeling programs to make objects.

There are several options for modeling programs including 3D Studio Max and Maya, which are basically the industry standard but cost about the same price as a used car. I am a huge fan of Blender myself. Blender is free and I find it to basically be as good as 3D Studio Max which I have used over the years.

In a modeling program you draw your objects and the vertices are defined for you. Generally, you will want to start with what is called "box modeling". With box modeling, you start with a box and then stretch it into whatever shape you want, such as the shape of a person or the shape of a car. You can make unbelievably complex shapes by stretching a box, if you know what you are doing.

Once you have your model. You export it into a file format that your program can read. I'm currently using the .fbx format. From OpenGL, you need some code to read that particular format into memory. You may be able to use a library for this. Once the model is in memory, it has to be put into a vertex (and probably index) buffer to be drawn.

I don't want to go too deep into matrices right now - since you did not ask about them, but they are very fundamental to this.

A world matrix is loaded up with all the math necessary to place your model into the 3D scene including position, scale, and rotation.

A view matrix simulates the camera and allows you to place and rotate the camera.

A projection matrix (probably perspective projection) contains all the math to translate the 3D world and project it onto your 2D computer monitor.

The three matrices are combined and then every vertex in your model has the combined matrix applied to it one at a time in order to draw every vertex onto the 2D surface of your computer monitor. Then the triangles are drawn, by your graphics card, in between the vertices.

This post has been edited by BBeck: 20 November 2013 - 05:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 Brian_Entei  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-November 13

Re: OpenGL Java Game - Help with how to render 3D Objects

Posted 22 November 2013 - 01:09 PM

Wow, great explanation, thanks very much! I would actually like to learn more about the three matrix types(and everything related) and begin coding my very own open source game. Would you mind tutoring me more on the subject, or do you perhaps know of an online tutorial that I might use? Thanks for your help thus far,

-Brian Entei
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: OpenGL Java Game - Help with how to render 3D Objects

Posted 22 November 2013 - 09:44 PM

My website has some stuff you might find useful. Specifically, the "Enter the Matrix" article will probably answer a lot of your questions. The website is currently focused on XNA, but Matrices shouldn't be much different in OpenGL.

My Matrices in Motion tutorial covers a lot with matrices, but its for XNA.

This thread also covers it a bit.

And if you have any questions, feel free to ask.

This post has been edited by BBeck: 22 November 2013 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1