2012年4月27日 星期五

OpenGL: General Strucure

since: 2012/04/27
update: 2012/04/28

reference:
1. OpenGL: A Primer, 3/e
2. 投影的分類(Classification) - 3D2D projection


A. 說明:

     延續 I touchs: Beginning OpenGL on Mac OS X 這篇文章的專案, 來調整成
     OpenGL 常用的程式結構.

-------------------------------------------------------------------------------------

B. 開啓 main.cpp 檔案, 修改如下:
....
//@add: Let us create a function to initialize everything
void init()
{
    // This determines the background color, in this case: gray
    // state: current clear color
    glClearColor(0.5, 0.5, 0.5, 1.0); // R, G, B, A
   
    // Determines which matrix to apply operations to.
    // value: GL_PROJECTION or GL_MODELVIEW
    glMatrixMode(GL_PROJECTION); // default vlew
   
    // Loads the Identity matrix
    glLoadIdentity();
   
    // 正投影(Orthographic Projection)
    //
    // The gluOrtho2D function defines a 2-D orthographic projection matrix.
    //
    // Set the size and projection of the buffer
    // (x left, x right, y bottom, y top)
    // corner => (left, bottom), (right, top)
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // default
}

void display()
{
    // Clear the buffer
    glClear(GL_COLOR_BUFFER_BIT);
   
    //@add: Set the color for this polygon (Red)
    //
    //state: current drawing color
    glColor3f(1.0, 0.0, 0.0);
   
    // Let us begin drawing some points
    glBegin(GL_POLYGON);
   
    // Specify the points
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5, 0.5);
    glVertex2f(0.5, 0.5);
    glVertex2f(0.5, -0.5);
   
    // Ok we are done specifying points
    glEnd();
   
    // Write this out to the screen
    glFlush();
}

int main(int argc, char * argv[])
{
    // insert code here...
    //std::cout << "Hello, World!\n";
   
    //@add for OpenGL
    //
    // Init glut
    glutInit(&argc, argv);
   
    //@add: Specify the Display Mode
    //
    // this one means there is a single buffer
    // and uses RGB to specify colors (default)
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); 
   
    //@add: Set the window size
    glutInitWindowSize(640, 480);
   
    //@add: Where do we want to place the window initially?
    glutInitWindowPosition(100,100);
   
    // Name the window and create it:
    //
    // default position: upper-left corner
    // default size: 300 x 300 pixels
    glutCreateWindow("Hello OpenGL");
   
    // Set the callback function, will be called as needed
    glutDisplayFunc(display);
   
    //@add: Initialize the window
    init();
   
    // Start the main loop running, nothing after this
    // will execute for all eternity (right now)
    glutMainLoop();
   
    //@update: will not be executed
    //return 0;
}

-------------------------------------------------------------------------------------

C. 編譯並執行:

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。