Friday, December 12, 2008

NeHe Tutorials, Lesson 4 Ported to iPhone

NeHe Lesson 4 was very easy to port. The newly introduced functionality all pretty much works as is.

Here is the link for the ported Lesson 4 project.

The only noteworthy difference (other than items mentioned in the previous blog postings) is the fact that I declared rtri and rquad as static variables within the drawView method rather than declare global variables. This was just to keep all the relevant code for the project together, and doesn't have any impact on the program's functinoality.

One change I might recommend would be to tie the increases to rtri and rquad to the amount of time elapsed. It seems to run pretty smoothly, but if you wanted it perfectly smooth, that would be the way to accomplish it. To do this, replace this code:
 rtri+=0.2f;      // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
with something like this code, which rotates at a steady 20° per second for the triangle, and 15° per second for the square.
 static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rtri+=20.0 * timeSinceLastDraw;
rquad-=15.0 * timeSinceLastDraw;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
Another thing you can do - it won't change the functionality, but will help during debugging - is to keep the rotation within a 360° scope, which can be done simply by adding a couple of lines of code:
 static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rtri+=20.0 * timeSinceLastDraw;
if (rtri > 360.0)
rtri -= 360.0;

rquad-=15.0 * timeSinceLastDraw;
if (rquad < 0.0)
rquad += 360.0;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];




6 comments:

Alex Cone said...

Any idea why all these examples might result in a blank black screen? Tried simulator and 3g iPhone, 21. and 2.2 os for build. Runs, no errors in console - just no display... :-(

Jeff LaMarche said...

Honestly no... they run fine for me in both the simulator and on the device (older, 1G iPhone). Do you get the same result regardless of which SDK version you use? Try using 2.1 or 2.0 and see if you get a different result, and let me know what that result is :)

Jeff LaMarche said...

actually, looks like I had a bad link in the projects - try re-downloading the template and projects now.

NCATDESIGNER said...

I tried you example with a custom figure ( a rectangle ) but I only get the camera rotating, not the rectangle

glLoadIdentity();
//Clear the screen to this color
glClearColor(1.0, 1.0, 1.0, 1.0);
//Clear Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Enable the ability to render arrays of vertex
glEnableClientState(GL_VERTEX_ARRAY);



static GLfloat rtri;

Triangle3D triangle[2];
triangle[0].v1 = Vertex3DMake( 0.0, 1.0, -3.0 );
triangle[0].v2 = Vertex3DMake( 1.0, 0.0, -3.0 );
triangle[0].v3 = Vertex3DMake(-1.0, 0.0, -3.0 );
triangle[1].v1 = Vertex3DMake( 0.0, -1.0, -3.0 );
triangle[1].v2 = Vertex3DMake( 1.0, 0.0, -3.0 );
triangle[1].v3 = Vertex3DMake(-1.0, 0.0, -3.0 );

glLoadIdentity();
glRotatef(rtri,0.0f,1.0f,0.0f);
rtri += 1.0;


//glColor4f(color.red, color.green, color.blue, color.alpha);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 18);


//Finally disable the vertex array rendering
glDisableClientState(GL_VERTEX_ARRAY);

Iain said...

This is quite frustrating. Using iPhone Simulator 3.1.3 in Debug mode - just see a blank white screen until I hit the "home" button.

Then I see the white screen change to the triangle and square as they fade away while the application shuts down.

Any ideas on what might be going wrong here?

I'll see if I can reinstall the SDK to include earlier versions prior to 3.0...

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels