|
salam all,
Lama x memposting, ak bru blaja Computer Grafik so nak tya skit.
-Macamana nak buat 2 objek (cthnya 2 polygon) tp berbeza pergerakan apabila key ditekan.
Cthnya petak1 apabila tekan key"A" dia bergerak ke kiri, petak 2 apabila tekan "S" ia bergerak ke kanan.
Main point " pergerakan lain pada objek berlainan apabila key ditekan".
-Problem ke 2 plak, pada slot create objek, bla ak aku open "glTranslated" objek trus x kuar bla compile,... stressful toll..
ni coding yg ak maksudkan
------------------------------------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
char rotAxis='n';
double rotX=0.0, rotY=0.0, rotZ=0.0,
vertical=0.0,
horizontal=0.0;
void init()
{
glClearColor(0.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.0);
}
void reset_value(void)
{
rotAxis='n',
rotX=0,rotZ=0,rotY=0,
vertical=0.0,
horizontal=0.0,
init();
}
void testpower()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.5,1.5);
//glTranslated(-1,0,0); <<<-----------------------aku tutup sbb x kuar gambar bla compile
glBegin(GL_POLYGON);
glVertex2f(0.5,0.5);
glVertex2f(-0.5,0.5);
glVertex2f(-0.5,-0.5);
glVertex2f(0.5,-0.5);
glEnd();
/*glColor3f(0.0,0.0,0.0);
glTranslated(2,0,0);
glBegin(GL_POLYGON);
glVertex2f(0.5,0.5);
glVertex2f(-0.5,0.5);
glVertex2f(-0.5,-0.5);
glVertex2f(0.5,-0.5);
glEnd();*/
glFlush();
}
void animation()
{
if (rotAxis=='x')
rotX=0.5;
else if (rotAxis=='y')
rotY=0.5;
else if (rotAxis=='z')
rotZ=0.5;
glRotated(rotX,1.0,0.0,0.0);
glRotated(rotY,0.0,1.0,0.0);
glRotated(rotZ,0.0,0.0,1.0);
glutSwapBuffers();
glutPostRedisplay();
}
void myKeyboard(unsigned char key, int x, int y)
{
if (key == 27)
exit(0);
else if (key == '0')
reset_value();
else if (key == 'x')
rotAxis = 'x';
else if (key == 'y')
rotAxis = 'y';
else if (key == 'z')
rotAxis = 'z';
}
void mySpecial(int key, int x, int y)
{
vertical=0.0;
horizontal=0.0;
if (key==GLUT_KEY_UP)
vertical+=0.2;
else if (key==GLUT_KEY_DOWN)
vertical-=0.2;
else if (key==GLUT_KEY_LEFT)
horizontal-=0.2;
else if (key==GLUT_KEY_RIGHT)
horizontal+=0.2;
glTranslated(horizontal,vertical,0);
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(0,0);
glutCreateWindow("Testing");
glutDisplayFunc(testpower);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecial);
glutIdleFunc(animation);
init();
glutMainLoop();
} |
|