คือผมเขียนโค้ด รูปสี่เหลี่ยมแบบ3D แต่คือมันยังผิดอยู่อีก 8 ตัวผมพยายามหาแล้วแต่ก็ยังรันไม่ได้ครับ
ผู้รู้ช่วยที
GLfloat xt=0.0, yt=0.0, zt=0.0;
Glfloat xs=1.0, ys=1.0, zs=1.0;
GLfloat xangle=20.0,yangle=20.0,zangle=20.0;
GLint window;
void init(void)
{
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-6,6,-6,6,-6,6);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myDisplay(void)
{
glclear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Anti-Alias
glEnable (GL_LINE_SMOOTH);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(0.0,0.0,1.0); //set color to blue.
glBegin (GL_LINES); //Draw x-axis
glVertex3f (-5.5, 0.0, 0.0);
glVertex3f (5.5, 0.0, 0.0);
glEnd( );
glBegin (GL_LINES); //Draw y-axis
glVertex3f (0.0,-5.5, 0.0);
glVertex3f (0.0, 5.5, 0.0);
glEnd( );
glPushMatrix();
glTranslatef(xt, yt, 0.0); //Translation
glScalef(xs, ys, 1.0); //Scaling
glRotatef(xangle, 1.0, 0.0, 0.0); //Rotate x-axis
glRotatef(yangle, 0.0, 1.0, 0.0); //Rotate y-axis
glRotatef(zangle, 0.0, 0.0, 1.0); //Rotate z-axis
glColor3f(0.0, 0.0, 1.0); //set color to blue
glutWireCube (2);
//glutWireTeapot (2);
glPopMatrix();
glutSwapBuffers();
glFlush ( );
}
void KeyboardAssign (GLubyte key, GLint x, GLint y)
{
switch (key)
{
case ESCAPE :
printf("escape pressed. exit.\n");
glutDestroyWindow(window);
exit(0);
break;
case 't':
xt += 0.2; //xt=xt*2
glutPostRedisplay();
break;
case 'T':
yt += 0.2; //xt=xt*2
glutPostRedisplay();
break;
case 's': //Scale y up
xs += 0.2;
glutPostRedisplay();
break;
case 'S': //Scale y up
ys += 0.2;
glutPostRedisplay();
break;
case 'x': //Rotate Counter ClockWise x-axis
xangle += 10.0;
glutPostRedisplay();
break;
case 'y': //Rotate Counter ClockWise y-axis
yangle += 10.0;
glutPostRedisplay();
break;
case 'z': //Rotate Counter ClockWise x-axis
zangle += 10.0;
glutPostRedisplay();
break;
default:
break;
}
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (50,50);
glutInitWindowSize(500,500);
glutCreateWindow("t=tran x, T=tran y, s=scale x, S=scale y, x=rot x, y=rot y, z=rot z");
int();
glutDisplayFunc (myDisplay);
glutIdleFunc(myDisPlay);
glutKeyboardFunc (KeyboardAssign);
glutMainLoop();
}
เขียนโค้ดรูปสี่เหลี่ยม 3D
ผู้รู้ช่วยที
GLfloat xt=0.0, yt=0.0, zt=0.0;
Glfloat xs=1.0, ys=1.0, zs=1.0;
GLfloat xangle=20.0,yangle=20.0,zangle=20.0;
GLint window;
void init(void)
{
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-6,6,-6,6,-6,6);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myDisplay(void)
{
glclear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Anti-Alias
glEnable (GL_LINE_SMOOTH);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(0.0,0.0,1.0); //set color to blue.
glBegin (GL_LINES); //Draw x-axis
glVertex3f (-5.5, 0.0, 0.0);
glVertex3f (5.5, 0.0, 0.0);
glEnd( );
glBegin (GL_LINES); //Draw y-axis
glVertex3f (0.0,-5.5, 0.0);
glVertex3f (0.0, 5.5, 0.0);
glEnd( );
glPushMatrix();
glTranslatef(xt, yt, 0.0); //Translation
glScalef(xs, ys, 1.0); //Scaling
glRotatef(xangle, 1.0, 0.0, 0.0); //Rotate x-axis
glRotatef(yangle, 0.0, 1.0, 0.0); //Rotate y-axis
glRotatef(zangle, 0.0, 0.0, 1.0); //Rotate z-axis
glColor3f(0.0, 0.0, 1.0); //set color to blue
glutWireCube (2);
//glutWireTeapot (2);
glPopMatrix();
glutSwapBuffers();
glFlush ( );
}
void KeyboardAssign (GLubyte key, GLint x, GLint y)
{
switch (key)
{
case ESCAPE :
printf("escape pressed. exit.\n");
glutDestroyWindow(window);
exit(0);
break;
case 't':
xt += 0.2; //xt=xt*2
glutPostRedisplay();
break;
case 'T':
yt += 0.2; //xt=xt*2
glutPostRedisplay();
break;
case 's': //Scale y up
xs += 0.2;
glutPostRedisplay();
break;
case 'S': //Scale y up
ys += 0.2;
glutPostRedisplay();
break;
case 'x': //Rotate Counter ClockWise x-axis
xangle += 10.0;
glutPostRedisplay();
break;
case 'y': //Rotate Counter ClockWise y-axis
yangle += 10.0;
glutPostRedisplay();
break;
case 'z': //Rotate Counter ClockWise x-axis
zangle += 10.0;
glutPostRedisplay();
break;
default:
break;
}
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (50,50);
glutInitWindowSize(500,500);
glutCreateWindow("t=tran x, T=tran y, s=scale x, S=scale y, x=rot x, y=rot y, z=rot z");
int();
glutDisplayFunc (myDisplay);
glutIdleFunc(myDisPlay);
glutKeyboardFunc (KeyboardAssign);
glutMainLoop();
}