Hello! I want to make a 3d game, or at least start it, using OpenGL. I have absolutely no experience in 3d at all, i did however, make some simple games in 2d, so the game mechanics is not new to me.

Basically the knowledge im lacking is how the 3d graphics work. For example i had problem understanding what this piece of code(in Java) means:

Code:

   GL11.glMatrixMode(GL11.GL_PROJECTION);
   GL11.glLoadIdentity();
   GL11.glOrtho(0, 800, 0, 600, 1, -1);
   GL11.glMatrixMode(GL11.GL_MODELVIEW);

and i got this link for an answer:http://www.songho.ca/opengl/gl_transform.html

This link explains the basic concepts of 3d programming, but my problem is that i cant understand it because i dont know basics beneath these basics, like for example what "eye coordinates" or "clip coordinates" mean.

By searching in google im either only finding very specific tutorials and articles on how to actually program stuff, or advanced articles like the one above that i dont understand.

Im not really asking you to explain it to me, because i know its a ton of information, but maybe you know some links to sites where i could get introduced to 3d programming without having any background?
http://www.glprogramming.com/red/chapter01.html
A *lot* of understanding OpenGL is predicated on being able to represent transformations in terms of rotation/transformation matrices and homogeneous coordinates. If you don't really understand those, you're going to have a rough time of it.
These 3 links are exaclty what i need. I dont have much time to read them today, but i will read it eventually. Thank you a lot for help.
Quote:
http://www.glprogramming.com/red/chapter01.html
Its in c++, and even though the diffrences are small, there are some things that i cant figure out how to do in java. For example:

Code:
static GLubyte allIndices = {4, 5, 6, 7, 1, 2, 6, 5,
 0, 1, 5, 4, 0, 3, 2, 1,
 0, 4, 7, 3, 2, 3, 7, 6};
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, allIndices);
//No such type as GLubyte, not even in  the GL11 libary. Also, AllIndicies has to either be long, byte or int, but i cant create an array without puting [] after the type

However i would really like to continue reading this one, because its the best tutorial i've ever seen. Does anybody know a solution?
Quote:
A *lot* of understanding OpenGL is predicated on being able to represent transformations in terms of rotation/transformation matrices and homogeneous coordinates. If you don't really understand those, you're going to have a rough time of it.


I dont understand a single word. I just searched through it for one hour, reading about the concepts i dont understand that lead to concepts i dont understand and so on for ever. But this wasn't really what i was looking for. I was looking for something more OpenGL-user-friendly, not the whole implementation behing the libary. For example i don't know what the glTranslate function does at all, and "translate" doesn't say much.
I really recommend you learn about 3Dspace first before using OpenGL. Since you are using OpenGL 1.1, the functions are provided to you for manipulating points, unlike 4.x where you must build and use your own matrices.

Again, you need to know about math and 3D space before doing 3D math/manipulation. Don't use OpenGL until you are able to understand these concepts as you will have a terrible time.
elfprince13 wrote:
A *lot* of understanding OpenGL is predicated on being able to represent transformations in terms of rotation/transformation matrices and homogeneous coordinates. If you don't really understand those, you're going to have a rough time of it.


I usually use euler rotations for rotating the camera and use glGetMatrix to get the transformation matrix movement.


Its hard to find a good explanation of how transformation matrices are structured, so I'll just post my understanding of them.

-----

Transformation matrices are 4x4 in size and contain the translation and rotation of a location in 3d space. The matrix format is as follows:


Code:

[ rx  ry  rz  tx ]
[ ux  uy  uz  ty ]
[ fx  fy  fz  tz ]
[ 0  0  0  1]

{rx, ry, rz} is a normalized vector pointing to the right
{ux, uy, uz} is a normalized vector pointing up
{fx, fy, fz} is a normalized vector pointing forward
{tx, ty, tz} is the location in 3d space


So moving is really simple. You just add the forward, right, or up vector to your current location.

For example, to rotate 90 degrees on the x axis and move forward 5 units you could do


Code:
float matrix[4][4];
glLoadIdentity()   // reset the transformation
glRotatef(90, 1, 0, 0)  // rotate 90 degrees
glGetMatrix(GL_MODELVIEW_MATRIX, matrix);  // get the transformation matrix
glTranslatef(5 * matrix[3][0], 5 * matrix[3][1], 5 * matrix[3][2]);



Another thing, Java isn't really the best thing to use when learning OpenGL. Due to the structure of the language, many of the function in OpenGL are slightly different.
But where are the axes pointing? Because whenever i change something everythings diffrent. For example i want a quad to be in the middle of the screen and be able to rotate around its upper side. This is what i got:

Code:

         GL11.glRotatef(rotation, rotatespeed, 0f, 0f);
      
      GL11.glTranslatef(x, y, z);////this was here from the beginning, dont know the purpose, doesnt change anything when removed.
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glVertex3f(x - 2, y - 2,z-20);
         GL11.glVertex3f(x + 2, y - 2,z-20);
         GL11.glVertex3f(x + 2, y + 2,z-20);
         GL11.glVertex3f(x - 2, y + 2,z-20);
      GL11.glEnd();
      GL11.glTranslatef(-x, -y, -z); //same as the upper one

but this was just some playing around. I have no idea what im doing.

EDIT: Okay i see it a bit clearer now, but still cant figure out how to do what i want.
jammasterz wrote:
But where are the axes pointing? Because whenever i change something everythings diffrent. For example i want a quad to be in the middle of the screen and be able to rotate around its upper side. This is what i got:


The X axis is left-to-right, y axis is up-to-down, and z axis is forward-to-back. Matrices don't stores rotations though, they store directions. eg. what you'd add to the current location to go right, up, or forward.

Where the quad appears on the screen depends on where the current location and rotation of the modelview matrix * the projection matrix (though you should really only mess with the modelview matrix).
SoulofDeity wrote:
Matrices don't stores rotations though, they store directions. eg. what you'd add to the current location to go right, up, or forward.

Matrices represent transformations - this could be a translation ("what you'd add to the current location to go right, up or forward") but they can also represent rotation, reflection, scaling, shearing and any combination of the above.

jammasterz wrote:
But where are the axes pointing? Because whenever i change something everythings diffrent. For example i want a quad to be in the middle of the screen and be able to rotate around its upper side. This is what i got: [snip]

You'll need to paste more complete code. Functions like glRotatef and glTranslatef multiply the current matrix with another one (a rotation and translation matrix in this instance) and your code doesn't provide any hints as to what the "current" matrix is. You'll need to set up an appropriate projection matrix (a bit like your camera position) and be providing a rotating modelview matrix to place your quad in the desired position and orientation in the world.

Code:
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;

public class Snake {
   final int width = 800;
   final int height = 600;
   /** position of quad */
   float x = 0, y = 0, z = 0;
   /** angle of quad rotation */
   static float rotation = 0;
   /** time at last frame */
   long lastFrame;
   /** frames per second */
   int fps;
   /** last fps time */
   long lastFPS;
   float rotatespeed = 0;
   boolean taketurn = false;
   float previousrotation;

   public void start() {
      try {
         Display.setDisplayMode(new DisplayMode(width, height));
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
      }

      initGL(); // init OpenGL
      getDelta(); // call once before loop to initialise lastFrame
      lastFPS = getTime(); // call before loop to initialise fps timer

      while (!Display.isCloseRequested()) {
         int delta = getDelta();
         
         update(delta);
         renderGL();

         Display.update();
         Display.sync(60); // cap fps to 60fps
      }

      Display.destroy();
   }
   
   public void update(int delta) {
      // rotate quad
      if (taketurn)
         if (rotation < previousrotation + 90)
            rotation += 0.1f * delta;
         else
            taketurn = false;
      if (rotation == 360)
         rotation -= 360;
      
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
         if (!taketurn){
            previousrotation = rotation;
            rotatespeed = 1f;
            taketurn = true;
         }
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
         if (!taketurn){
            previousrotation = rotation;
            rotatespeed = -1f;
            taketurn = true;
         }
      }
      updateFPS(); // update FPS Counter
   }
   
   public int getDelta() {
       long time = getTime();
       int delta = (int) (time - lastFrame);
       lastFrame = time;
   
       return delta;
   }
   public long getTime() {
       return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }
   public void updateFPS() {
      if (getTime() - lastFPS > 1000) {
         Display.setTitle("FPS: " + fps);
         fps = 0;
         lastFPS += 1000;
      }
      fps++;
   }
   public void initGL() {
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GLU.gluPerspective(45.0f, (float)width / (float)height, 0.1f, 200.0f);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glLoadIdentity();
   }

   public void renderGL() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
      GL11.glClearColor(0f, 0f, 0f, 0f);
      GL11.glLoadIdentity();
      //set up the camera
      GL11.glTranslatef(x, -5, -20);
      GL11.glRotatef(45, 1f, 0f, 0f);
      //draw the grid
      GL11.glTranslatef(x, y, -10);
      GL11.glRotatef(rotation, 0f, rotatespeed, 0f);
      
      //GL11.glTranslatef(-x, -y, -z); dafuq is this
      
      GL11.glColor3f(1f, 0.5f, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glVertex3f(-2,0,2);
         GL11.glVertex3f(-2,0,-2);
         GL11.glVertex3f(+2,0,-2);
         GL11.glVertex3f(+2,0,2);
      GL11.glEnd();
   }
   
   public static void main(String[] argv) {
      Snake game = new Snake();
      game.start();
   }
}
benryves wrote:
SoulofDeity wrote:
Matrices don't stores rotations though, they store directions. eg. what you'd add to the current location to go right, up, or forward.

Matrices represent transformations - this could be a translation ("what you'd add to the current location to go right, up or forward") but they can also represent rotation, reflection, scaling, shearing and any combination of the above.


I know, I'm just trying to keep the explanation as simple as possible.
Most sites just post wolfram pics that look like they're written in some alien language
Okay, i have figured out how to solve my problems, as well as the function of glTranslate that i will now put in human words - it moves stuff around. Sorry i didnt listen to your explanation earlier benryves.

But i have a next question. As you can see in my previous code there is this line that sets the perspective:

Code:
      GLU.gluPerspective(45.0f, (float)width / (float)height, 0.1f, 200.0f);

So from what i understand my point of view should now be from 45 degrees. But when i try to draw a quad on the Z-axis, like this:

Code:
      GL11.glColor3f(1f, 0.5f, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glVertex3f(-2,0,2);
         GL11.glVertex3f(-2,0,-2);//marked
         GL11.glVertex3f(+2,0,-2);//marked
         GL11.glVertex3f(+2,0,2);
      GL11.glEnd();

suprisingly i don't see it. I can only see it when i replace the y coordinates in marked lines by something other than 0, which means i still have a perspective of 0 degrees ( basically as if my eyes were on the Z-axis).

What am i missing?
jammasterz wrote:
Okay, i have figured out how to solve my problems, as well as the function of glTranslate that i will now put in human words - it moves stuff around. Sorry i didnt listen to your explanation earlier benryves.

But i have a next question. As you can see in my previous code there is this line that sets the perspective:

Code:
      GLU.gluPerspective(45.0f, (float)width / (float)height, 0.1f, 200.0f);

So from what i understand my point of view should now be from 45 degrees. But when i try to draw a quad on the Z-axis, like this:

Code:
      GL11.glColor3f(1f, 0.5f, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glVertex3f(-2,0,2);
         GL11.glVertex3f(-2,0,-2);//marked
         GL11.glVertex3f(+2,0,-2);//marked
         GL11.glVertex3f(+2,0,2);
      GL11.glEnd();

suprisingly i don't see it. I can only see it when i replace the y coordinates in marked lines by something other than 0, which means i still have a perspective of 0 degrees ( basically as if my eyes were on the Z-axis).

What am i missing?


gluPerspective doesn't set the camera angle, it sets the projection matrix. The 45 is the field of view angle, or in other words your range of sight.
Eg. a field of view that's 0 would be like a horse with blinders;

| |

Whereas a field of view that's 45 expands outwards;

\ /

The second field is the aspect ratio, which is viewport width / height. The third field is the near clipping plane, and the fourth field is the far clipping plane.



Edit:
The reason you're not seeing it is because you have your near plane set to 0.1 and the z-coordinates of your quad is at -2, behind the near plane. change that to 6 and you'll see it.
Still cant see it.

EDIT: I solved it by setting up some random camera position and simply rotating it.

EDIT2 : I have updated the code few post back with the new code. My problem with it this time isn't actually as stiupid as my previous problems.

When i start the program the quad is perfectly aligned. But when i rotate it a few times it gets off. After few 360 rotations its off by more than 90 degrees! Also when i change the rotation direction from one to the other the quad flickers for some reason. I assume that my problems are because the answers of trig functions is getting rounded each time i perform a rotation, but how can i fix it?
jammasterz wrote:
Still cant see it.

EDIT: I solved it by setting up some random camera position and simply rotating it.

EDIT2 : I have updated the code few post back with the new code. My problem with it this time isn't actually as stiupid as my previous problems.

When i start the program the quad is perfectly aligned. But when i rotate it a few times it gets off. After few 360 rotations its off by more than 90 degrees! Also when i change the rotation direction from one to the other the quad flickers for some reason. I assume that my problems are because the answers of trig functions is getting rounded each time i perform a rotation, but how can i fix it?



You need to call glLoadIdentity before translating or rotating to reset the transformation. Remember, the code is being called every frame.
A few months ago I began toying around with OpenGL 1.1, and a few weeks ago, my friend started working with it too. Looking at his struggles, here's a small list of things that caught him up.

1) There is a viewing volume represented by 8 points, called the viewing frustum. Any points that lie within this volume are flattened to the screen and then used for drawing. Management of the 8 points that bound the volume are usually handled via gluPerspective, glOrtho, or glFrustum.

2) When you are using gluPerspective and have depth checking enabled, the granularity of the depth buffer is logarithmically scaled. This is so that the level of detail is front-loaded, because you're more likely to see polygons clip when you're closer to them. This means that more information is focused to the area near the camera. If you get too close to the camera, you'll have far too little detail outside of the first few units in front of the camera. Basically, don't set gluPerspective's znear any lower than 0.5.

3) For lighting, OpenGL doesn't automatically compute which direction each polygon is facing, so you have to do that yourself. (You can write a shader to compute face normals, but that's very hairy for a beginner like me)
Flat shading's pretty easy, and smooth shading is just a little more involved.

4) OpenGL provides a very nice mechanism for transforming batches of vertices via glTranslate, glRotate, etc. Use these instead of managing vertex coordinates yourself. It's faster and makes cleaner, more reusable code.

5) OpenGL provides scoping for its transformation matrices via glPushMatrix() and glPopMatrix(). These are very useful for when you're using many transformations.

6) Most importantly, learn about matrix and vector maths. Going in to OpenGL with a very shaky understanding of both hasn't served me well.
Okay i have 2 small problems. I have drawn the 3d grid, implemented movement and now im trying to draw the snake. I have drawn a pyramid before, and thats why im asking, i dont see any mistakes.

Basically my snake is a tall pyramid thats rotated 90 degrees so that it looks like some kind of a bullet. But the upper side of the snake doesnt seem to be there as does the quad (base of the pyramid). Here are some screenshots.
[img]http://imageshack.us/photo/my-images/29/pyramidm.jpg/[/img]
As you can see on picture 1, the blue quad is not drawn at all, on picture 2 however you can see a yellow quad there that wasn't visible from previous angle. Picture 3 is from the quads angle.

The code is:

Code:
      GL11.glPushMatrix();
      GL11.glBegin(GL11.GL_QUADS);
         GL11.glColor3f(0f, 0f, 1.0f); //blue
         GL11.glVertex3f(1, 0.5f, 0.5f);
         GL11.glVertex3f(1, 2.5f, 0.5f);
         GL11.glVertex3f(3, 2.5f, 0.5f);
         GL11.glVertex3f(3, 0.5f, 0.5f);
      GL11.glEnd();
   
      GL11.glBegin(GL11.GL_TRIANGLES);
         GL11.glColor3f(1.0f,1.0f,0f);//yellow
         GL11.glVertex3f(1, 0.5f, 0.5f);
         GL11.glVertex3f(3, 0.5f, 0.5f);
         GL11.glVertex3f(2, 1.5f, 3.5f);
         
         GL11.glVertex3f(1, 2.5f, 0.5f);
         GL11.glVertex3f(3, 2.5f, 0.5f);
         GL11.glVertex3f(2, 1.5f, 3.5f);

         GL11.glColor3f(1.0f, 0f, 0f);//red
         GL11.glVertex3f(1, 0.5f, 0.5f);
         GL11.glVertex3f(1, 2.5f, 0.5f);
         GL11.glVertex3f(2, 1.5f, 3.5f);
         
         GL11.glVertex3f(3, 2.5f, 0.5f);
         GL11.glVertex3f(3, 0.5f, 0.5f);
         GL11.glVertex3f(2, 1.5f, 3.5f);      
      GL11.glEnd();

      GL11.glRotatef(180, 0, 1, 0);
      GL11.glRotatef(snakeRotation, 0f, 0f, 1f);
      GL11.glTranslatef(squareX * 4 + 2, 0, - squareZ * 4 - 2);
      GL11.glPopMatrix();


Also the rotations that are at the end of the code dont work, which is odd.[/url]
Rotate after you translate. glRotatef and glTranslatef do their jobs relative to the current transformation matrix.
Please know that glBegin and glEnd work on the state of the current matrix at the time called. Right now, you push the matrix, draw something, modify the matrix, then pop. Basically, you just draw since the transformations are done after you draw, and pop matrix undoes your changes to it.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement