A continuación dejare el código en lenguaje C, y el ejecutable de la aplicación para que puedan ver por si mismo el trabajo realizado
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
float posx = 0.0;
float posx1 = 0.0;
int angulo = 1;
void IniciarGLUT() {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000,600);
glutInitWindowPosition(100,100);
glutCreateWindow("TALLER 1");
glClearColor(0., 1., 1., 1.);
glColor3f(0., 0., 0.);
glEnable(GL_DEPTH_TEST);
}
void circulo(float x, float y, int radio){
glBegin(GL_TRIANGLE_FAN);
glColor3f(0., 0., 0.);
glVertex3f(x, y, 1);
for (angulo=0; angulo<360; angulo+=5){
glVertex3f(x+sin(angulo)*radio, y+cos(angulo)*radio, 1);
}
glEnd();
}
void Auto(int x, int y){
glBegin(GL_POLYGON);
glColor3f(0., 0., 0.);
glVertex3f(4.+posx, -3., 1.);
glVertex3f(0.+posx, -3., 1.);
glVertex3f(-5.+posx, -5., 1.);
glVertex3f(-5.+posx, -6., 1.);
glVertex3f(4.+posx, -6., 1.);
glEnd();
glBegin(GL_QUADS);
glColor3f(0., 0., 0.);
glVertex3f(9.+posx, -3., 1.);
glVertex3f(5.+posx, -3., 1.);
glVertex3f(5.+posx, -6., 1.);
glVertex3f(13.+posx, -6., 1.);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0., 0., 0.);
glVertex3f(4.5+posx, -2.5, 1.);
glVertex3f(4.5+posx, -10, 1.);
glVertex3f(-5.5+posx, -10, 1.);
glVertex3f(-6.+posx, -5., 1);
glVertex3f(0.+posx, -2.5, 1);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0., 0., 0.);
glVertex3f(9.5+posx, -2.5, 1.);
glVertex3f(14.5+posx, -6, 1.);
glVertex3f(14.5+posx, -8, 1.);
glVertex3f(12+posx, -7.6, 1);
glVertex3f(10+posx, -8., 1);
glVertex3f(9+posx, -9., 1);
glVertex3f(8+posx, -10, 1);
glVertex3f(4.5+posx, -10, 1);
glVertex3f(4.5+posx, -2.5, 1);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0., 0., 0.);
glVertex3f(10.+posx, -2., 0.5);
glVertex3f(0.+posx, -2., 0.5);
glVertex3f(-6.8+posx, -4.7, 0.5);
glVertex3f(-15.+posx, -6., 0.5);
glVertex3f(-15.+posx, -11., 0.5);
glVertex3f(19.+posx, -11., 0.5);
glVertex3f(19.+posx, -6., 0.5);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1., 0., 0.);
glVertex3f(10.+posx, -2., 0.5);
glVertex3f(0.+posx, -2., 0.5);
glVertex3f(-10.+posx, -6., 0.5);
glVertex3f(-15.+posx, -6., 0.5);
glVertex3f(-15.+posx, -11., 0.5);
glVertex3f(-10.+posx, -11., 0.5);
glVertex3f(10.+posx, -11., 0.5);
glVertex3f(19.+posx, -11., 0.5);
glVertex3f(19.+posx, -6., 0.5);
glVertex3f(16.+posx, -6., 0.5);
glEnd();
circulo(-9+posx, -11, 3.8);
circulo(12+posx, -11, 3.8);
}
void arbol(float x, float y, float z){
glBegin(GL_QUADS);
glColor3ub(150., 0., 0);
glVertex3f(-18+posx1+x, -5+y, 0.1);
glVertex3f(-19+posx1+x, -5+y, 0.1);
glVertex3f(-19+posx1+x, -9+y, 0.1);
glVertex3f(-18+posx1+x, -9+y, 0.1);
glEnd();
glBegin(GL_QUADS);
glColor3ub(0., 100, 0);
glVertex3f(-17+posx1+x, -4+y, 0.1);
glVertex3f(-20+posx1+x, -4+y, 0.1);
glVertex3f(-20+posx1+x, -5+y, 0.1);
glVertex3f(-17+posx1+x, -5+y, 0.1);
glEnd();
glBegin(GL_QUADS);
glColor3ub(0., 100, 0);
glVertex3f(-16+posx1+x, -3+y, 0.1);
glVertex3f(-21+posx1+x, -3+y, 0.1);
glVertex3f(-21+posx1+x, -4+y, 0.1);
glVertex3f(-16+posx1+x, -4+y, 0.1);
glEnd();
glBegin(GL_QUADS);
glColor3ub(0., 100, 0);
glVertex3f(-15+posx1+x, -2+y, 0.1);
glVertex3f(-22+posx1+x, -2+y, 0.1);
glVertex3f(-22+posx1+x, -3+y, 0.1);
glVertex3f(-15+posx1+x, -3+y, 0.1);
glEnd();
glBegin(GL_QUADS);
glColor3ub(0., 100, 0);
glVertex3f(-16+posx1+x, -1+y, 0.1);
glVertex3f(-21+posx1+x, -1+y, 0.1);
glVertex3f(-21+posx1+x, -2+y, 0.1);
glVertex3f(-16+posx1+x, -2+y, 0.1);
glEnd();
glBegin(GL_QUADS);
glColor3ub(0., 100, 0);
glVertex3f(-17+posx1+x, 0+y, 0.1);
glVertex3f(-20+posx1+x, 0+y, 0.1);
glVertex3f(-20+posx1+x, -1+y, 0.1);
glVertex3f(-17+posx1+x, -1+y, 0.1);
glEnd();
}
void fondo(int x, int y){
arbol(-45, 0, 0);
arbol(-30, 0, 0);
arbol(-15, 0, 0);
arbol(0, 0 , 0);
arbol(15, 0, 0);
arbol(30, 0, 0);
arbol(45, 0, 0);
arbol(60, 0, 0);
arbol(75, 0, 0);
glBegin(GL_QUADS);
glColor3f(0., 1., 0.);
glVertex3f(35, -7, 0.);
glVertex3f(-40, -7, 0.);
glVertex3f(-40, -25, 0.);
glVertex3f(35, -25, 0.);
glEnd();
}
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Auto(10+posx, 0);
fondo(0, 0);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void ControlFlechas(int key, int x, int y){
switch (key)
{
case GLUT_KEY_LEFT:
posx=posx-0.2;
posx1=posx1+0.3;
if(posx<=-40) posx=35;
if(posx1>=45) posx1=-40;
break;
case GLUT_KEY_RIGHT:
posx=posx+0.2;
posx1=posx1-0.3;
if(posx>=35) posx=-40;
if(posx1<=-50) posx1=45;
break;
}
glutPostRedisplay();
}
void Reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45., (GLfloat) w / h, 1., 50.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, -6, 30, 0, 0, -100, 0, 1, 0);
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
IniciarGLUT();
glutReshapeFunc(Reshape);
glutDisplayFunc(Display);
glutSpecialFunc(ControlFlechas);
glutMainLoop();
return 0;
}
Auto 2D
Y eso es todo, cualquier duda mandenme un mensaje o comenten, esto esta hecho en Dev-C++ y recuerden que no funcionara a menos que descargen e instalen las librerias de opengl y glut
Y eso es todo, cualquier duda mandenme un mensaje o comenten, esto esta hecho en Dev-C++ y recuerden que no funcionara a menos que descargen e instalen las librerias de opengl y glut

No hay comentarios:
Publicar un comentario