1. Trang chủ
  2. » Công Nghệ Thông Tin

code ví dụ về ngôn ngữ openGL

4 710 6

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 48,23 KB

Nội dung

Trang 1

//Vẽ chong chóng quay theo thời gian

#include <glut.h>

GLfloat anpha=0;

void init()

{

glClearColor (1.0, 0.0, 0.0, 1.0);

//glColor3f(1.0, 1.0, 1.0);

glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); }

void mydisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix(); //Quay chong chóng

glRotatef(anpha, 0, 0, 1);

//Vẽ chong chóng

glPushMatrix();

for(int i=1;i<=4;i++)

{

if (i==1) glColor3f(0, 1, 0);

if (i==2) glColor3f(0, 0, 1);

if (i==3) glColor3f(1, 1, 0);

if (i==4) glColor3f(0, 1, 1);

glBegin(GL_POLYGON);

glVertex3f(0.0, 0.5,0.0);

glVertex3f(0.15, 0.2, 0.0); glVertex3f(0.0, 0.0, 0.0);

glEnd();

glRotatef(90,0,0,1);

}

glPopMatrix(); //Kết thúc vẽ chong chóng

glPopMatrix(); //Kết thúc quay chong chóng glutSwapBuffers();

}

void timer(int )

{

anpha+=10;

if (anpha>360) anpha=0;

glutTimerFunc(50, timer,v);

glutPostRedisplay();

}

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("simple");

init();

Trang 2

glutDisplayFunc(mydisplay);

timer(0);

glutMainLoop();

}

// Vẽ bàn rồi quay theo các trục điều khiển bởi phím

#include <glut.h>

GLfloat gocx=0, gocy=0, gocz=0;

void init(void)

{

glClearColor (0.0, 0.0, 0.0, 0.0);

glShadeModel (GL_FLAT);

}

void matban(GLfloat , GLfloat , GLfloat )

{

glPushMatrix();

glScalef (x, y, z); /* modeling transformation */ glutWireCube (1.0);

glPopMatrix();

}

void chanban(GLfloat , GLfloat , GLfloat )

{

glPushMatrix();

glScalef (x, y, z);

glutWireCube (1.0);

glPopMatrix();

}

void veban(GLfloat , GLfloat , GLfloat , GLfloat ) {

matban(l, d, w);

glPushMatrix ();

glTranslatef(l/2-d/2, -h/2, w/2-d/2);

chanban(d, h, d);

glPopMatrix ();

glPushMatrix ();

glTranslatef(l/2-d/2, -h/2, -(w/2-d/2));

chanban(d, h, d);

glPopMatrix ();

glPushMatrix ();

glTranslatef(-(l/2-d/2), -h/2, w/2-d/2);

chanban(d, h, d);

glPopMatrix ();

glPushMatrix ();

glTranslatef(-(l/2-d/2), -h/2, -(w/2-d/2));

chanban(d, h, d);

glPopMatrix ();

}

void display(void)

{

Trang 3

glClear (GL_COLOR_BUFFER_BIT);

glColor3f (1.0, 1.0, 1.0);

glPushMatrix ();

glRotatef (gocx,1,0,0);

glRotatef (gocy,0,1,0);

glRotatef (gocz,0,0,1);

veban(2, 1, 0.8, 0.1);

glPopMatrix ();

glFlush ();

}

void keyboard(unsigned char key, int , int )

{

switch (key)

{ case 'x':

gocx+=5;

if (gocx>=360) gocx =0;

glutPostRedisplay();

break;

case 'y':

gocy+=5;

if (gocy>=360) gocy =0;

glutPostRedisplay();

break;

case 'z':

gocz+=5;

if (gocz>=360) gocz =0;

glutPostRedisplay();

break;

default:

gocx=0; gocy =0; gocz=0;

glutPostRedisplay();

break;

}

}

void reshape (int , int )

{

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

glMatrixMode (GL_MODELVIEW);

glLoadIdentity (); /* clear the matrix */

/* viewing transformation */

gluLookAt (2.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); }

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (500, 500);

glutInitWindowPosition (100, 100);

glutCreateWindow (argv[0]);

init ();

glutDisplayFunc(display);

Trang 4

glutReshapeFunc(reshape); glutKeyboardFunc (keyboard); glutMainLoop();

return 0;

}

Ngày đăng: 09/05/2016, 22:39

TỪ KHÓA LIÊN QUAN

w