Sunday, November 29, 2009

First Steps into 3D

Making a rotating 3d mesh proved much more difficult than I had expected. While I was able to make and display a shape, it took me a long time to figure out how to enable depth sorting so that polygons would be drawn in the correct order. Then, I needed to enable perspective mode in order to give the illusion of moving the camera. Just for fun, I looked up how to enable anti-aliasing for smoother edges. Enjoy!


video

I made the pyramids in Blender 3D and looked at the local point coordinates in order to bring them into OpenGL.:

//Draw a 3-sided pyramid

glBegin(GL_TRIANGLES);

//DAC (points must be listed in counterclockwise order)

glColor3f(1.0f, 0.0f,0.0f); //RED

glVertex3f(1.0f, -1.0f,-0.798f); //D

glColor3f(0.0f, 1.0f,0.0f); //GREEN

glVertex3f(0.0f, -0.333f,0.798f); //A

glColor3f(0.0f, 0.0f,1.0f); //BLUE

glVertex3f(-1.0f, -1.0f,-0.798f); //C

//DCB

glColor3f(1.0f, 0.0f,0.0f); //RED

glVertex3f(1.0f, -1.0f,-0.798f); //D

glColor3f(0.0f, 0.0f,1.0f); //BLUE

glVertex3f(-1.0f, -1.0f,-0.798f); //C

glColor3f(1.0f, 0.0f,1.0f); //Violet

glVertex3f(0.0f,1.0f,-0.798f); //B

//CAB

glColor3f(0.0f, 0.0f,1.0f); //BLUE

glVertex3f(-1.0f, -1.0f,-0.798f); //C

glColor3f(0.0f, 1.0f,0.0f); //GREEN

glVertex3f(0.0f, -0.333f,0.798f); //A

glColor3f(1.0f, 0.0f,1.0f); //Violet

glVertex3f(0.0f,1.0f,-0.798f); //B

//BAD

glColor3f(1.0f, 0.0f,1.0f); //Violet

glVertex3f(0.0f,1.0f,-0.798f); //B

glColor3f(0.0f, 1.0f,0.0f); //GREEN

glVertex3f(0.0f, -0.333f,0.798f); //A

glColor3f(1.0f, 0.0f,0.0f); //RED

glVertex3f(1.0f, -1.0f,-0.798f); //D

glEnd();


Next week, I plan to look through the code I hacked together in order to truly understand how it works. I will post an explanation so that anyone getting started with OpenGL and Cocoa will not have the same problems I did.

Beginning OpenGL

It has been a long time since my last update, but I have finally found the time to start learning OpenGL. There seems to be very few online tutorials directed at Objective-C specifically, so I have had to cobble together information from many different sources. However, I have now managed to create a very simple application that draws an OpenGL scene and updates it 60 times a second. Behold the rotating triangle:

video

Using this framework, I can now use general OpenGL tutorials. Soon, I hope to post experiments with more complicated meshes.