Graphics/3D/Ortho/source/main.cpp

/****************************************
 *      NDS NeHe Lesson 06              *
 *      Author: Dovoto                  *
 ****************************************/

// include your ndslib
#include <nds.h>
#include <malloc.h>
//needed to load pcx files
#include <nds/arm9/image.h>


#include "drunkenlogo_pcx.h"

int DrawGLScene();

float   xrot;               // X Rotation ( NEW )
float   yrot;               // Y Rotation ( NEW )
float   zrot;               // Z Rotation ( NEW )

int texture[1];         // Storage For One Texture ( NEW )

int LoadGLTextures()                                    // Load PCX files And Convert To Textures
{
    sImage pcx;                

    //load our texture
    loadPCX((u8*)drunkenlogo_pcx, &pcx);
    
    image8to16(&pcx);

    glGenTextures(1, &texture[0]);
    glBindTexture(0, texture[0]);
    glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.image.data8);

    imageDestroy(&pcx);

    return TRUE;
}

int main() {    
    
    // Setup the Main screen for 3D 
    videoSetMode(MODE_0_3D);
    vramSetBankA(VRAM_A_TEXTURE); // reserve some memory for textures

    // initialize gl
    glInit();
    
    //enable textures
    glEnable(GL_TEXTURE_2D);
    
    // enable antialiasing
    glEnable(GL_ANTIALIAS);
    
    // setup the rear plane
    glClearColor(0,0,0,31); // BG must be opaque for AA to work
    glClearPolyID(63); // BG must have a unique polygon ID for AA to work
    glClearDepth(0x7FFF);
    
    glLight(0, RGB15(31,31,31) , 0,               floattov10(-1.0),      0);
    glLight(1, RGB15(31,31,31) , 0,               0,    floattov10(-1.0));
    glLight(2, RGB15(31,31,31) , 0,               0,    floattov10(1.0));
    
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    
    glMatrixMode(GL_MODELVIEW);
    
    //need to set up some material properties since DS does not have them set by default
    glMaterialf(GL_AMBIENT, RGB15(16,16,16));
    glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
    glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8));
    glMaterialf(GL_EMISSION, RGB15(16,16,16));
    
    //ds uses a table for shinyness..this generates a half-ass one
    glMaterialShinyness();

    // Set our viewport to be the same size as the screen
    glViewport(0,0,255,191);
    
    LoadGLTextures();
    
    // set the vertex color to white
    glColor3f(1,1,1);

    while (1) 
    {
        scanKeys();
        
        //reset the projection matrix
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        // set the projection matrix as either ortho or perspective
        if(!(keysHeld() & KEY_R)) {
            gluPerspective(70, 256.0 / 192.0, 0.1, 100);
        }
        else
            glOrtho(-3, 3,-2, 2, 0.1, 100);
        
        // Set the current matrix to be the model matrix
        glMatrixMode(GL_MODELVIEW);
        
        //ds specific, several attributes can be set here   
        if((keysHeld() & KEY_L))
            glPolyFmt(POLY_ALPHA(0) | POLY_CULL_NONE  | POLY_FORMAT_LIGHT0| POLY_FORMAT_LIGHT1| POLY_FORMAT_LIGHT2);
        else
            glPolyFmt(POLY_ALPHA(31)  | POLY_CULL_NONE  | POLY_FORMAT_LIGHT0| POLY_FORMAT_LIGHT1| POLY_FORMAT_LIGHT2);
        
        
        //Push our original Matrix onto the stack (save state)
        glPushMatrix(); 

        DrawGLScene();
        
        // Pop our Matrix from the stack (restore state)
        glPopMatrix(1);

        // flush to screen  
        glFlush(0);
    }
    
    return 0;
}

int DrawGLScene()                                           // Here's Where We Do All The Drawing
{
    glTranslatef(0.0f,0.0f,-5.0f);

    glRotatef(xrot,1.0f,0.0f,0.0f);
    glRotatef(yrot,0.0f,1.0f,0.0f);
    glRotatef(zrot,0.0f,0.0f,1.0f);

    glBindTexture(GL_TEXTURE_2D, texture[0]);

    glBegin(GL_QUADS);
        // Front Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
        // Back Face
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
        // Top Face
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        // Bottom Face
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
        // Right face
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
        // Left Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    glEnd();

    xrot+=0.3f;
    yrot+=0.2f;
    zrot+=0.4f;
    return TRUE;            
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Generated on Sat Oct 2 12:55:11 2010 for libnds by  doxygen 1.6.3