Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include "stdlib.h"
- #include "math.h"
- #include "stdio.h"
- #include <GL\freeGlut.h>
- //---------------------------------------------------------------------------
- //------------------------------------------------------- Variaveis globais
- //---------------------------------------------------------------------------
- GLfloat wC = 10.0, hC = 10.0; //.. coordenadas reais - definição do mundo
- GLfloat wSup = 4.0, hSup = 1.0; //.. dimensoes do suporte
- GLfloat xSup = 0.0, incSup = 0.45; //.. posicao horizontal do suporte / incremento para deslocamento
- GLfloat wLig = 0.8, hLig = 3.0;
- GLfloat agLig = 90.0, incLig = 10.0;
- GLfloat wExt = 3.0, hExt = 0.8;
- GLfloat agExt = 0.0, incExt = 10.0;
- GLint msec = 20; //.. definicao do timer (actualizacao)
- char texto[30]; //.. texto a ser escrito no ecran
- GLint tentativas = 0; //.. numero de blocos gerados
- //++++++++++++++++++++++++++++++++++++++++++++ fazer pelos alunos
- GLfloat Matriz[4][4]; // para obter a posição Px, Py
- GLfloat Px, Py; // posição final da extremidade
- //++++++++++++++++++++++++++++++++++++++++++++ fazer pelos alunos
- //---------------------------------------------------------------------------
- //---------------------------------------------------- Inicializa parâmetros
- //---------------------------------------------------------------------------
- void inicializa(void) {
- glClearColor(0, 0, 0, 1);
- glShadeModel(GL_SMOOTH);
- gluOrtho2D(-wC, wC, -hC, hC); // coordenadas mundo
- srand(1); // para gerar posicao aleatória
- }
- float aleatorio(GLint minimo, GLint maximo) {
- GLfloat y;
- y = rand() % 1000;
- return (minimo + 0.001 * y * (maximo - minimo));
- }
- //---------------------------------------------------------------------------
- //----------------------------------------------------------------- Desenhar
- //---------------------------------------------------------------------------
- void desenhaQuadrado(GLfloat tam, GLfloat r, GLfloat g, GLfloat b) {
- //------------------------------------ desenhado na origem
- glColor3f(r, g, b);
- glBegin(GL_QUADS);
- glVertex2f(0 - tam, 0 - tam);
- glVertex2f(0 - tam, 0 + tam);
- glVertex2f(0 + tam, 0 + tam);
- glVertex2f(0 + tam, 0 - tam);
- glEnd();
- }
- void desenhaTexto(char* string, GLfloat x, GLfloat y) {
- glRasterPos2f(x, y);
- while (*string)
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string++);
- }
- void desenhaRobot(GLfloat xSup, GLfloat agLig, GLfloat agExt) {
- int i, j;
- glPushMatrix();
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Suporte vermelho
- glTranslatef(xSup, 0.0, 0.0);
- glGetFloatv(GL_MODELVIEW_MATRIX, &Matriz[0][0]); // valor final do ponto 'extremidade'
- glPushMatrix();
- glScalef(wSup, hSup, 1.0);
- desenhaQuadrado(1.0, 1, 0, 0);
- glPopMatrix();
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Ligacao verde
- glRotatef(agLig, 0.0, 0.0, 1.0);
- glTranslatef(0.0, hLig, 0.0);
- glPushMatrix();
- glScalef(wLig, hLig, 1.0);
- desenhaQuadrado(1.0, 0, 1, 0);
- glPopMatrix();
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Extremidade Azul
- glTranslatef(0, hLig, 0.0);
- glRotatef(agExt, 0.0, 0.0, 1.0);
- glPushMatrix();
- glScalef(wExt, hExt, 1.0);
- desenhaQuadrado(1.0, 0, 0, 1);
- glPopMatrix();
- glPopMatrix();
- }
- //---------------------------------------------------------------------------
- //.............................................................. Desenha Cena
- //---------------------------------------------------------------------------
- void desenhaJanela(void) {
- int i, j;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~limpa
- glClear(GL_COLOR_BUFFER_BIT);
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~BraçoArticulado
- desenhaRobot(xSup, agLig, agExt);
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Quadrado a cair
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Apanhou ou nao??
- //+++++++++++++++++++++++++++++++++++++ posicao final da extremidade
- // o glGetFloatv está no local pretendido !!!!!!!
- // onde se quer conher a posicao final
- //glGetFloatv(GL_MODELVIEW_MATRIX, &Matriz[0][0]); // valor final do ponto 'extremidade '
- Px = wC * Matriz[3][0];
- Py = hC * Matriz[3][1];
- printf(" Px=%4.3f Py=%4.3f\n ", Px, Py);
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX A fazer pelos alunos
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Actualizar tentativas e apanhados
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX A fazer pelos alunos
- sprintf_s(texto, "Tentativas = %d", tentativas);
- desenhaTexto(texto, -wC + 1, hC - 3);
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX A fazer pelos alunos
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Informacao
- glColor3f(0, 0, 0.97);
- char t[] = "{jh,ep}@dei.uc.pt";
- desenhaTexto(t, -wC + 0.2, -hC + 0.1);
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Refresh
- glutSwapBuffers();
- }
- //---------------------------------------------------------------------------
- //......................................................................Timer
- //---------------------------------------------------------------------------
- void Timer(int value) {
- glutPostRedisplay();
- glutTimerFunc(msec, Timer, 1);
- }
- //---------------------------------------------------------------------------
- //....................................................................Eventos
- //---------------------------------------------------------------------------
- void teclasNotAscii(int key, int x, int y) {
- if (key == GLUT_KEY_LEFT) {
- xSup = xSup - incSup;
- glutPostRedisplay();
- }
- if (key == GLUT_KEY_RIGHT) {
- xSup = xSup + incSup;
- glutPostRedisplay();
- }
- if (key == GLUT_KEY_UP) {
- agLig = agLig + incLig;
- glutPostRedisplay();
- }
- if (key == GLUT_KEY_DOWN) {
- agLig = agLig - incLig;
- glutPostRedisplay();
- }
- }
- void teclado(unsigned char key, int x, int y) {
- switch (key) {
- case 'A':
- case 'a':
- agExt = agExt + incExt;
- glutPostRedisplay();
- break;
- case 'Q':
- case 'q':
- agExt = agExt - incExt;
- glutPostRedisplay();
- break;
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- //-----------------------------------------------------
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(300, 100);
- glutCreateWindow("Teclas: left, right, up, down, q, a ");
- inicializa();
- glutDisplayFunc(desenhaJanela);
- glutKeyboardFunc(teclado);
- glutSpecialFunc(teclasNotAscii);
- glutTimerFunc(msec, Timer, 1);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement