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


damn vertex arrays by luke
1
 
#ifdef _WIN32
    #pragma comment(lib, "glu32.lib")
    #pragma comment(lib, "opengl32.lib")
    #pragma comment(lib, "SDL.lib")
    #pragma comment(lib, "SDLmain.lib")    
    #define WIN32_LEAN_AND_MEAN 1
    #include <windows.h>
#endif

#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"

#include <cstdlib>
#include <cstddef>
#include <iostream>

const int    width    = 800;
const int    height    = 800;
const int    bpp        = 32;
const float    fov        = 45.0f;
const float    nClip    = 1.0f;
const float    fClip    = 500.0f;

void Draw();
bool CheckInput();
void SetOrthoProj();
void ResetPersProj();
void CheckGLErrors();

int main(int argc, char** argv)
{
    if(SDL_InitSubSystem(SDL_INIT_VIDEO) == -1 || SDL_SetVideoMode(width, height, bpp, SDL_OPENGL) == 0)
    {
        SDL_Quit();
        std::cerr << "Initialization has failed, exiting." << std::endl;
        return -1;
    }

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(fov, float(width)/float(height), nClip, fClip);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glPointSize(10);

    while(CheckInput() == true)
    {
        Draw();
        CheckGLErrors();
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

void Draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    SetOrthoProj();

    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    static GLint tempa[] = {    0, 0,
                                100, 100,
                                0, 100};
    
    static GLfloat tempb[] = {    1, 0, 0,
                                0, 1, 0,
                                0, 0, 1};
    
    glVertexPointer(2, GL_INT,    0, tempa);
    glColorPointer(3, GL_FLOAT,    0, tempb);
    
    
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

    glBegin(GL_POINTS);
        glColor3fv(tempb+0);    glVertex2iv(tempa+0);
        glColor3fv(tempb+3);    glVertex2iv(tempa+2);
        glColor3fv(tempb+6);    glVertex2iv(tempa+4);
    glEnd();

    ResetPersProj();
}

bool CheckInput()
{
    static SDL_Event event;
    while(SDL_PollEvent(&event))
    {
        if((event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) || event.type == SDL_QUIT)    return false;
    }
    return true;
}

void SetOrthoProj()
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, double(width), 0, double(height), -1, 1);
    glMatrixMode(GL_MODELVIEW);
}

void ResetPersProj()
{
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}

void CheckGLErrors()
{
    static GLenum error;
    
    error = glGetError();
    if(error != GL_NO_ERROR)
    {
        std::cerr << "* OpenGL Error: " << gluErrorString(error) << std::endl;
    }
}