Hi,
Here’s the video of my problem:
As you can see, the lightning effects aren’t fading completely, but bouncing from one state to another.
Here’s my full code:
//
// main.c
// MAMAN15Q5
//
// Created by Vitali Pom on 6/6/14.
// Copyright (c) 2014 Vitali Pom. All rights reserved.
//
#include <stdio.h>
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#include "simclist.h"
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define KEY_ESCAPE 27
GLfloat CLIP_HEIGHT = 50;
GLfloat CLIP_WIDTH = 50;
typedef struct {
int width;
int height;
int aspectRatio;
char* title;
} glutWindow;
int DELAY = 30;
GLfloat rotate_by=1;
glutWindow win;
list_t polygons;
size_t szelem(const void *el) {
return sizeof(list_t);
}
size_t szelem2(const void *el) {
return sizeof(GLfloat [3]);
}
void * list_get_last(const list_t *restrict l){
return list_get_at(l, list_size(l)-1);
}
void addPoint(list_t *polygon, GLfloat (*point)[3]){
list_append(polygon, point);
}
void addPolygon(int size, GLfloat (*vertex)[3], ...){
list_t polygon1;
list_init(&polygon1);
list_attributes_copy(&polygon1, szelem2, 1);
va_list args;
va_start(args, vertex);
addPoint(&polygon1, vertex);
for(int i=0;i<size-1; i++){
GLfloat (*p)[3];
p=va_arg(args, GLfloat (*)[3]);
addPoint(&polygon1, p);
}
list_append(&polygons, &polygon1);
}
void * normal_vec(GLfloat *p1, GLfloat *p2, GLfloat *p3){
GLfloat *normal_v=malloc(3*sizeof(GLfloat));
//for(int i =0; i<3;i++){
//*(normal_v+i) = (*(p1+i)- *(p2+i)) * (*(p3+i)-*(p2+i));
//}
*(normal_v+0) = ((*(p1+1)- *(p2+1))*(*(p3+2)-*(p2+2)))-((*(p1+2)- *(p2+2))*(*(p3+1)-*(p2+1)));
*(normal_v+1) = -(((*(p1+0)- *(p2+0))*(*(p3+2)-*(p2+2)))-((*(p1+2)- *(p2+2))*(*(p3+0)-*(p2+0))));
*(normal_v+2) = ((*(p1+0)- *(p2+0))*(*(p3+1)-*(p2+1)))-((*(p1+1)- *(p2+1))*(*(p3+0)-*(p2+0)));
GLfloat magnitude = sqrt((*(normal_v+0) * *(normal_v+0)) + (*(normal_v+1) * *(normal_v+1)) + (*(normal_v+2) * *(normal_v+2)));
*(normal_v+0) /= magnitude;
*(normal_v+1) /= magnitude;
*(normal_v+2) /= magnitude;
return normal_v;
}
void * sub_vec(GLfloat *p1, GLfloat *p2){
GLfloat *sub_v = malloc(3*sizeof(GLfloat));
for(int i =0; i<3;i++){
sub_v[i] = (*(p1+i)- *(p2+i));
}
return sub_v;
}
GLfloat dot_vec(GLfloat *p1, GLfloat *p2){
float result =0;
for(int i =0;i<3;i++){
GLfloat v1 = *(p1+i);
//printf("V1:%f ",v1);
GLfloat v2 = *(p2+i);
//printf("V2:%f",v2);
result += v1*v2;
//printf("result:%f ",result);
//printf("
");
}
return result;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT );
GLfloat view_from[3] = {7,7, -5};
GLfloat view_at[3] = { 7, 7,18};
gluLookAt(view_from[0], view_from[1], view_from[2], view_at[0], view_at[1] , view_at[2], 0, 1, 0);
glClearColor(0.0, 0.0, 0.0, 1.0);
//glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float var =0.0;
GLfloat *mysub = sub_vec(&view_at, &view_from); //good
//printf("(%f,%f,%f)
",*(mysub+0), *(mysub+1), *(mysub+2));
for(int i =0; i<list_size(&polygons);i++){
list_t *polygon = list_get_at(&polygons, i);
GLfloat (*pol_point0)= list_get_at(polygon, 0);
GLfloat (*pol_point1) = list_get_at(polygon, 2);
GLfloat (*pol_point2) = list_get_at(polygon, 3);
GLfloat (*mynorm) = normal_vec(pol_point0, pol_point1, pol_point2); //good
//printf("(%f,%f,%f)
",*(mynorm+0), *(mynorm+1),*(mynorm+2)); //good
//printf("(%f,%f,%f)
",*(mynorm+0), *(mynorm+1),*(mynorm+2)); //good
GLfloat mydot = dot_vec(mynorm,mysub);
//printf("dot=%f
",mydot);
if(mydot<0.0f){
//printf("drawing %i
",i);
glColor3f(0.9, var+=0.2, 0.5);
//glColorMaterial(<#GLenum face#>, <#GLenum mode#>);
glBegin(GL_POLYGON);
glNormal3fv(mynorm);
for(int j=0;j<list_size(polygon);j++){
GLfloat (*point_ptr) = list_get_at(polygon, j);
//GLfloat *point = *point_ptr;
glVertex3fv(point_ptr);
}
glEnd();
}
}
//glLineWidth(4.0);
glEnable(GL_MULTISAMPLE_ARB);
glutSwapBuffers();
}
void rotate(){
for(int i =0; i<list_size(&polygons);i++){
list_t *polygon = list_get_at(&polygons, i);
for(int j =0; j<list_size(polygon);j++){
GLfloat *point = list_extract_at(polygon, j);
GLfloat point1[16] = {0};
point1[0] = *(point+0);
point1[1] = *(point+1);
point1[2] = *(point+2);
point1[3] = 1;
//multiply:
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(7, 7, 6);
glRotatef(rotate_by,1, 0, 0);
glTranslatef(-7, -7, -6);
glMultMatrixf(point1);
GLfloat resultMat[16];
glGetFloatv(GL_MODELVIEW, resultMat);
//glLoadIdentity();
//glMultMatrixf(resultMat);
//glGetFloatv(GL_MODELVIEW, resultMat);
*(point+0) = resultMat[0];
*(point+1) = resultMat[1];
*(point+2) = resultMat[2];
//printf("new point: (%f,%f,%f)
",*(point+0), *(point+1), *(point+2));
list_insert_at(polygon, point, j);
glPopMatrix();
}
}
}
void initialize ()
{
list_init(&polygons);
list_attributes_copy(&polygons, szelem, 1);
//front
addPolygon(4, &((GLfloat[3]){5,5,5}), &((GLfloat[3]){10,5,5}), &((GLfloat[3]){10,10,5}),&((GLfloat[3]){5,10,5}));
//back
addPolygon(4, &((GLfloat[3]){5,10,7}), &((GLfloat[3]){10,10,7}), &((GLfloat[3]){10,5,7}), &((GLfloat[3]){5,5,7}));
//bottom
addPolygon(4, &((GLfloat[3]){5,5,5}), &((GLfloat[3]){5,5,7}), &((GLfloat[3]){10,5,7}),&((GLfloat[3]){10,5,5}));
//left side
addPolygon(4, &((GLfloat[3]){5,10,5}),&((GLfloat[3]){5,10,7}),&((GLfloat[3]){5,5,7}), &((GLfloat[3]){5,5,5}));
//right
addPolygon(4, &((GLfloat[3]){10,5,5}), &((GLfloat[3]){10,5,7}), &((GLfloat[3]){10,10,7}),&((GLfloat[3]){10,10,5}));
//top
addPolygon(4, &((GLfloat[3]){5,10,5}),&((GLfloat[3]){10,10,5}), &((GLfloat[3]){10,10,7}), &((GLfloat[3]){5,10,7}));
glClearColor(0.0, 0.0, 0.0, 0.0);
//GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//GLfloat mat_shininess[] = { 60.0 };
GLfloat light_position[] = { 0, 0, -5, 1.0f };
GLfloat direction[] = {5,5,7};
// grey diffuse light
float diffuse_light [] = {0.6f, 0.6f, 0.6f, 1};
// yellow specular light
float specular_light [] = {0.4f, 0.4f, 0.4f, 1};
//
//glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
///
//glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction );
// grey diffuse light
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10);
// yellow specular light
glLightfv (GL_LIGHT0, GL_SPECULAR, specular_light);
GLfloat ambientColor[] = {.1, .1, .1, 1}; //Color(0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial (GL_FRONT, GL_DIFFUSE);
glColorMaterial (GL_FRONT, GL_SPECULAR);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glFrontFace(GL_CCW);
rotate_by = 360.0f*DELAY/(15000.0f);
}
void keyboard ( unsigned char key, int mousePositionX, int mousePositionY )
{
switch ( key )
{
case KEY_ESCAPE:
exit ( 0 );
break;
default:
break;
}
}
void resizeWindow(GLsizei w, GLsizei h){
if(h==0 || w==0){
glutReshapeWindow(1, 1);
}
win.height = h;
win.width = w;
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
GLdouble aspect = (GLdouble)( (double)win.width /(double) win.height);
if(aspect>1){
glViewport(0, 0, ((GLdouble)win.width)/aspect, win.height);
}else{
glViewport(0, 0, ((GLdouble)win.width), win.height*aspect);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15, 15, -15, 15, 0, 20);
//glFrustum(-10, 10, -10, 10, 6, 20);
//gluPerspective(90, aspect, 0.1f, 100.0f);
//gluLookAt(0, 0, 0, 0, 0, 0, 0, 1, 0);
}
void Timer(int i){
rotate();
glutTimerFunc(DELAY, Timer, 0);
glutPostRedisplay();
}
int main(int argc, char * argv[])
{
// set window values
win.width =700;
win.height = 700;
win.aspectRatio = win.width/win.height;
win.title = "Q5";
// initialize and run program
glutInitWindowSize(win.width,win.height);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH|GLUT_MULTISAMPLE );
glutCreateWindow(win.title);
glutReshapeFunc(resizeWindow);
initialize();
glutDisplayFunc(display);
glutKeyboardFunc( keyboard );
Timer(0);
glutMainLoop();
return 0;
}
Q: Why is that and how to fix it?
Thanks ahead,
Vitali Pom.