Witam, robię właśnie takie zadanie
"Zmodyfikuj program, aby po wciśnięciu ‘o’ używanie klawiszy (a,w,s,d) umożliwiało przesuwanie okręgu, zaś po wciśnięciu ‘k’ przesuwanie kwadratu." I na razie wygląda to u mnie tak:
// opengl.cpp : Defines the entry point for the console application.
//
#include "glut.h"
#include "math.h"
float x = 0, y = 0;
int kolor = 0;
void drawCircle(float xc, float yc, float rad)
{
//
// draw a circle centered at (xc,yc) with radius rad
//
glPushMatrix(); //zachowuje aktualna macierz transformacji
glTranslatef(xc, yc, 0); //przesuwa uklad o dany wektor
glBegin(GL_LINE_LOOP);
//
for (int angle = 0; angle < 365; angle = angle + 5)
{
float angle_radians = angle * (float)3.14159 / (float)180;
float x = rad * (float)cos(angle_radians);
float y = rad * (float)sin(angle_radians);
glVertex3f(x, y, 0);
}
//
glEnd();
glPopMatrix(); //przywracan poprzednia macierz transformacji
}
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(x, y, 0);
//glRotatef(kat,0,0,1); rotacja w okolo osi z
if (kolor > 3) kolor = 1;
if (kolor == 1) glColor3f(1.0, 0.0, 0.0);//pokoloruj red
if (kolor == 2) glColor3f(0.0, 1.0, 0.0);//pokoloruj green
if (kolor == 3) glColor3f(0.0, 0.0, 1.0);//pokoloruj blue
/* draw unit square polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glPopMatrix();
//rysowanie okregu
drawCircle(0, 0, 1);
glEnd();
//
glFlush();//natychmiastowe wykonanie wszystkich poprzednich polecen
}
static void Przesuniecie(unsigned char przycisk)
{
switch (przycisk)
{
case 'o':
Key()
break;
case 'k':
break;
case ' ':
kolor++;
glutPostRedisplay();
break;
case 27:
exit(0);
}
}
static void Key(unsigned char key, int x_d, int y_d)
{
switch (key)
{
case 'w':
y += .1;
glutPostRedisplay();
break;
case 's':
y -= .1;
glutPostRedisplay();
break;
case 'a':
x -= .1;
glutPostRedisplay();
break;
case 'd':
x += .1;
glutPostRedisplay();
break;
}
}
void init()
{
/* set clear color to black */
glClearColor(0.0, 0.0, 0.0, 0.0);
/* set fill color to white */
glColor3f(1.0, 1.0, 1.0);
/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple");
glutDisplayFunc(display);
glutKeyboardFunc(Key);
init();
glutMainLoop();
return 0;
}
No i właśnie nie wiem jak zrobić, żeby jak ktoś wciśnie "o" lub "k" to, żeby przechodziło do drugiej funkcji, bo ta zmienna "key" cały czas się będzie zmieniać. Chyba, że to należy jakoś inaczej ugryźć.
Bo na początku to wyglądało ogółem u mnie tak o:
// opengl.cpp : Defines the entry point for the console application.
//
#include "glut.h"
#include "math.h"
float x = 0, y = 0;
int kolor = 0;
void drawCircle(float xc, float yc, float rad)
{
//
// draw a circle centered at (xc,yc) with radius rad
//
glPushMatrix(); //zachowuje aktualna macierz transformacji
glTranslatef(xc, yc, 0); //przesuwa uklad o dany wektor
glBegin(GL_LINE_LOOP);
//
for (int angle = 0; angle < 365; angle = angle + 5)
{
float angle_radians = angle * (float)3.14159 / (float)180;
float x = rad * (float)cos(angle_radians);
float y = rad * (float)sin(angle_radians);
glVertex3f(x, y, 0);
}
//
glEnd();
glPopMatrix();
}
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(x, y, 0);
//glRotatef(kat,0,0,1); rotacja w okolo osi z
if (kolor > 3) kolor = 1;
if (kolor == 1) glColor3f(1.0, 0.0, 0.0);//pokoloruj red
if (kolor == 2) glColor3f(0.0, 1.0, 0.0);//pokoloruj green
if (kolor == 3) glColor3f(0.0, 0.0, 1.0);//pokoloruj blue
/* draw unit square polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glPopMatrix();
//rysowanie okregu
drawCircle(0, 0, 1);
glEnd();
//
glFlush();//natychmiastowe wykonanie wszystkich poprzednich polecen
}
static void Key(unsigned char key, int x_d, int y_d)
{
switch (key)
{
case 'w':
y += .1;
glutPostRedisplay();
break;
case 's':
y -= .1;
glutPostRedisplay();
break;
case 'a':
x -= .1;
glutPostRedisplay();
break;
case 'd':
x += .1;
glutPostRedisplay();
break;
case ' ':
kolor++;
glutPostRedisplay();
break;
case 27:
exit(0);
}
}
void init()
{
/* set clear color to black */
glClearColor(0.0, 0.0, 0.0, 0.0);
/* set fill color to white */
glColor3f(1.0, 1.0, 1.0);
/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple");
glutDisplayFunc(display);
glutKeyboardFunc(Key);
init();
glutMainLoop();
return 0;
}