做了一個可以任意移動旋轉的綠色立體三角柱
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0, angle2=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 1, 0, 0);
glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glEnd();
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2, -0.8);
glVertex3f( 0.2,-0.2, -0.8);
glVertex3f(-0.2,-0.2, -0.8);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, -0.8);
glVertex3f( 0.2,-0.2, -0.8);
glVertex3f(-0.2,-0.2, -0.8);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glPopMatrix();
glFlush();
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
angle = x;
angle2 =y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160765:)");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
多兩行使方塊更好旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0, angle2=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 1, 0, 0);
glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glEnd();
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2, -0.8);
glVertex3f( 0.2,-0.2, -0.8);
glVertex3f(-0.2,-0.2, -0.8);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, -0.8);
glVertex3f( 0.2,-0.2, -0.8);
glVertex3f(-0.2,-0.2, -0.8);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glPopMatrix();
glFlush();
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
angle = x;
angle2 =y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160765:)");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言