これは、3D ピラミッドを表示し、キーボードを使用して回転させるために作成したコードです。光源も用意しましたが、ピラミッドと一緒に回転します。光源は固定でお願いします。私は何をしますか?CodeBlocks でプログラミングを行いました。
#include<windows.h>
#include<GL/glut.h>
#include<stdlib.h>
double rotate_y=0;
double rotate_x=0;
double rotate_z=0;
void specialkeys(int key, int x, int y)
{
if(key==GLUT_KEY_RIGHT)
rotate_y-=5;
else if(key==GLUT_KEY_LEFT)
rotate_y+=5;
else if(key==GLUT_KEY_UP)
rotate_x+=5;
else if(key==GLUT_KEY_DOWN)
rotate_x-=5;
else if(key==GLUT_KEY_PAGE_UP)
rotate_z+=5;
else if(key==GLUT_KEY_PAGE_DOWN)
rotate_z-=5;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotate_x,1.0,0.0,0.0);
glRotatef(rotate_y,0.0,1.0,0.0);
glRotatef(rotate_z,0.0,0.0,1.0);
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.0,0.0,0.0);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.5, 0.5, -0.5 );
glEnd();
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( -0.5, 0.5, -0.5 );
glEnd();
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
glFlush();
glutSwapBuffers();
}
void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lmodel_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Pyramid!");
init();
glutDisplayFunc(display);
glutSpecialFunc(specialkeys);
glutMainLoop();
return 0;
}