GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


Lighting by KoraToori
1
 
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glfw.h>

void Paint(void);
void doInit(void);

int main( int argc, char **argv )
{
    int    mainWindow;        // Window opened?
    int    running;        // Program running?
    int    width, height;    // Window dimensions
    
    glfwInit();
    
    mainWindow = glfwOpenWindow(640, 480,    // Open window
    8, 8, 8,                                // Red, green, and blue bits for color buffer
    8,                                        // Bits for alpha buffer
    24,                                        // Bits for depth buffer (Z-buffer)
    0,                                        // Bits for stencil buffer
    GLFW_WINDOW);                            // Window (could be GLFW_FULLSCREEN)
    
    if(!mainWindow)                            // If we could not open a window, quit program
    {
        glfwTerminate();
        return 0;
    }
    
    doInit();
    
    do                                                    // Main rendering loop
    {
        Paint();                                        // Call scene drawing
        glfwSwapBuffers();                                // Swap front and back buffers
        running = !glfwGetKey( GLFW_KEY_ESC ) &&
                   glfwGetWindowParam( GLFW_OPENED );    // Still going?
    } while(running);
    
    glfwTerminate();
    return 0;      
}

void doInit(void)
{
    glfwSetWindowTitle("Lighting Test");
    glfwEnable(GLFW_STICKY_KEYS);            //Catch all keystrokes
    
    glMatrixMode(GL_PROJECTION);            // Select projection matrix
    glLoadIdentity();                        // "Take me home..."
    
    glfwGetWindowSize( &width, &height );    // Get window size
    height = height < 1 ? 1 : height;        // Make sure that height is non-zero to avoid division by zero
    
    gluPerspective(                            // Set perspective view
    45.0,                                    // Field of view = 45 degrees
    (double)width/(double)height,            // Window aspect (assumes square pixels)
    0.1,                                    // Near Z clipping plane
    100.0                                    // Far Z clippling plane
    );
    
    glMatrixMode(GL_MODELVIEW);                // Select modelview matrix
    glLoadIdentity();                        // Start with an identity matrix
    
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    
    GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat LightPosition[]= { 3.0f, 0.0f, 15.0f, 1.0f };
    glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
    glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);
    glEnable(GL_LIGHT0);                
}

void Paint(void)                            //Main painting function
{
    int    width, height;                        // Window dimensions
    double t;                                // Time (in seconds)
    t = glfwGetTime();                        // Get time
    glfwGetWindowSize( &width, &height );    // Get window size
    height = height < 1 ? 1 : height;        // Avoid div by 0
    glViewport( 0, 0, width, height );        // Set viewport
    
    glClear(GL_COLOR_BUFFER_BIT |
            GL_DEPTH_BUFFER_BIT);            // Clear Screen
    glLoadIdentity();                        //Back to 0,0,0
    
    glTranslatef(0.0f, 0.0f, -20.0f);
    glRotatef(90 * (float)t, 0.0, 1.0, 0.0);
    
    glBegin( GL_TRIANGLES );         
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( -5.0f, -5.0f, 5.0f ); 
    glColor3f( 0.0f, 0.0f, 0.3f );    
    glVertex3f(  5.0f, -5.0f, 5.0f ); 
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f(  0.0f, 5.0f, 0.0f ); 
    
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( -5.0f, -5.0f, -5.0f );
    glColor3f( 0.0f, 0.0f, 0.3f );    
    glVertex3f( -5.0f, -5.0f, 5.0f ); 
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( 0.0f, 5.0f, 0.0f );
    
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( 5.0f, -5.0f, -5.0f ); 
    glColor3f( 0.0f, 0.0f, 0.3f );    
    glVertex3f( -5.0f, -5.0f, -5.0f );
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( 0.0f, 5.0f, 0.0f );
    
    
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( 5.0f, -5.0f, 5.0f ); 
    glColor3f( 0.0f, 0.0f, 0.3f );    
    glVertex3f( 5.0f, -5.0f, -5.0f ); 
    glColor3f( 0.0f, 0.0f, 0.0f );    
    glVertex3f( 0.0f, 5.0f, 0.0f );
    glEnd();                      
}