Enviado em 24/04/2021 - 19:33h
Boa noite a todos.//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <GL/glut.h>
#include <math.h>
int n_stars;
GLdouble theta, wTamPad = 10, wCentX, wCentY, Wh = 10;
// GLdouble theta, wTamPad = 10, wCentX = 50, wCentY = 200, Wh = 10;
struct Ponto {
float x;
float y;
};
struct Ponto v[6];
static void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 400, 0, 400);
}
//Gera cor aleatoria para cada componente da função glColor3f
float gerar_cores() {
float cor_f;
int gerado;
srand(time(NULL));
gerado = rand() % 101;
cor_f = (float)gerado/100;
return cor_f;
}
void drawStars(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// for(int i = 0; i < n_stars; i++) {
//glColor3f(1.0, 0.5, 0.8);
glColor3f(0.5, gerar_cores(), 1.0);
GLint k;
theta = (6.2831853 / 6);
for (k = 0; k < 6; k++) {
v[k].x = wCentX + cos(theta * k) * wTamPad;
v[k].y = wCentY + sin(theta * k) * wTamPad;
};
float d = (v[1].x - v[2].x)/2;
float dp = sqrt(pow(d,2)+pow(Wh,2));
glBegin(GL_POLYGON);
glVertex2f(v[0].x,v[0].y);
glVertex2f(v[1].x + dp, v[1].y);
glVertex2f(v[1].x, v[1].y);
glVertex2f(v[1].x, v[1].y);
glVertex2f(wCentX, v[1].y+Wh);
glVertex2f(v[2].x, v[2].y);
glVertex2f(v[2].x, v[2].y);
glVertex2f(v[2].x - dp, v[2].y);
glVertex2f(v[3].x, v[3].y);
glVertex2f(v[3].x, v[3].y);
glVertex2f(v[2].x - dp, v[4].y);
glVertex2f(v[4].x, v[4].y);
//glVertex2f(v[4].x, v[4].y);
//glVertex2f(wCentX, v[4].y - Wh);
//glVertex2f(v[5].x, v[5].y);
//
glVertex2f(v[5].x, v[5].y);
glVertex2f(v[5].x + dp, v[5].y);
glVertex2f(v[0].x, v[0].y);
glEnd();
glFlush();
// }
}
// void display() {
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// for (int i = 0; i < n_stars; i++)
// {
// drawStars();
// }
// }
// Programa Principal
int main(int argc, char** argv)
{
printf( "n = " );
scanf( "%d", &n_stars );
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1280, 780);
glutCreateWindow("Draw Stars");
init();
// ---------------------------------------------------------------
for (int i = 0; i < n_stars; i++)
{
printf( "xc = " );
scanf( "%le", &wCentX );
printf( "yc = " );
scanf( "%le", &wCentY );
glutDisplayFunc(drawStars);
}
// ---------------------------------------------------------------
// glutDisplayFunc(drawStars);
glutMainLoop();
return 0;
}