#include "stozac.h"
struct stozac_t* stozac_inicijaliziraj(float r, float h, int nn){
struct stozac_t *s;
s = (struct stozac_t *)calloc(1, sizeof(struct stozac_t));
assert(s != NULL);
s->n = nn;
s->h = h;
s->r = r;
s->vrhovi_baza_len = 3 * (s->n + 2);
s->normale_baza_len = s->vrhovi_baza_len;
s->vrhovi_baza = (GLfloat *)calloc(s->vrhovi_baza_len, sizeof(GLfloat));
assert(s->vrhovi_baza != NULL);
s->normale_baza = (GLfloat *)calloc(s->normale_baza_len,
sizeof(GL_FLOAT));
assert(s->normale_baza != NULL);
s->indeksi_baza_len = s->n + 2;
s->indeksi_baza = (GLuint *)calloc(s->indeksi_baza_len, sizeof(GLuint));
assert(s->indeksi_baza != NULL);
s->vrhovi_plast_len = 2 * 3 * (s->n + 1);
s->normale_plast_len = s->vrhovi_plast_len;
s->vrhovi_plast = (GLfloat *)calloc(s->vrhovi_plast_len, sizeof(GLfloat));
assert(s->vrhovi_plast != NULL);
s->normale_plast = (GLfloat *)calloc(s->normale_plast_len,
sizeof(GLfloat));
assert(s->normale_plast != NULL);
s->indeksi_plast_len = 2 * (s->n + 1);
s->indeksi_plast = (GLuint *)calloc(s->indeksi_plast_len, sizeof(GLuint));
assert(s->indeksi_plast != NULL);
int v = 0;
int n = 0;
s->normale_baza[n++] = 0.0f;
s->normale_baza[n++] = 0.0f;
s->normale_baza[n++] = -1.0f;
s->vrhovi_baza[v++] = 0.0f;
s->vrhovi_baza[v++] = 0.0f;
s->vrhovi_baza[v++] = 0.0f;
double t = 0.0;
for(int i = 0; i < (s->n + 1); ++i){
s->normale_baza[n++] = 0.0f;
s->normale_baza[n++] = 0.0f;
s->normale_baza[n++] = -1.0f;
s->vrhovi_baza[v++] = s->r * ((float)cos(t));
s->vrhovi_baza[v++] = s->r * ((float)sin(t));
s->vrhovi_baza[v++] = 0.0f;
t += 2.0 * M_PI / ((double) s->n);
}
for(int i = 0; i < s->indeksi_baza_len; ++i){
s->indeksi_baza[i] = i;
}
v = 0;
n = 0;
t = 2.0 * M_PI;
float tmp[] = {0.0f, 0.0f, 0.0f};
for(int i = 0; i < (s->n + 1); ++i){
vprodukt(tmp,
s->r * ((float)cos(t)), s->r * ((float)sin(t)), -1.0f * s->h,
(float)cos(t + M_PI * 2.0), (float)sin(t + M_PI * 2.0), 0.0f);
normiraj(tmp);
s->normale_plast[n++] = tmp[0];
s->normale_plast[n++] = tmp[1];
s->normale_plast[n++] = tmp[2];
s->vrhovi_plast[v++] = s->r * ((float)cos(t));
s->vrhovi_plast[v++] = s->r * ((float)sin(t));
s->vrhovi_plast[v++] = 0.0f;
t -= M_PI / ((double) s->n);
vprodukt(tmp,
s->r * ((float)cos(t)), s->r * ((float)sin(t)), -1.0f * s->h,
(float)cos(t + M_PI * 2.0), (float)sin(t + M_PI * 2.0), 0.0f);
normiraj(tmp);
s->normale_plast[n++] = tmp[0];
s->normale_plast[n++] = tmp[1];
s->normale_plast[n++] = tmp[2];
s->vrhovi_plast[v++] = 0.0f;
s->vrhovi_plast[v++] = 0.0f;
s->vrhovi_plast[v++] = s->h;
t -= M_PI / ((double) s->n);
}
for(int i = 0; i < s->indeksi_plast_len; ++i){
s->indeksi_plast[i] = i;
}
return s;
}
void stozac_crtaj(struct stozac_t* s){
assert(s != NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, s->vrhovi_baza);
glNormalPointer(GL_FLOAT, 0, s->normale_baza);
glDrawElements(GL_TRIANGLE_FAN, s->indeksi_baza_len, GL_UNSIGNED_INT,
s->indeksi_baza);
glVertexPointer(3, GL_FLOAT, 0, s->vrhovi_plast);
glNormalPointer(GL_FLOAT, 0, s->normale_plast);
glDrawElements(GL_TRIANGLE_STRIP, s->indeksi_plast_len, GL_UNSIGNED_INT,
s->indeksi_plast);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
void stozac_oslobodi(struct stozac_t* s){
free(s->vrhovi_baza);
free(s->normale_baza);
free(s->indeksi_baza);
free(s->vrhovi_plast);
free(s->normale_plast);
free(s->indeksi_plast);
}
void normiraj(float *v){
float d = (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] /= d;
v[1] /= d;
v[2] /= d;
}
void vprodukt(float *v, float a0, float a1, float a2, float b0, float b1,
float b2){
v[0] = a1 * b2 - a2 * b1;
v[1] = a2 * b0 - a0 * b2;
v[2] = a0 * b1 - a1 * b0;
}