public void BeginGL()
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0,800,0,600,800,-800);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
// CB CodeBunny: Add this and it works great.
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
public void pyramid()
{
GL11.glTranslatef(400, 300, 0);
GL11.glRotatef(1f,1,0,0);
GL11.glBegin(GL11.GL_TRIANGLES);
//triangle 1
GL11.glColor3f(1.0f,0.0f,0.0f);
GL11.glVertex3f(0,100,0);
GL11.glVertex3f(100, 0, 100);
GL11.glVertex3f(-100, 0, 100);
//triangle 2
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f(0,100,0);
GL11.glVertex3f(-100, 0, 100);
GL11.glVertex3f(-100, 0, -100);
//triangle 3
GL11.glColor3f(1.0f,0.0f,1.0f);
GL11.glVertex3f(0,100,0);
GL11.glVertex3f(-100, 0, -100);
GL11.glVertex3f(100, 0, -100);
//triangle 4
GL11.glColor3f(0.0f,1.0f,1.0f);
GL11.glVertex3f(0,100,0);
GL11.glVertex3f(100, 0, -100);
GL11.glVertex3f(100, 0, 100);
GL11.glEnd();
GL11.glTranslatef(-400, -300, 0);
}
the triangles are not being drawn correctly. it appears that the triangles are drawn over previous triangles drawn.
Is the depth buffer enabled?
Is now and it works awesomely; thank you CodeBunny. Would you happen to know if there would be any reason to separate each face with glBegin(..) and glEnd()? Like could I transform each one differently?
You can render each individual polygon in separate glBegin/glEnd blocks, yes. However, this is almost always a bad idea when dealing with a complete object. Doing that would cost a lot more processing time for the GPU, and I think you be a lot less useful than you are thinking.
Most of the time, people do the opposite of what you mentioned: they "cache" the rendering of the object in some way, making it less flexible to change, but making it *much* faster to render. VBOs/Display Lists/Vertex Arrays are examples of this.
However you can split up a model into pieces and perform transforms on each of the components when rendering. Maybe this might do what you want?
No purpose other than to learn some 3d programming. I was playing with textures today and noticed that if I wanted to load a texture on one face of the pyramid I had to separate the face's vertices with glBegin/glEnd so I was able to enable and disable GL_2D_TEXTURE. I am not sure if it's mentioned in the wiki I most likely overlooked it. I'll be going through the opengl specifications when I complete these tutorials.
Just found out about glColor3fv (been doing it manually) and then read http://lwjgl.org/wiki/index.php?title=About_LWJGL " Thrown out methods designed for efficient C programming that make no sense at all in java, such as glColor3fv."
public float[][] colors = {{0,0,1},
{0,1,0},
{1,0,0},
{0,1,1},
{1,0,1},
{1,1,0},};
GL11.glColor3f(colors[0][0], colors[0][1], colors[0][2]);