Skip to content

Instantly share code, notes, and snippets.

@carolinapardo
Created April 21, 2013 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carolinapardo/5430828 to your computer and use it in GitHub Desktop.
Save carolinapardo/5430828 to your computer and use it in GitHub Desktop.
Representación solar: En este proyecto se quiere representar el comportamiento de la parte externa del sol, tema que ha sido previamente investigado por medio del proyecto. Este proyecto fue realizado mediante el uso de programación en c++ con ayuda de la librería opengl.
//creadores gerardo tibamoso pedraza, carolina pardo y leonel felipe morales avella.
#include <math.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;
#define PI 3.1415
int day, year;
float puntos[100][100][3];
float ptosElipses[100][100][3];
float dx=0.0;
float theta, alpha;
float c,d;
GLfloat mat_ambient[]= { 0.0, 0.0, 0.0, 1.0 };
void elipses(float r, int lat, int lon )
{
int k=0, latCir;
float dx, rCir;
dx=r/2.0;
theta=PI/lon;
latCir=lat/4;
rCir=r/latCir;
for(int i=0; i<=lat; i++){
for(int j=0; j<=lon; j++){
if(i==0){
ptosElipses[i][j][0]=0.0;
ptosElipses[i][j][1]=0.0;
}
else if(i<latCir){
ptosElipses[i][j][0]=(rCir*i)*cos(theta*j-PI/2);
ptosElipses[i][j][1]=(rCir*i)*sin(theta*j-PI/2);
}else{
ptosElipses[i][j][0]=(r+dx*(i-latCir))*cos(theta*j-PI/2);
ptosElipses[i][j][1]=r*sin(theta*j-PI/2);
}
}
}
// glColor3f(((float)i/(float)lat+(float)j/(float)lon)/2,
// ((float)i/(float)lat+(float)j/(float)lon)/2,
// ((float)i/(float)lat+(float)j/(float)lon)/2);
for(int i=0; i<lat; i++){
glColor3f(1-(float)i/(float)lat,
1-(float)i/(float)lat,
0.0);
for(int j=0; j<lon; j++){
glBegin(GL_QUADS);
glVertex3f(ptosElipses[i][j][0],ptosElipses[i][j][1],ptosElipses[i][j][2]);
glVertex3f(ptosElipses[i+1][j][0],ptosElipses[i+1][j][1],ptosElipses[i+1][j][2]);
glVertex3f(ptosElipses[i+1][j+1][0],ptosElipses[i+1][j+1][1],ptosElipses[i+1][j+1][2]);
glVertex3f(ptosElipses[i][j+1][0],ptosElipses[i][j+1][1],ptosElipses[i][j+1][2]);
glEnd();
}
}
for(int i=0; i<=latCir; i++){
for(int j=0; j<=lon; j++){
if(i==0){
ptosElipses[i][j][0]=0.0;
ptosElipses[i][j][1]=0.0;
}
else{
ptosElipses[i][j][0]=(rCir*i)*cos(theta*j+PI/2);
ptosElipses[i][j][1]=(rCir*i)*sin(theta*j+PI/2);
}
}
}
for(int i=0; i<latCir; i++){
glColor3f(1-(float)i/(float)lat,
1-(float)i/(float)lat,
0.0);
for(int j=0; j<lon; j++){
glBegin(GL_QUADS);
glVertex3f(ptosElipses[i][j][0],ptosElipses[i][j][1],ptosElipses[i][j][2]);
glVertex3f(ptosElipses[i+1][j][0],ptosElipses[i+1][j][1],ptosElipses[i+1][j][2]);
glVertex3f(ptosElipses[i+1][j+1][0],ptosElipses[i+1][j+1][1],ptosElipses[i+1][j+1][2]);
glVertex3f(ptosElipses[i][j+1][0],ptosElipses[i][j+1][1],ptosElipses[i][j+1][2]);
glEnd();
}
}
return;
}
void esfera(float r, int lat, int lon)
{
theta = PI/lat;
alpha = 2*PI/lon;
for(int i=0; i<=lat; i++){
for(int j=0; j<=lon; j++){
puntos[i][j][0]=r*cos(theta*i+PI/2)*cos(alpha*j);
puntos[i][j][1]=r*sin(theta*i+PI/2);
puntos[i][j][2]=-r*cos(theta*i+PI/2)*sin(alpha*j);
}
}
for(int i=0; i<lat; i++){
for(int j=0; j<lon; j++){
/* glColor3f(((float)i/(float)lat+(float)j/(float)lon)/2,
((float)i/(float)lat+(float)j/(float)lon)/2,
((float)i/(float)lat+(float)j/(float)lon)/2);*/
glBegin(GL_QUADS);
/*glColor3f(1,0,0);*/
c = fabs(2.f*i/lat);
d = fabs(1.3f*(j-lon/2)/lon);
glColor3f(c, d, 0.0);
//glColor3f(((float)i/(float)lat+(float)j/(float)lon),0.6,0.6);
glVertex3f(puntos[i][j][0],puntos[i][j][1],puntos[i][j][2]);
glVertex3f(puntos[i+1][j][0],puntos[i+1][j][1],puntos[i+1][j][2]);
glVertex3f(puntos[i+1][j+1][0],puntos[i+1][j+1][1],puntos[i+1][j+1][2]);
glVertex3f(puntos[i][j+1][0],puntos[i][j+1][1],puntos[i][j+1][2]);
glEnd();
}
}
return;
}
void display(void)
{
float ang, angGrad;
int numArc=10;
ang=2*PI/numArc;
angGrad=ang*180/PI;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
//glOrtho(1.0, -1.0, 1.0, -1.0, 1.0, -1.0);
esfera(1.0, 20, 20);
// en este ciclo se generan las elipses de mayor tamaño alrededor de la esfera
for(int i=0; i<numArc; i++){
glPushMatrix();
glRotatef(angGrad*i,0,0,1);
glTranslatef(1.0, 0.0, 0.0);
elipses(0.2, 10, 20);
glPopMatrix();
}
// en este ciclo se generan las elipses de menor tamaño alrededor de la esfera
for(int i=0; i<numArc; i++){
glPushMatrix();
glRotatef(angGrad*i+(angGrad/2),0,0,1);
glTranslatef(1.0, 0.0, 0.0);
elipses(0.1, 10, 20);
glPopMatrix();
}
glPopMatrix();
glRotatef(90,1,0,0);
glutWireSphere(1.9,50,50);
glutSwapBuffers();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'x':
exit(0);
break;
default:
break;
}
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//glOrtho(1.0, -1.0, 1.0, -1.0, 1.0, -1.0);
gluPerspective(60.0, 1.0, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
//glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment