1. Conceitos Iniciais
1.1. quadrados4
-
Código fonte: quadrados4.c
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
void display(void);
void timer(int);
void keyboard(unsigned char key, int x, int y);
//cores dos quadrados
GLfloat r=0.8, g=0.5, b=0.3;
//flags para controle das cores
bool bigR = false, bigG = false, bigB = false;
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
// inicia um temporizador. após 33ms ativa a funcao timer
glutTimerFunc(50, timer, 1);
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel (GL_FLAT);
glOrtho (0, 1, 0, 1, -1 ,1);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void timer(int value){
if(r>1) bigR = true;
else if(r<0) bigR = false;
if(g>1) bigG = true;
else if(g<0) bigG = false;
if(b>1) bigB = true;
else if(b<0) bigB = false;
if(bigR == false) r=r+0.01;
else r=r-0.01;
if(bigG == false) g=g+0.03;
else g=g-0.03;
if(bigB == false) b=b+0.05;
else b=b-0.05;
glutPostRedisplay();
glutTimerFunc(50, timer, 1);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < 4; i++){
glColor3f (r+i*0.2, g+i*0.2, b+i*0.2);
glBegin(GL_POLYGON);
glVertex2f(0.10+i*0.1,0.10+i*0.1);
glVertex2f(0.60+i*0.1,0.10+i*0.1);
glVertex2f(0.60+i*0.1,0.60+i*0.1);
glVertex2f(0.10+i*0.1,0.60+i*0.1);
glEnd();
glFlush();
}
glutSwapBuffers();
}
-
Resultado
1.2. quadradoscolor
-
Código fonte: quadradoscolor.c
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
void display(void);
void timer(int);
void keyboard(unsigned char key, int x, int y);
void HSItoRGB(int h, float s, float i);
float rad(float a);
/* cores do quadrado */
GLfloat r=0.0, g=0.0, b=0.0;
float s, i;
int h[4];
int main(int argc, char** argv){
for (int k=0; k<4; k++)
h[k] = 0;
s = 1.0;
i = 1.0;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
// inicia um temporizador. Após 100ms ativa a funcao timer
glutTimerFunc(100, timer, 1);
glClearColor(1.0, 1.0, 1.0, 0.0);
//glShadeModel (GL_FLAT);
glOrtho (0, 1, 0, 1, -1 ,1);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void timer(int value){
h[0] = (h[0] + 80)%360;
glutPostRedisplay();
glutSwapBuffers();
glutTimerFunc(100, timer, 1);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
//vertice1
HSItoRGB(h[0], s, i);
glColor3f (r, g, b);
glVertex2f(0.25,0.25);
//vertice2
HSItoRGB(h[1], s, i);
glColor3f (r, g, b);
glVertex2f(0.75,0.25);
//vertice3
HSItoRGB(h[2], s, i);
glColor3f (r, g, b);
glVertex2f(0.75,0.75);
//vertice4
HSItoRGB(h[3], s, i);
glColor3f (r, g, b);
glVertex2f(0.25,0.75);
glEnd();
glFlush();
//troca das cores
h[3]=h[2];
h[2]=h[1];
h[1]=h[0];
}
//função de HSI para RGB
void HSItoRGB(int h, float s, float i) {
if (h>= 0 && h < 120)
{
b = i*(1-s)/3;
r = i*(1+(s*cos(rad(h))/cos(rad(60-h))))/3;
g = i*(1-(r+b));
} else if (h < 240)
{
h = h-120;
r = i*(1-s)/3;
g = i*(1+(s*cos(rad(h))/cos(rad(60-h))))/3;
b = i*(1-(r+g));
} else if (h <= 360)
{
h = h-240;
g = i*(1-s)/3;
b = i*(1+(s*cos(rad(h))/cos(rad(60-h))))/3;
r = i*(1-(g+b));
}
}
float rad(float a) {
return ((float)a)*M_PI/180.0;
}
-
Resultado
2. Desenhando linhas e pontos
2.1. quadrado
-
Código fonte: quadrado.c
#include <GL/glut.h>
#include <stdlib.h>
void init(void);
void display(void);
void keyboard(unsigned char key, int x, int y);
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Desenhando um quadrado");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho (0, 256, 0, 256, -1 ,1);
glColor3f (1.0, 0.0, 0.0);
}
void display(void){
int i;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2i(30, 226); glVertex2i(226,226);
glVertex2i(226,226); glVertex2i(226,30);
glVertex2i(226,30); glVertex2i(30,30);
glVertex2i(30,30); glVertex2i(30,226);
glEnd();
glFlush();
}
void keyboard(unsigned char key, int x, int y){
switch (key) {
case 97:
glColor3f (0.0, 0.0, 1.0);
glutPostRedisplay();
break;
case 118:
glColor3f (1.0, 0.0, 0.0);
glutPostRedisplay();
break;
}
}
-
Resultado
2.2. Algoritmo de bresenham para traçado de linha
-
Código fonte: quadrado.c
#include <GL/glut.h>
#include <stdlib.h>
void init(void);
void display(void);
int x1=40, y1=200, x2=200, y2=10, x, y, delta_x, delta_y, s1, s2, temp, new_e;
bool troca=0;
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Desenhando uma linha verde");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(0.0, 0.0, 0.0, 1.0);
glOrtho (0, 256, 0, 256, -1 ,1);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);
glBegin(GL_POINTS);
x = x1;
y = y1;
delta_x = abs(x2 - x1);
delta_y = abs(y2 - y1);
if (x2 < x1) s1=-1;
else s1=1;
if (y2 < y1) s2=-1;
else s2=1;
if(delta_y > delta_x){
temp = delta_x;
delta_x = delta_y;
delta_y = temp;
troca = 1;
}
else{
troca = 0;
}
new_e = 2*delta_y - delta_x;
for(int i=1; i<= delta_x; i++){
glVertex2i(x,y);
while (new_e >= 0){
if(troca == 1){
x = x + s1;
}
else{
y = y + s2;
}
new_e = new_e - 2*delta_x;
}
if(troca == 1){
y = y + s2;
}
else{
x = x + s1;
}
new_e= new_e + 2*delta_y;
}
glEnd();
glFlush();
}
-
Resultado
2.3. Algoritmo de bresenham para circunferência
-
Código fonte: circle.c
#include <GL/glut.h>
#include <stdlib.h>
void init(void);
void pontosDaCircunferencia(int, int);
void display(void);
int x0=128, y0=128, raio=50, d, x, y;
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Desenhando uma circunferência");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(0.0, 0.0, 0.0, 1.0);
glOrtho (0, 256, 0, 256, -1 ,1);
}
void pontosDaCircunferencia(int x,int y){
glVertex2i(x+x0,y+y0);
glVertex2i(y+x0,x+y0);
glVertex2i(-x+x0,y+y0);
glVertex2i(-y+x0,x+y0);
glVertex2i(-x+x0,-y+y0);
glVertex2i(-y+x0,-x+y0);
glVertex2i(x+x0,-y+y0);
glVertex2i(y+x0,-x+y0);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_POINTS);
x = 0;
y = raio;
d = 1 - y;
pontosDaCircunferencia(x,y);
while(y > x){
if(d < 0){
d = d + 2*x + 3;
x = x + 1;
}
else{
d = d + 2*(x-y) + 5;
x = x + 1;
y = y - 1;
}
pontosDaCircunferencia(x,y);
}
glEnd();
glFlush();
}
-
Resultado
3. Preenchimento de regiões
3.1. selecao
-
Código fonte: selecao.c
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
GLfloat rL[4], gL[4], bL[4], rF[4], gF[4], bF[4];
GLint x[4], y[4];
bool flagF;
void init(void);
void display(void);
void defineColor(GLint, GLint);
void draw();
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Quadradinhos");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho (0, 256, 0, 256, -1 ,1);
for (int i=0; i<4; i++){
rL[i]=0.0; gL[i]=0.0; bL[i]=0.0;
rF[i]=1.0; gF[i]=1.0; bF[i]=0.0;
}
x[0]=30; x[1]=143; x[2]=30; x[3]=143;
y[0]=226; y[1]=226; y[2]=113; y[3]=113;
flagF=true;
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_POLYGON_STIPPLE);
draw();
glFlush();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y){
switch (key) {
case 'b':
flagF=false;
break;
case 'f':
flagF=true;
break;
}
}
void mouse(int button, int state, int px, int py){
switch (button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
defineColor(px,(py*-1)+256);
}
break;
}
}
void draw(){
for (int i=0; i<4; i++){
glPolygonMode(GL_BACK, GL_FILL);
glColor3f(rF[i], gF[i], bF[i]);
glBegin(GL_POLYGON);
glVertex2i(x[i],y[i]); glVertex2i(x[i]+83,y[i]);
glVertex2i(x[i]+83,y[i]-83); glVertex2i(x[i],y[i]-83);
glEnd();
glPolygonMode(GL_BACK, GL_LINE);
glColor3f(rL[i], gL[i], bL[i]);
glBegin(GL_POLYGON);
glVertex2i(x[i],y[i]); glVertex2i(x[i]+83,y[i]);
glVertex2i(x[i]+83,y[i]-83); glVertex2i(x[i],y[i]-83);
glEnd();
}
}
void defineColor(GLint px, GLint py){
for (int i=0; i<4; i++){
if (px>=x[i] && px<=x[i]+83 && py>=y[i]-83 && py<=y[i]){
if (flagF==true){
rF[i]=(GLfloat)rand()/(RAND_MAX+1.0);
gF[i]=(GLfloat)rand()/(RAND_MAX+1.0);
bF[i]=(GLfloat)rand()/(RAND_MAX+1.0);
glutPostRedisplay();
}
else{
rL[i]=(GLfloat)rand()/(RAND_MAX+1.0);
gL[i]=(GLfloat)rand()/(RAND_MAX+1.0);
bL[i]=(GLfloat)rand()/(RAND_MAX+1.0);
glutPostRedisplay();
}
}
}
}
-
Resultado
3.2. selecaoSingleBuffer
-
Código fonte: selecao.c
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
GLfloat rL[4], gL[4], bL[4], rF[4], gF[4], bF[4];
GLint x[4], y[4];
bool flagF;
void init(void);
void display(void);
void defineColor(GLint, GLint);
void draw();
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (256, 256);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Quadradinhos");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho (0, 256, 0, 256, -1 ,1);
for (int i=0; i<4; i++){
rL[i]=0.0; gL[i]=0.0; bL[i]=0.0;
rF[i]=1.0; gF[i]=1.0; bF[i]=0.0;
}
x[0]=30; x[1]=143; x[2]=30; x[3]=143;
y[0]=226; y[1]=226; y[2]=113; y[3]=113;
flagF=true;
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_POLYGON_STIPPLE);
draw();
glFlush();
}
void keyboard(unsigned char key, int x, int y){
switch (key) {
case 'b':
flagF=false;
break;
case 'f':
flagF=true;
break;
}
}
void mouse(int button, int state, int px, int py){
switch (button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
defineColor(px,(py*-1)+256);
}
break;
}
}
void draw(){
for (int i=0; i<4; i++){
glPolygonMode(GL_BACK, GL_FILL);
glColor3f(rF[i], gF[i], bF[i]);
glBegin(GL_POLYGON);
glVertex2i(x[i],y[i]); glVertex2i(x[i]+83,y[i]);
glVertex2i(x[i]+83,y[i]-83); glVertex2i(x[i],y[i]-83);
glEnd();
glPolygonMode(GL_BACK, GL_LINE);
glColor3f(rL[i], gL[i], bL[i]);
glBegin(GL_POLYGON);
glVertex2i(x[i],y[i]); glVertex2i(x[i]+83,y[i]);
glVertex2i(x[i]+83,y[i]-83); glVertex2i(x[i],y[i]-83);
glEnd();
}
}
void defineColor(GLint px, GLint py){
for (int i=0; i<4; i++){
if (px>=x[i] && px<=x[i]+83 && py>=y[i]-83 && py<=y[i]){
if (flagF==true){
rF[i]=(GLfloat)rand()/(RAND_MAX+1.0);
gF[i]=(GLfloat)rand()/(RAND_MAX+1.0);
bF[i]=(GLfloat)rand()/(RAND_MAX+1.0);
glutPostRedisplay();
}
else{
rL[i]=(GLfloat)rand()/(RAND_MAX+1.0);
gL[i]=(GLfloat)rand()/(RAND_MAX+1.0);
bL[i]=(GLfloat)rand()/(RAND_MAX+1.0);
glutPostRedisplay();
}
}
}
}
-
Resultado
Não ocorreram mudanças em relação ao código anterior.
3.3. padrao
-
Código fonte: padrao.c
#include <GL/glut.h>
#include <stdlib.h>
GLubyte tux[] = {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x08, 0x00,
0x00, 0x80, 0x0F, 0xF8,
0x00, 0x80, 0x08, 0x08,
0x00, 0x80, 0x08, 0x08,
0x00, 0x80, 0x08, 0x08,
0x00, 0x80, 0x08, 0x08,
0x3F, 0xFE, 0x0F, 0xF8,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
GLfloat r,g,b;
void init(void);
void display(void);
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (300, 300);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Padrão");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void){
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho (0, 300, 0, 300, -1 ,1);
r=0; g=0; b=1;
}
void display(void){
int i;
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_POLYGON_STIPPLE);
glPolygonMode(GL_BACK, GL_FILL);
glColor3f(r, g, b);
glBegin(GL_POLYGON);
glVertex2i(50,150); glVertex2i(50+200/3,250);
glVertex2i(50+2*200/3,250); glVertex2i(250,150);
glVertex2i(50+2*200/3,50); glVertex2i(50+200/3,50);
glEnd();
glEnable(GL_POLYGON_STIPPLE);
glColor3f(1.0, 0.0, 1.0);
glPolygonStipple(tux);
glBegin(GL_POLYGON);
glVertex2i(50,150); glVertex2i(50+200/3,50);
glVertex2i(50+2*200/3,50); glVertex2i(250,150);
glVertex2i(50+2*200/3,250); glVertex2i(50+200/3,250);
glEnd();
glFlush();
glutSwapBuffers();
}
-
Resultado
4. Transformações
4.1. braco_garra
-
Código fonte: braco_garra.c
#include <GL/glut.h>
#include <stdlib.h>
static int shoulder = 0, elbow = 0, thumb = 0, index = 0, middle = 0;
void init(void){
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void display(void){
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
/* origem posicionada no ombro */
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
/* origem posicionada no centro do braço */
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
/* origem posicionada no cotovelo */
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
/* Thumb */
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glTranslatef (0.0, -0.2, 0.0);
glRotatef ((GLfloat) thumb, 0.0, 0.0, 1.0);
glTranslatef (0.25, 0.075, 0.125);
glScalef (0.5, 0.15, 0.25);
glutWireCube (1.0);
glPopMatrix();
/* Index */
glPushMatrix();
glTranslatef (0.0, 0.2, 0.5);
glRotatef ((GLfloat) index, 0.0, 0.0, 1.0);
glTranslatef (0.25, -0.075, -0.125);
glScalef (0.5, 0.15, 0.25);
glutWireCube (1.0);
glPopMatrix();
/* Middle */
glTranslatef (0.0, 0.2, -0.5);
glRotatef ((GLfloat) middle, 0.0, 0.0, 1.0);
glTranslatef (0.25, -0.075, 0.125);
glScalef (0.5, 0.15, 0.25);
glutWireCube (1.0);
glPopMatrix();
/* origem volta para o sistema de coordenadas original */
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -10.0);
}
void keyboard (unsigned char key, int x, int y){
switch (key) {
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case 'S':
shoulder = (shoulder - 5) % 360;
glutPostRedisplay();
break;
case 'e':
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case 'E':
elbow = (elbow - 5) % 360;
glutPostRedisplay();
break;
case 'p':
thumb = (thumb + 5) % 360;
glutPostRedisplay();
break;
case 'P':
thumb = (thumb - 5) % 360;
glutPostRedisplay();
break;
case 'i':
index = (index + 5) % 360;
glutPostRedisplay();
break;
case 'I':
index = (index - 5) % 360;
glutPostRedisplay();
break;
case 'm':
middle = (middle + 5) % 360;
glutPostRedisplay();
break;
case 'M':
middle = (middle - 5) % 360;
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 (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
-
Resultado
4.2. braco_garra_3d
-
Código fonte: braco_garra_3d.c
#include <GL/glut.h>
#include <stdlib.h>
static int shoulder = 0, elbow = 0, thumb = 0, index = 0, middle = 0, base = 0;
void init(void){
glClearColor (0.0, 0.0, 0.0, 0.0);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
void display(void){
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
/* origem posicionada no ombro */
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) base, 0.0, 1.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
/* origem posicionada no centro do braço */
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glColor3f(1,0,0);
glutSolidCube(1.0);
glPopMatrix();
/* origem posicionada no cotovelo */
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glColor3f(0,1,0);
glutSolidCube(1.0);
glPopMatrix();
/* Thumb */
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glTranslatef (0.0, -0.2, 0.0);
glRotatef ((GLfloat) thumb, 0.0, 0.0, 1.0);
glTranslatef (0.25, 0.075, 0);
glScalef (0.5, 0.15, 0.25);
glColor3f(1,1,0);
glutSolidCube(1.0);
glPopMatrix();
/* Index */
glPushMatrix();
glTranslatef (0.0, 0.2, 0.5);
glRotatef ((GLfloat) index, 0.0, 0.0, 1.0);
glTranslatef (0.25, -0.075, -0.125);
glScalef (0.5, 0.15, 0.25);
glColor3f(0,0,1);
glutSolidCube(1.0);
glPopMatrix();
/* Middle */
glTranslatef (0.0, 0.2, -0.5);
glRotatef ((GLfloat) middle, 0.0, 0.0, 1.0);
glTranslatef (0.25, -0.075, 0.125);
glScalef (0.5, 0.15, 0.25);
glColor3f(0,1,1);
glutSolidCube(1.0);
glPopMatrix();
/* origem volta para o sistema de coordenadas original */
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -10.0);
}
void keyboard (unsigned char key, int x, int y){
switch (key) {
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case 'S':
shoulder = (shoulder - 5) % 360;
glutPostRedisplay();
break;
case 'e':
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case 'E':
elbow = (elbow - 5) % 360;
glutPostRedisplay();
break;
case 'p':
thumb = (thumb + 5) % 360;
glutPostRedisplay();
break;
case 'P':
thumb = (thumb - 5) % 360;
glutPostRedisplay();
break;
case 'i':
index = (index + 5) % 360;
glutPostRedisplay();
break;
case 'I':
index = (index - 5) % 360;
glutPostRedisplay();
break;
case 'm':
middle = (middle + 5) % 360;
glutPostRedisplay();
break;
case 'M':
middle = (middle - 5) % 360;
glutPostRedisplay();
break;
case 'b':
base = (base + 5) % 360;
glutPostRedisplay();
break;
case 'B':
base = (base - 5) % 360;
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 (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
-
Resultado