Intelligence Development Program - Lập trình thông minh - Blog về học lập trình, mã nguồn, phần mềm, công nghệ mới mực in mã vạch, chip xử lý mã nguồn.

CÀI ĐẶT OPENGL VỚI VISUAL C++ TRONG WINDOWS




Hướng dẫn cài đặt OpenGL với Visual C++ 6.0 trong Windows.

Download các files OpenGL từ

http://www.xmission.com/~nate/glut.html (bao gồm glut-3.7.6-bin.zip, thư viện và các file header).

Tạo thư mục "GL" trong thư mục include cài Visual C++
c:\program files\microsoft visual studio\vc98\include

Copy các files OpenGL vừa tải về vào thư mục GL vừa tạo
c:\programme\microsoft visual studio\vc98\include\GL

Copy all *.lib files into
c:\program files\microsoft visual studio\vc98\Lib

Doubleclick vào file .c bạn muốn build, Visual C++ sẽ mở file.

Biên dịch compile file hoặc build để VC++ để tạo workspace.

Mở menu VC++ >> project >> settings >> Link và thêm những thư viện libraries vào Object/libary modules bao gồm:
glut32.lib glu32.lib opengl32.lib glaux.lib

Thực thi chương trình!

File code ví dụ template.c
Mã C:

#include 
#include 
#include 
#include 

#include 


/* 
 * Window properties 
 */
#define WINDOW_WIDTH    500
#define WINDOW_HEIGHT   500
#define WINDOW_X        100
#define WINDOW_Y        100
#define WINDOW_TITLE    "RGB-ColorCube"

/* 
 * Perspective properties 
 */
#define FOV_ANGLE       30

#define CENTER_X        0.0
#define CENTER_Y        0.0
#define CENTER_Z        0.0

#define VIEWER_X        0.0
#define VIEWER_Y        0.0
#define VIEWER_Z        -2.1

#define UP_X            0.0
#define UP_Y            1.0
#define UP_Z            0.0

#define CLIPPLANE_NEAR  1.0
#define CLIPPLANE_FAR   20.0

 
#define ROTATION_SPEED  2.0

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

GLfloat rot_x = 0.0, rot_y = 0.0;
GLfloat saved_x, saved_y;

/*
 * Material colors used for shading
 */
GLfloat red[4] = {.8, 0.0, 0.0, 1.0};
GLfloat white[4] = {.8, .8, .8, 1.0};


/*
 * Function prototypes
 */
void usage(void);
void draw_scene(void);
void draw_object(void);
void init(int argc, char **argv, void (*draw)(void));
void save_position(int button, int state, int x, int y);
struct point get_coords(double a, double b);
void vertex(double a, double b);
void rotate(int x, int y);
void set_color(int angle);

int main(int argc, char **argv) {
    /*
     * Init OpenGL and enter the event loop
     */
    init(argc, argv, draw_scene);

    return 0;
}
/*
 * Handle rotations and buffer swapping and call the function draw_object 
 * which does the actual drawing
 */
void draw_scene(void) {
    static GLfloat old_rot_matrix[16];
    static int initialized = 0;
    GLfloat new_rot_matrix[16];

    /* calculate new rotation matrix */
    glPushMatrix();
    glLoadIdentity();
    glRotatef(rot_x, 1.0, 0.0, 0.0);
    glRotatef(rot_y, 0.0, 1.0, 0.0);
    glGetFloatv(GL_MODELVIEW_MATRIX, new_rot_matrix);
    glPopMatrix();
    
    /* calculate total rotation */


/*
 * Handle rotations and buffer swapping and call the function draw_object 
 * which does the actual drawing
 */
void draw_scene(void) {
    static GLfloat old_rot_matrix[16];
    static int initialized = 0;
    GLfloat new_rot_matrix[16];

    /* calculate new rotation matrix */
    glPushMatrix();
    glLoadIdentity();
    glRotatef(rot_x, 1.0, 0.0, 0.0);
    glRotatef(rot_y, 0.0, 1.0, 0.0);
    glGetFloatv(GL_MODELVIEW_MATRIX, new_rot_matrix);
    glPopMatrix();
    
    /* calculate total rotation */
    glPushMatrix();
    glLoadIdentity();
    glMultMatrixf(new_rot_matrix);
    if (initialized) {
      glMultMatrixf(old_rot_matrix);
    }

    glGetFloatv(GL_MODELVIEW_MATRIX, old_rot_matrix);
    initialized = 1;
    glPopMatrix();

    glPushMatrix();
    glMultMatrixf(old_rot_matrix); 

    draw_object();

    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}


/*
 * Initialize the OpenGL machine
 */
void init(int argc, char **argv, void (*draw)(void)) {
    GLfloat light0_pos[] = {100.0, 100.0, 100.0, 1.0};
    GLfloat light0_color[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat ambient_light[] = {0.8, 0.8, 0.8, 1.0};

    glutInit(&argc, argv);

    /* Create a window */
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitWindowPosition(WINDOW_X, WINDOW_Y);
    glutCreateWindow(WINDOW_TITLE);
    glClearColor(0.0, 0.0, 0.0, 0.0);

    /* Set up some light sources */
    glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_light);

    glEnable(GL_LIGHT0);
    /* glEnable(GL_LIGHTING); */

    glEnable(GL_DEPTH_TEST);

    glShadeModel(GL_SMOOTH);

    /* Create a viewing frustum */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(FOV_ANGLE, WINDOW_WIDTH/WINDOW_HEIGHT, CLIPPLANE_NEAR, 
        CLIPPLANE_FAR);
    gluLookAt(VIEWER_X, VIEWER_Y, -VIEWER_Z, CENTER_X, CENTER_Y, CENTER_Z, 
        UP_X, UP_Y, UP_Z);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(VIEWER_X, VIEWER_Y, VIEWER_Z);

    glutDisplayFunc(draw);
    glutMouseFunc(save_position);
    glutMotionFunc(rotate);
    glutMainLoop();

    /* Not reached */
}


/*
 * Save the position of the mouse pointer where the bottun press occured
 */
void save_position(int button, int state, int x, int y) {
    if (state == GLUT_DOWN) {
        saved_x = x;
        saved_y = y;
    }
}

/*
 * Calculate the angle the object has rotated by since the last update
 */
void rotate(int x, int y) {
    rot_y = (GLfloat)(x - saved_x) * ROTATION_SPEED;
    rot_x = (GLfloat)(y - saved_y) * ROTATION_SPEED;
    saved_x = x;
    saved_y = y;
    
    glutPostRedisplay();
}


/*
 * Draw a object
 */
void draw_object(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_QUAD_STRIP);
   
      glNormal3f(0,0,1);
      
      glColor3f(0,0,0);
      glVertex3f(-0.5,-0.5,-0.5);
      
      glColor3f(0,1,0);
      glVertex3f(-0.5,0.5,-0.5);

      glColor3f(1,0,0);
      glVertex3f(0.5,-0.5,-0.5);

      glColor3f(1,1,0);
      glVertex3f(0.5,0.5,-0.5);
       
      glNormal3f(1,0,0);
      glColor3f(1,0,1);
      glVertex3f(0.5,-0.5,0.5);
      
      glColor3f(1,1,1);
      glVertex3f(0.5,0.5,0.5);    

      glNormal3f(0,0,1);
      glColor3f(0,0,1);
      glVertex3f(-0.5,-0.5,0.5);
      glColor3f(0,1,1);
      glVertex3f(-0.5,0.5,0.5);
      
      glNormal3f(-1,0,0);
      glColor3f(0,0,0);
      glVertex3f(-0.5,-0.5,-0.5);
      glColor3f(0,1,0);
      glVertex3f(-0.5,0.5,-0.5);

    glEnd();

    
   glBegin(GL_QUADS);
   
      glNormal3f(0,1,0);
      
      glColor3f(0,1,0);
      glVertex3f(-0.5,0.5,-0.5);
      
      glColor3f(0,1,1);
      glVertex3f(-0.5,0.5,0.5);

      glColor3f(1,1,1);
      glVertex3f(0.5,0.5,0.5);

      glColor3f(1,1,0);
      glVertex3f(0.5,0.5,-0.5);

    glEnd();

    glBegin(GL_QUADS);
   
      glNormal3f(0,-1,0);
      
      glColor3f(0,0,0);
      glVertex3f(-0.5,-0.5,-0.5);
      
      glColor3f(0,0,1);
      glVertex3f(-0.5,-0.5,0.5);

      glColor3f(1,0,1);
      glVertex3f(0.5,-0.5,0.5);

      glColor3f(1,0,0);
      glVertex3f(0.5,-0.5,-0.5);

    glEnd();
    
}


Ebook