Menu
Home
DS
PC
Tutorials
Links
Contact


Affiliates
NeHe 1 - Initialization

 

In this first lesson, you will learn how to do basic initialization of 3d with the OpenGL wrapper and should understand what's happening with the functions.

Lesson 1 Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// include your ndslib
#include <nds.h>
  
int DrawGLScene();
 
int main()
{	
	// Turn on everything
	powerON(POWER_ALL);
 
	// Setup the Main screen for 3D 
	videoSetMode(MODE_0_3D);
 
	// IRQ basic setup (not strickly required but nice
	irqInit();
	irqSet(IRQ_VBLANK, 0);


	// Set our viewport to be the same size as the screen
	glViewPort(0,0,255,191);
 
	// Specify the Clear Color and Depth 
	glClearColor(0,0,0);
 
	glClearDepth(0x7FFF);

 
	while (1) 
	{
		// Reset the screen and setup the view
		glReset();
		gluPerspective(35, 256.0 / 192.0, 0.1, 100);
		glColor3f(1, 1, 1);
		// Set the color..not in nehe source...ds gl default will be black
 
		//ds specific, several attributes can be set here	
		glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
 
		// Set the current matrix to be the model matrix
		glMatrixMode(GL_MODELVIEW);
 
		//Push our original Matrix onto the stack (save state)
		glPushMatrix();	
 
		DrawGLScene();
 
		// Pop our Matrix from the stack (restore state)
		glPopMatrix(1);
 
		//a handy little built in function to wait for a screen refresh
		swiWaitForVBlank();
 
		// flush to screen	
		glFlush();
	}
 
	return 0;
}
 
int DrawGLScene(void)
{

	//this is where the magic happens
	glLoadIdentity();
	return TRUE;
}

Analyze:

Line 12: videoSetMode(MODE_0_3D);
You can select modes 0 to 5. As you can guess from previous DS programming, this will setup the main screen for 3d. Only the main screen can have true 3d - there's a way to do 2-screen 3d, but I won't get into that.

Line 20: glViewPort(0,0,255,191);
Think of this line as a function that sets a rectangle from (0,0) to (255,191). The viewport is where the 3d scene is rendered to.

Line 23: glClearColor(0,0,0);
This sets the background color to black - RGB15 format. Where ever there is not anything drawn, this is the color you will see.

Line 25: glClearDepth(0x7FFF);
The function setes the clip depth buffer's value.

Line 31: glReset();
Clears the FIFO and matrix stacks and sets current matrices to an identity.

Line 32: gluPerspective(35, 256.0 / 192.0, 0.1, 100);
This sets up the camera's FOV - field of view, aspect ratio, near clip, and far clip planes.
The FOV is set in degrees (based on a 360 degree circle).

The aspect ratio is viewport width/viewport height, and it makes the rendering look more "correct" on the screen. A cube will look like a cube with the aspect ratio passed, instead of an elongated box.

The near clip is how close a face/triangle/polygon can get to the camera before it is clipped and no longer rendered.

The far clip is how far a face/triangle/polygon gets from the camera before it is clipped and no longer rendered.



This also sets up the frustum. Here's a basic explanation:

The diagram shows the view at point b looking downwards to point a.
b represents the "camera" location and near clipping plane
a represents the far clipping plane
Everything that is within this "sawed-off" pyramid is what is rendered to the view.

Line 33: glColor3f(1, 1, 1);
glColor3f is for diffusion. You can use it for coloring vertices, which will hint a texture applied to a poly, a future lesson will describe this in more detail, since it's not needed in this lesson.

Line 37: glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
This sets up how the polygons rendered after this line appear on the screen.

POLY_ALPHA(31) does not mean fully opaque, it's just the alpha ID set for the objects. polygons with different POLY_ALPHA IDs will be blended together.

POLY_CULL_NONE means both sides of the polygons will be rendered. From my experience so far, you probably will not notice a difference in speed if you were to change to POLY_CULL_FRONT or POLY_CULL_BACK unless your scene has a high polygon count.

Line 40: glMatrixMode(GL_MODELVIEW);
There are 3 different matrix modes: Projection, Texture, and Model View. This sets up the current matrix to be used; in this case, we use the modelview matrix that was already setup from gluPerspective().
Projection is automatically set in creating the frustum. It's the matrix that the vertices are multipled by to get the screen (2d) pixel coordinates for rasterization.

Texture is used for setting up how textures appear (will be used in future lessons)

Model View is used for rendering your 3d scene.

Line 43: glPushMatrix();
Pushes the current matrix in the mode into the matrix stack. There is a maximum of 16 matrices that can be set in here. You can use the matrix stack to permanently store matrices for uses such as bones (animation). Pushing it into the stack "saves" the state, as dovoto puts it. It stores the current matrix in the stack, to get it back, the glPopMatrix index is 1.
It is good to use glPushMatrix() to be able to start over and render something in front of the camera permanently if you change it to an identity, which is only in affect until glPopMatrix(index) is called.

Note: If you push a matrix, make sure you pop it

Line 64: glLoadIdentity();
Resets the current matrix to an identity. In future lessons, drawing/rendering begins after this identity is called.

Line 48: glPopMatrix(1);
Sets current matrix to the matrix that was last pushed.
NOTE: PC OpenGL does not pop an index, it always pops the last matrix pushed.

Line 54: glFlush();
Cleans up everything. It clears the buffers from memory.


Now, you should have a decent understanding of what's happening with initialization and for the next lesson, you will be ready to start drawing some shapes.