c_cpp 多个部分的通用UITableView / UICollectionView数据源
多个部分的通用UITableView / UICollectionView数据源
ADVSectionInfo.h
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
@protocol ADVSectionInfo <NSObject>
@required
@property (readonly, nonatomic) NSArray *objects;
@property (readonly, nonatomic) NSUInteger numberOfObjects;
@optional
@property (readonly, nonatomic) NSString *name;
@property (readonly, nonatomic) NSString *indexTitle;
@end
ADVDataSource.m
//
// Copyright © 2013 Yuri Kotov
//
#import "ADVDataSource.h"
#import "ADVSectionInfo.h"
@interface ADVDataSource ()
@property (readonly, nonatomic) NSString *cellIdentifier;
@property (readonly, nonatomic) ADVCellConfigurationBlock configurationBlock;
@end
@implementation ADVDataSource
#pragma mark - ADVManagedDataSource
- (instancetype) initWithSections:(NSArray *)sections
reusableCellIdentifier:(NSString *)identifier
cellConfigurationBlock:(ADVCellConfigurationBlock)block
{
if ((self = [super init]))
{
_sections = sections;
_configurationBlock = block;
_cellIdentifier = identifier;
}
return self;
}
- (NSInteger) numberOfSections
{
return self.sections.count;
}
- (NSInteger) numberOfItemsInSection:(NSInteger)section
{
id <ADVSectionInfo> sectionInfo = self.sections[section];
return sectionInfo.numberOfObjects;
}
- (id) view:(id)view cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
id <ADVSectionInfo> sectionInfo = self.sections[indexPath.section];
id object = sectionInfo.objects[indexPath.row];
UITableViewCell *cell = [view dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
self.configurationBlock(cell, object);
return cell;
}
#pragma mark - UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [self numberOfSections];
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <ADVSectionInfo> sectionInfo = self.sections[section];
return sectionInfo.name;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self numberOfItemsInSection:section];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self view:tableView cellForItemAtIndexPath:indexPath];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return self.numberOfSections;
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self numberOfItemsInSection:section];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [self view:collectionView cellForItemAtIndexPath:indexPath];
}
@end
ADVDataSource.h
//
// Copyright © 2013 Yuri Kotov
//
#import <Foundation/Foundation.h>
typedef void(^ADVCellConfigurationBlock)(id cell, id object);
@interface ADVDataSource : NSObject <UITableViewDataSource, UICollectionViewDataSource>
@property (readonly, nonatomic) NSArray *sections;
- (instancetype) initWithSections:(NSArray *)sections
reusableCellIdentifier:(NSString *)identifier
cellConfigurationBlock:(ADVCellConfigurationBlock)block;
@end
c_cpp C - scanf
C - scanf
scanf_example.c
#include <stdio.h>
int main(void){
char str[] = "hello world";
int num;
float value;
scanf("%s", str);
scanf("%d", &num);
scanf("%f", &value);
printf("%s\n", str);
printf("%d\n", num);
printf("%f\n", value);
return 0;
}
c_cpp C - 多维字符串数组
C - 多维字符串数组
multi_array.c
#include <stdio.h>
void print_day(int day){
char days[8][10] = {
"", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"
};
if (day < 1 || day > 7){
printf("Illegal day number!\n");
}
printf("%s\n", days[day]);
}
int main(void){
print_day(2);
return 0;
}
c_cpp C - 随机值
C - 随机值
gen_random.c
#include <stdlib.h>
#include <stdio.h>
#include <time.h> /*用到了time函数,所以要有这个头文件*/
#define MAX 10
int main( void)
{
int number[MAX] = {0};
int i;
srand((unsigned) time(NULL)); /*播种子*/
for(i = 0; i < MAX; i++)
{
number[i] = rand() % 100; /*产生100以内的随机整数*/
printf("%d ", number[i]);
}
printf("\n");
return 0;
}
c_cpp C - 指针作为参数
C - 指针作为参数
draft.c
#include <stdio.h>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main(int argc, char *argv[]){
int num1 = 5;
int num2 = 10;
printf("before swap: %d, %d\n", num1, num2);
swap(&num1, &num2);
printf("after swap: %d, %d\n",num1, num2);
return 0;
}
c_cpp C - 结构
C - 结构
struct.c
#include <math.h>
struct ComplexStruct {
// 定义复数的结构体
double x, y;
};
double real_part(struct ComplexStruct z){
// 返回复数的实部
return z.x;
}
double img_part(struct ComplexStruct z){
// 返回复数的虚部
return z.y;
}
double magnitude(struct ComplexStruct z){
// 返回复数的模
return sqrt(z.x * z.x + z.y * z.y);
}
double angle(struct ComplexStruct z){
// 返回复数的辐角
return atan2(z.y, z.x);
}
struct ComplexStruct make_from_real_img(double x, double y){
// 用实部和虚部构造复数结构体
struct ComplexStruct z;
z.x = x;
z.y = y;
return z;
}
struct ComplexStruct make_from_mag_ang(double r, double A){
// 用辐角和模构造复数结构体
struct ComplexStruct z;
z.x = r * cos(A);
z.y = r * sin(A);
return z;
}
// 定义复数的加减乘除运算
struct ComplexStruct add_complex(struct ComplexStruct z1, struct ComplexStruct z2){
return make_from_real_img(
real_part(z1) + real_part(z2),
img_part(z1) + img_part(z2)
);
}
struct ComplexStruct sub_complex(struct ComplexStruct z1, struct ComplexStruct z2){
return make_from_real_img(
real_part(z1) - real_part(z2),
img_part(z1) - img_part(z2)
);
}
struct ComplexStruct mul_complex(struct ComplexStruct z1, struct ComplexStruct z2){
return make_from_mag_ang(
magnitude(z1) * magnitude(z2),
angle(z1) + angle(z2)
);
}
struct ComplexStruct div_complex(struct ComplexStruct z1, struct ComplexStruct z2){
return make_from_mag_ang(
magnitude(z1) / magnitude(z2),
angle(z1) - angle(z2)
);
}
c_cpp 与Pebble通信的Arduino代码
与Pebble通信的Arduino代码
Pebble.ino
#include <string.h>
#include <ctype.h>
#include <SoftwareSerial.h>
// the Bluetooth Shield connects to Pin D9 & D10
SoftwareSerial bt(9,10);
const uint8_t req[5] = {0x00, 0x01, 0x00, 0x11, 0x00};
const uint8_t cap[17] = {0x00, 0x0d, 0x00, 0x11, 0x01, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32};
const uint8_t ping[9] = {0x00, 0x05, 0x07, 0xd1, 0x00, 0xde, 0xad, 0xbe, 0xef};
void setup()
{
Serial.begin(9600);
bt.setTimeout(100);
bt.begin(9600);
Serial.println("Arduino - Pebble Test");
}
void loop()
{
int i = 0;
bool is_req = false;
// After connected with Arduino, Pebble sends a 5 bytes request asking for "Phone Version"
// We make a fake answer to Pebble so that Pebble takes our arduino as an "android phone"
// For details, check https://github.com/Hexxeh/libpebble
if(bt.available())
{
for(i = 0;i < 5; i++)
{
int sig = bt.read();
Serial.print((char)sig);
if(req[i] != sig)
{
break;
}
is_req = true;
}
}
if(is_req)
{
bt.write(cap, 17);
}
bt.write(ping, 9);
delay(5000);
}
c_cpp Java Directory App
Java Directory App
gistfile1.cpp
package project5;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the file containing the directory you would like to load:");
String fileName = in.nextLine();
fileName = "directory.txt";
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
int rowCount = 0;
ArrayList tempRows = new ArrayList();
ArrayList<ArrayList<String>> dir = new ArrayList<ArrayList<String>>();
while((lineString = br.readLine()) != null) {
dir.add(rowCount, new ArrayList<String>());
String [] tmpArray = lineString.split("-");
for(int x = 0; x < 7; x++){
dir.get(rowCount).add(tmpArray[x]);
}
tempRows.add(rowCount, lineString);
rowCount++;
}
while(menu){
System.out.println("********* CURRENT DIRECTORY LOADED ***********");
for(int x = 0; x < dir.size(); x++){
for(int y = 0; y < 5; y++){
System.out.print(dir.get(x).get(y) + " - ");
}
System.out.println(dir.get(x).get(6));
System.out.println("");
}
System.out.println("***********************************************");
System.out.println("");
System.out.println("1) View the contentents of a perticular entry");
System.out.println("2) Sort the entire directory");
System.out.println("3) Insert an entry");
System.out.println("4) Delete an entry");
System.out.println("5) Save the directory to a file and exit");
System.out.println("");
Double input = in.nextDouble();
System.out.println("");
if(input == 1){ //If the user wants to view a specific entry
System.out.println("Directory Entries: (last, first)");
System.out.println("");
for(int x = 0; x < dir.size(); x++){ //We display the first and last names to the user
for(int y = 0; y < 2; y ++){
System.out.print(dir.get(x).get(y));
if(y!=1){
System.out.print(", ");
}
}
System.out.println(" ");
}
//We ask the user what last name he would like to view more about
System.out.println("Please enter the last name of the person you would like to view");
String inp = in.next();
String[] data = new String[dir.size()];
for(int x = 0; x < dir.size(); x++){
data[x] = dir.get(x).get(0);
}
binSearch bs = new binSearch(data, inp); //BINARY SEARCH
System.out.println("");
System.out.println("");
entryRequest = bs.search();
System.out.println(entryRequest);
System.out.println("****");
for(int x = 0; x < data.length; x++){
System.out.println(x + " - " + data[x]);
}
if(entryRequest < 0){
System.out.println("That entry does not exist");
} else {
System.out.println(entryRequest);
for(int x = 0; x < 5; x++){
System.out.print(dir.get(entryRequest).get(x) + " - ");
}
System.out.println("");
}
} else if(input == 3){
System.out.println("");
System.out.println("Here you may enter a new person into the directory");
System.out.println("Enter it in the format of:");
System.out.println("Last name - first name - road - city - state - zip code - phone number");
System.out.println("Seperated by brackets");
System.out.println("");
String addInp = in.next();
String [] tmpAddInp = addInp.split("-");
int dirSize = dir.size();
dir.add(dirSize, new ArrayList<String>());
for(int x = 0; x < tmpAddInp.length; x++){
dir.get(dirSize).add(x, tmpAddInp[x]);
}
System.out.println("Entry successfully added!");
} else if(input == 2){
String[] lastNames = new String[dir.size()];
for(int x = 0; x < dir.size(); x++){
lastNames[x] = dir.get(x).get(0);
}
ArrayList<ArrayList<String>> dirTemp = new ArrayList<ArrayList<String>>();
Arrays.sort(lastNames);
for(int x = 0; x < dir.size(); x++){
for(int y = 0; y < 5; y++){
System.out.print(dirTemp.get(x).get(y) + " - ");
}
System.out.println(" ");
}
}
}
br.close();
}
private static String inputFile, lineString, lineCount = null;
private static int count, entryRequest = 0;
private static boolean found = false;
private static boolean menu = true;
}
c_cpp C ++ Spinning Cube
C ++ Spinning Cube
gistfile1.cpp
#include GLUT/glut.h
#include iostream
#include stdlib.h
#include math.h
#include cmath
using namespace std;
class wcPt3D // class to represent a 2D point
{
public:
GLfloat x, y, z;
};
GLfloat xRotated, yRotated, zRotated;
GLfloat xScale = 1, yScale = 1, zScale = 1;
GLfloat xTranslate = 0, yTranslate = 0, zTranslate = 02;
string mode = "none";
wcPt3D top[3];
wcPt3D bottom[3];
wcPt3D left[3];
wcPt3D right[3];
wcPt3D front[3];
wcPt3D back[3];
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void DrawCube(void)
{
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-10.5);
glPushMatrix();
glRotatef(xRotated,1.0,0.0,0.0); // rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0); // rotation about Z axis
glTranslatef(xTranslate, yTranslate, zTranslate);
glScalef(xScale, yScale, zScale);
glRotatef(zRotated,0.0,0.0,1.0);
glBegin(GL_QUADS); // Draw The Cube Using quads
glColor3f(0.0f,1.0f,0.0f); // Color Blue
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1, 1, 1); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f); // Color Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f); // Color Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f); // Color Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f); // Color Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f); // Color Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // End Drawing The Cube
//glPopMatrix();
//glutSwapBuffers();
glFlush();
}
void animation(void)
{
if(mode=="spin"){
yRotated += 0.5;
xRotated += 0.6;
}
DrawCube();
}
void reshape(int x, int y)
{
if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Angle of view:40 degrees
//Near clipping plane distance: 0.5
//Far clipping plane distance: 20.0
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y); //Use the whole window for rendering
}
void key(unsigned char key, int x, int y){
switch(key){
case 115:
if(mode=="spin"){
mode = "none";
} else {
mode = "spin";
}
glutPostRedisplay();
break;
case 103: //g
xScale = xScale + .1;
yScale = yScale + .1;
zScale = zScale + .1;
glutPostRedisplay();
break;
case 'h':
xScale = xScale - .1;
yScale = yScale - .1;
zScale = zScale - .1;
glutPostRedisplay();
break;
case 'd':
xTranslate = xTranslate + .1;
glutPostRedisplay();
break;
case 'f':
xTranslate = xTranslate - .1;
glutPostRedisplay();
break;
case 'j':
xRotated = xRotated + 1;
//yRotated = yRotated + 1;
glutPostRedisplay();
break;
case 'k':
xRotated = xRotated - 1;
//yRotated = yRotated - 1;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv){
glutInit(&argc, argv);
//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(10, 10);
glutInitWindowSize(1000, 1000);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
//Set the function for the animation.
glutIdleFunc(animation);
glutMainLoop();
return 0;
}
c_cpp C ++ 3D城市建筑飞行
C ++ 3D城市建筑飞行
gistfile1.cpp
#include GLUT/glut.h
#include iostream
#include stdlib.h
#include math.h
#include cmath
using namespace std;
class wcPt2D // class to represent a 2D point
{
public:
GLfloat x, y, z;
};
wcPt2D look;
wcPt2D eye;
GLfloat yScale;
GLfloat xScale;
GLfloat yTranslate;
GLfloat xTranslate;
GLfloat zTranslate;
GLfloat color1, color2, color3;
/* Lights */
GLfloat globalAmbient[]={.0, .0, .0, 1.0}; // Global lighting
/* Light 0 is the overhead light */
GLfloat light0_ambient[]={.1, .1, .1, 1.0};
GLfloat light0_diffuse[]={.5, .5, .5, 1.0};
GLfloat light0_specular[]={.1, .1, .1, 1.0};
GLfloat light0_position[]={0.0, 1.5 , 0.0, 0.0}; // change last value to 1.0 to make this positional
/* Light 1 is the bottom light */
GLfloat light1_ambient[]={.1, .1, .1, 1.0};
GLfloat light1_diffuse[]={.5, .5, .5, 1.0};
GLfloat light1_specular[]={.1, .1, .1, 1.0};
GLfloat light1_position[]={0.0, -1.5 , 0.0, 0.0}; // change last value to 1.0 to make this positional
/* Light 3 is right spotlight */
GLfloat light3_ambient[]={.1, .1, .1, 1.0};
GLfloat light3_diffuse[]={.5, .5, .5, 1.0};
GLfloat light3_specular[]={.1, .1, .1, 1.0};
GLfloat dirVectorLight3[]={0.0, 0.0, 0.0};
GLfloat light3_position[]={0.0, 0.0, 0.0, 1.0};
/* Light 4 is right spotlight */
GLfloat light4_ambient[]={.5, .5, .5, 1.0};
GLfloat light4_diffuse[]={.5, .5, .5, 1.0};
GLfloat light4_specular[]={.1, .1,.1, 1.0};
GLfloat dirVectorLight4[]={0.0, 0.0, 0.0};
GLfloat light4_position[]={0.0, 0.0, 0.0, 1.0};
/* Materials */
//http://www.opengl.org/archives/resources/code/samples/sig99/advanced99/notes/node153.html
//Brass
GLfloat matBrass_specular[]={.992157, .941176, .807843, 1.0};
GLfloat matBrass_diffuse[]={.780392, .568627, .113725, 1.0};
GLfloat matBrass_ambient[]={.329412, .223529, .027451, 1.0};
GLfloat matBrass_shininess={27.8974};
//Chrome
GLfloat matChrome_specular[]={.774597, .458561, .200621, 1.0};
GLfloat matChrome_diffuse[]={.4, .4, .4, 1.0};
GLfloat matChrome_ambient[]={.25, .25, .25, 1.0};
GLfloat matChrome_shininess={76.8};
//Jade
GLfloat matJade_specular[]={.316228, .316228, .316228, .95};
GLfloat matJade_diffuse[]={.54, .89, .63, .95};
GLfloat matJade_ambient[]={.135, .2225, .1575, .95};
GLfloat matJade_shininess={12.8};
//Polished
GLfloat matPolished_specular[]={.774597, .774597, .774597, 1.0};
GLfloat matPolished_diffuse[]={.4, .4, .4, 1.0};
GLfloat matPolished_ambient[]={.25, .25, .25, 1.0};
GLfloat matPolished_shininess={76.8};
//Gold
GLfloat matGold_specular[]={.628281, .555802, .366065, 1.0};
GLfloat matGold_diffuse[]={.75164, .60648, .22648, 1.0};
GLfloat matGold_ambient[]={.24725, .1995, .0745, 1.0};
GLfloat matGold_shininess={51.2};
//Ruby
GLfloat matRuby_specular[]={.727811, .626959, .626959, .55};
GLfloat matRuby_diffuse[]={.61424, .04136, .04136, .55};
GLfloat matRuby_ambient[]={.1745, .01175, .01175, .55};
GLfloat matRuby_shininess={76.8};
bool start, initial;
void init(void)
{
glEnable(GL_CULL_FACE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_BACK, GL_FILL);
glPolygonMode(GL_FRONT, GL_FILL);
//glEnable(GL_COLOR_MATERIAL);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);
glShadeModel(GL_SMOOTH); /* enable smooth shading */
glClearColor (0.0, 0.0, 0.0, 1.0);
glColor3f (1.0, 1.0, 1.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
light4_position[0]=2.0;
light4_position[1]=0.0;
light4_position[2]=1.0;
light4_position[3]=1.0;
glLightf (GL_LIGHT4, GL_SPOT_CUTOFF, 40.0);
glLightf(GL_LIGHT4, GL_SPOT_EXPONENT, 10.5);
glLightfv(GL_LIGHT4, GL_AMBIENT, light4_ambient);
glLightfv(GL_LIGHT4, GL_DIFFUSE, light4_diffuse);
glLightfv(GL_LIGHT4, GL_SPECULAR, light4_specular);
glLightfv(GL_LIGHT4, GL_POSITION, light4_position);
light3_position[0]=2.0;
light3_position[1]=0.0;
light3_position[2]=1.0;
light3_position[3]=1.0;
glLightf (GL_LIGHT3, GL_SPOT_CUTOFF, 40.0);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 10.5);
glLightfv(GL_LIGHT3, GL_AMBIENT, light3_ambient);
glLightfv(GL_LIGHT3, GL_DIFFUSE, light3_diffuse);
glLightfv(GL_LIGHT3, GL_SPECULAR, light3_specular);
glLightfv(GL_LIGHT3, GL_POSITION, light3_position);
start = true;
look.x = 3;
look.y = 14;
look.z = 20;
eye.z = 20;
eye.x = 0;
zTranslate = 0;
xTranslate = 0;
initial = true;
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
//glEnable(GL_DEPTH_TEST);
}
void aCube(string color) // draw the cube
{
glPolygonMode(GL_FRONT, GL_FILL); // front is solid fill
glPolygonMode(GL_BACK, GL_LINE); // back is wireframe
GLfloat x1[] = {-1, 1, -1}; // left back top
GLfloat x2[] = {-1, 1, 1}; // left front top
GLfloat x3[] = {1, 1, 1}; // right front top
GLfloat x4[] = {1, 1, -1}; // right back top
GLfloat x5[] = {-1, -1, -1}; // left back bottom
GLfloat x6[] = {-1, -1, 1}; // left front bottom
GLfloat x7[] = {1, -1, 1}; // right front bottom
GLfloat x8[] = {1, -1, -1}; // right back bottom
glBegin(GL_QUADS);
//Top
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,1,0);
glVertex3fv(x1);
glVertex3fv(x2);
glVertex3fv(x3);
glVertex3fv(x4);
//Bottom
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,-1,0);
glVertex3fv(x5);
glVertex3fv(x8);
glVertex3fv(x7);
glVertex3fv(x6);
//Back
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,0,-1);
glVertex3fv(x1);
glVertex3fv(x4);
glVertex3fv(x8);
glVertex3fv(x5);
//Front
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,0,1);
glVertex3fv(x2);
glVertex3fv(x6);
glVertex3fv(x7);
glVertex3fv(x3);
//Left
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(-1,0,0);
glVertex3fv(x2);
glVertex3fv(x1);
glVertex3fv(x5);
glVertex3fv(x6);
//Right
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(1,0,0);
glVertex3fv(x4);
glVertex3fv(x3);
glVertex3fv(x7);
glVertex3fv(x8);
glEnd();
}
void B_Complex(){
glTranslatef(2, 0, -8);
glScalef(1, 2, 1);
aCube("bComplex");
}
void A_Complex(){
glTranslatef(-3, 0, 0);
glScalef(1, 3, 2);
aCube("aComplex");
}
void C_Complex(){
glTranslatef(-5, 0, -10);
glScalef(1, 5, 1);
aCube("cComplex");
}
void D_Complex(){
glTranslatef(1, 0, -14);
glScalef(2, 4, 1);
aCube("dComplex");
}
void complex(){
glTranslatef(5, 0, -3);
glScalef(1, 3, 2);
glRotated(2, 0, 0, 0);
aCube("eComplex");
}
void DrawCube(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING); /* turn lights on */
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
glEnable(GL_LIGHT4);
//glMatrixMode(GL_MODELVIEW);
//glutSolidSphere (1.0, 20, 16);
A_Complex();
glColor3f(.0, .0, .0);
glLoadIdentity();
gluLookAt(look.x, look.y, look.z, eye.x, 0, -20, 0, 1, 0);
glPushMatrix();
glBegin(GL_QUADS);
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
glNormal3f(10, 0, 1);
glVertex3f(-10, -3, -15);
glVertex3f(-10, -3, 11);
glVertex3f(10, -3, 11);
glVertex3f(10, -3, -15);
glEnd();
glPopMatrix();
glPushMatrix();
A_Complex();
glPopMatrix();
glPushMatrix();
B_Complex();
glPopMatrix();
glPushMatrix();
C_Complex();
glPopMatrix();
glPushMatrix();
D_Complex();
glPopMatrix();
glPushMatrix();
complex();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int x, int y)
{
if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,200);
glMatrixMode(GL_MODELVIEW);
//glViewport(0,0,x,y); //Use the whole window for rendering
glLoadIdentity();
}
void timer(int input){
if(start){
if(look.z > -12){
look.z -= .05;
if(look.z < 9){
if(look.x > 0){
look.x -= .05;
}
}
if(look.y > 4){
look.y -= .05;
}
}
if(look.z < -9){
eye.x += .5;
look.x += .2;
if(look.x > 7){
start = false;
}
}
} else {
eye.x = 0;
eye.z = 0;
eye.y = 0;
look.y = 10;
look.z = 20;
look.x = 0;
}
cout << "z - " << look.z << endl;
cout << "x - " << look.x << endl;
glutTimerFunc(30, timer, 1);
glutPostRedisplay();
}
void key(unsigned char key, int x, int y){
switch(key){
case 'g': {
start = true;
glutPostRedisplay();
break;
}
}
DrawCube();
}
void animation(void)
{
DrawCube();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(10, 10);
glutInitWindowSize(1000, 800);
glutCreateWindow(argv[0]);
init();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(animation);
glutTimerFunc(50, timer, 1);
glutMainLoop();
return 0;
}