Reputation: 302
Okay, so I was attempting to draw a cube by drawing the 6 faces.
I define each of the faces of the cube itself by giving it two vectors. As follows:
However, now I am having trouble of how to draw these faces in openGL (just a small note, I am using LWJGL, which is the java library build upon openGL).
I have attempted to draw the said faces, but I believe I have a slight mistake that I can't seem to get right. Currently, I have attempted to draw the faces as follows:
public void render() {
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(v2.x, v1.y, v1.z);
GL11.glVertex3f(v1.x, v1.y, v1.z);
GL11.glVertex3f(v1.x, v2.y, v2.z);
GL11.glVertex3f(v2.x, v2.y, v2.z);
GL11.glEnd();
}
But it seems here, that only the top and bottom faces get drawn correctly, while the side (left/right) faces aren't even visible.
How can I draw these properly?
Upvotes: 1
Views: 6579
Reputation: 4411
2 vectors define infinity of parallel faces. You need an additional point X
to select one of those unless your X = (0,0,0)
and that's why it doesn't show in your code.
If V1
and V2
define the length and direction of two sides of a face that start from a reference point X
, you can draw you quad as
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(X.x, X.y, X.z);
GL11.glVertex3f(X.x+v1.x, X.y+v1.y, X.z+v1.z);
GL11.glVertex3f(X.x+v1.x+v2.x, X.y+v1.y+v2.y, X.z+v1.z+v2.z);
GL11.glVertex3f(X.x+v2.x, X.y+v2.y, X.z+v2.z);
GL11.glEnd();
Upvotes: 2