Vdo OpenGL example
code
#include <windows.h>#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
init
void init(void)
{
float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
float mat_shininess[] = {50.0};
float light_position[] = {1.0, 1.0, 1.0, 0.0};
float light_ambient[] = {0.5, 0.5, 0.5, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glColorMaterial(GL_FRONT_AND_BACK,
GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.0);
theObj = gluNewQuadric();
}
display
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
.........
.....
}
reshape
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h && w>0)
glOrtho(-SIZEW,SIZEW,(GLdouble)-h/w*SIZEW,
(GLdouble)h/w*SIZEW,0.0,2*SIZEW);
else if(h>0)
glOrtho((GLdouble)-w/h*SIZEW,(GLdouble)w/h*SIZEW,
-SIZEW,SIZEW,0.0,2*SIZEW);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-SIZEW);
}
keyboard
void keyboard(unsigned char key, int x, int y)
{
switch(key){
case 'q':
exit(0);
break;
case 'f':
glutFullScreen();
break;
case 'w':
glutReshapeWindow(640, 480);
break;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h && w>0)
glOrtho(-SIZEW,SIZEW,(GLdouble)-h/w*SIZEW,
(GLdouble)h/w*SIZEW,0.0,2*SIZEW);
else if(h>0)
glOrtho((GLdouble)-w/h*SIZEW,(GLdouble)w/h*SIZEW,
-SIZEW,SIZEW,0.0,2*SIZEW);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-SIZEW);
}
keyboard
void keyboard(unsigned char key, int x, int y)
{
switch(key){
case 'q':
exit(0);
break;
case 'f':
glutFullScreen();
break;
case 'w':
glutReshapeWindow(640, 480);
break;
}
}
Mouse
void Mouse(int x, int y)
{
if (GLUT_LEFT_BUTTON == GLUT_DOWN)
{
Y = y++%360;
X = x++%360;
}
glutPostRedisplay();
}
void Mouse(int x, int y)
{
if (GLUT_LEFT_BUTTON == GLUT_DOWN)
{
Y = y++%360;
X = x++%360;
}
glutPostRedisplay();
}
main
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutMotionFunc(Mouse);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutMotionFunc(Mouse);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น