void Initialize(void)
{   
    /* Set background (clear) color to dark blue */ 
    glClearColor(0.0, 0.0, 0.4, 0.0);

    /* Enable depth testing */
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);    
    
    /* Transform matrix for the bar in the middle*/
    
    /*copy objects into buffer*/
    memcpy(vertex_buffer_data1, vertex_octagon, sizeof(vertex_octagon));
    memcpy(vertex_buffer_data2, vertex_pyramid, sizeof(vertex_pyramid));
    memcpy(vertex_buffer_data3, vertex_mainBar, sizeof(vertex_mainBar));
    memcpy(vertex_buffer_data4, vertex_cube, sizeof(vertex_cube));    
    memcpy(vertex_buffer_data5, vertex_cube, sizeof(vertex_cube));
    memcpy(vertex_buffer_data6, vertex_cube, sizeof(vertex_cube));
    memcpy(vertex_buffer_data7, vertex_cube, sizeof(vertex_cube));
/*
    memcpy(color_buffer_data1, color_octagon, sizeof(color_octagon));
    memcpy(color_buffer_data2, color_pyramid, sizeof(color_pyramid));
    memcpy(color_buffer_data3, color_mainBar, sizeof(color_mainBar));
    memcpy(color_buffer_data4, color_cube, sizeof(color_cube));    
    memcpy(color_buffer_data5, color_cube, sizeof(color_cube));
    memcpy(color_buffer_data6, color_cube, sizeof(color_cube));
    memcpy(color_buffer_data7, color_cube, sizeof(color_cube));
*/
    memcpy(index_buffer_data1, index_octagon, sizeof(index_octagon));
    memcpy(index_buffer_data2, index_pyramid, sizeof(index_pyramid));
    memcpy(index_buffer_data3, index_mainBar, sizeof(index_mainBar));
    memcpy(index_buffer_data4, index_cube, sizeof(index_cube));
    memcpy(index_buffer_data5, index_cube, sizeof(index_cube));
    memcpy(index_buffer_data6, index_cube, sizeof(index_cube));
    memcpy(index_buffer_data7, index_cube, sizeof(index_cube));

    /* Setup vertex, color, and index buffer objects */
    SetupDataBuffers();

    /* Setup shaders and shader program */
    CreateShaderProgram();  

    /* Initialize matrices */
    SetIdentityMatrix(ProjectionMatrix);
    SetIdentityMatrix(ViewMatrix);
    int i = 0;
    for(;i<OBJECTS;i++) {
		SetIdentityMatrix(ModelMatrix[i]);
	}
	
	printf("DEBUG: adding light-source ...\n");

	/* Add lighting source to the code */
	
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHT0); // adds first light-source to the scene

	GLfloat ambientLight[] = {0.2, 0.2, 0.2, 1.0};
	GLfloat diffuseLight[] = {0.8, 0.8, 0.8, 1.0};
	GLfloat specularLight[]= {1.0, 1.0, 1.0, 1.0};
	GLfloat positionLight[]= {5.0, 0.0, 0.0, 0.0};
	
	//glShadeModel(GL_SMOOTH);
	
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
	glLightfv(GL_LIGHT0, GL_POSITION, positionLight);
	
	GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
	GLfloat green[] = {0.0, 1.0, 0.0, 1.0};
	GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
	
	glMaterialfv(GL_FRONT, GL_AMBIENT, green);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
	glMaterialfv(GL_FRONT, GL_SPECULAR, white);
	glMaterialf(GL_FRONT, GL_SHININESS, 60.0);
	
	printf("DEBUG: finished adding light-source\n");

    /* Set projection transform */
    float fovy = 45.0;
    float aspect = 1.0; 
    float nearPlane = 1.0; 
    float farPlane = 50.0;
    SetPerspectiveMatrix(fovy, aspect, nearPlane, farPlane, ProjectionMatrix);

    /* Set viewing transform */
    float camera_disp = -10.0;
    SetTranslation(0.0, 0.0, camera_disp, ViewMatrix);

    /* Translate and rotate cube */
    SetTranslation(0, 0, 0, TranslateOrigin);
    SetRotationX(0.0, RotateX);
    SetRotationZ(0.0, RotateZ);	

    /* Translate down */	
    SetTranslation(0, -sqrtf(sqrtf(2.0) * 1.0), 0, TranslateDown);

    /* Initial transformation matrix */
    MultiplyMatrix(RotateX, TranslateOrigin, InitialTransform);
    MultiplyMatrix(RotateZ, InitialTransform, InitialTransform);
}
示例#2
0
void Initialize()
{   
    int k, i;
    int success;
    int vert, indx;

    /* Load OBJ models */
    char* filename[15];
    filename[0] = "models/ring.obj";
    filename[1] = "models/bar_01.obj"; 
    filename[2] = "models/bar_02.obj";
    filename[3] = "models/bar_03.obj";
    filename[4] = "models/bar_04.obj";
    filename[5] = "models/cone.obj";
    filename[6] = "models_n/ball_01.obj";           
    filename[7] = "models_n/ball_02.obj";
    filename[8] = "models/cone_small.obj";
    filename[9] = "models/rectangle_small.obj";
    filename[10] = "models/rectangle_big.obj";
    filename[11] = "models_n/elliptic_ring.obj";
    filename[12] = "models/ellipse.obj";
    
    filename[13] = "models_n/stand.obj";
    filename[14] = "models_n/surrounding.obj";
    
    for(k=0; k<model_count; ++k){
      
      success = parse_obj_scene(&data[k], filename[k]);

      if(!success)
	  printf("Could not load file. Exiting.\n");
    }
  
    /*  Copy mesh data from structs into appropriate arrays */ 
    
    for(k=0; k<model_count; ++k){
      vert = data[k].vertex_count;
      indx = data[k].face_count;

      vertex_buffer_data[k] = (GLfloat*) calloc (vert*3, sizeof(GLfloat));
      index_buffer_data[k] = (GLushort*) calloc (indx*3, sizeof(GLushort));
  
    /* Vertices */
      for(i=0; i<vert; i++)
      {
	vertex_buffer_data[k][i*3] = (GLfloat)(*data[k].vertex_list[i]).e[0] ; 			
	vertex_buffer_data[k][i*3+1] = (GLfloat)(*data[k].vertex_list[i]).e[1];
	vertex_buffer_data[k][i*3+2] = (GLfloat)(*data[k].vertex_list[i]).e[2];
	}

    /* Indices */
      for(i=0; i<indx; i++){
	index_buffer_data[k][i*3] = (GLushort)(*data[k].face_list[i]).vertex_index[0];
	index_buffer_data[k][i*3+1] = (GLushort)(*data[k].face_list[i]).vertex_index[1];
	index_buffer_data[k][i*3+2] = (GLushort)(*data[k].face_list[i]).vertex_index[2];
      }
    }
 
    /* Set background (clear) color to blue */ 
    glClearColor(0.1, 0.2, 0.5, 0.0);

    /* Setup Vertex array object - INTEL FIX*/ 
    GLuint VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);    
    
    /* Enable depth testing */
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);    

    /* Setup vertex, color, and index buffer objects */
    SetupDataBuffers();

    /* Setup shaders and shader program */
    CreateShaderProgram();  

    /* Initialize matrices */
    SetIdentityMatrix(ProjectionMatrix);
    SetIdentityMatrix(ViewMatrix);	
  
    // set all Model matrices       
    for(k=0; k<model_count; ++k){
       SetIdentityMatrix(ModelMatrix[k]);
    }
    
    /* Initialize animation matrices */
    SetIdentityMatrix(RotationMatrixAnimX);
    SetIdentityMatrix(RotationMatrixAnimY);
    SetIdentityMatrix(RotationMatrixAnimZ);
    SetIdentityMatrix(RotationMatrixAnim);
    
    /* Set projection transform */
    SetPerspectiveMatrix(fovy, aspect, nearPlane, farPlane, ProjectionMatrix);

    /* Set initial viewing transform */
    SetTranslation(0.0, 0.0, camera_disp, ViewMatrix);  // camera_disp == z coordinate of camera
    	
}