#include "stdafx.h"
#include "math.h"
#include <random>
#include "stdafx.h"
#include <time.h>
#include <windows.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
GLfloat A, B, C, D;
double M_PI=3.14159265358979323846;
GLdouble orth[16]={1,0,0,0, 0,1,0,0, -cos(45*M_PI/180),-sin(45*M_PI/150),1,0, 0,0,0,1};
GLboolean f=0;
GLfloat forx, fory;
typedef struct Point {
float x, y, z;
} Point;
class Prism {
public:
Point* vertex;
int n;
Prism() {
n = 0;
}
Prism(Point* tmp, int m) {
vertex = new Point[m];
for (n = 0; n < m; n++)
vertex[n] = tmp[n];
}
Prism(int m) {
vertex = new Point[m];
n = m;
}
~Prism() {
if (n > 0) {
delete []vertex;
n = 0;
}
}
Prism* computate(int current_time, Prism &A, Prism &B) {
int c_time = current_time % 60;
for(int i = 0; i < this->n; i++) {
this->vertex[i].x = A.vertex[i].x * (60 - c_time) + B.vertex[i].x * c_time;
this->vertex[i].y = A.vertex[i].y * (60 - c_time) + B.vertex[i].y * c_time;
this->vertex[i].z = A.vertex[i].z * (60 - c_time) + B.vertex[i].z * c_time;
}
return this;
}
void draw(int current_time) {
//this->computate(current_time);
int i;
glBegin(GL_POLYGON);
glColor3f(1, 0, 0);
for( i = 0; i < n/2; i++)
glVertex3f(this->vertex[i].x, this->vertex[i].y, this->vertex[i].z);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1, 0, 1);
for(i = n/2; i < n; i++)
glVertex3f(this->vertex[i].x, this->vertex[i].y, this->vertex[i].z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3d(0, 0, 1);
for (int i = 1; i < n/2 - 1; i++) {
glVertex3f(this->vertex[i-1].x, this->vertex[i-1].y, this->vertex[i-1].z);
glVertex3f(this->vertex[i].x, this->vertex[i].y, this->vertex[i].z);
glVertex3f(this->vertex[i+n/2].x, this->vertex[i+n/2].y, this->vertex[i+n/2].z);
}
glVertex3f(this->vertex[n/2-1].x, this->vertex[n/2-1].y, this->vertex[n/2-1].z);
glVertex3f(this->vertex[n/2].x, this->vertex[n/2].y, this->vertex[n/2].z);
glVertex3f(this->vertex[0].x, this->vertex[0].y, this->vertex[0].z);
glEnd();
glBegin(GL_TRIANGLES);
glColor3d(0, 0, 1);
for (int i = 0; i < n/2 - 2; i++) {
glVertex3f(this->vertex[n/2+i].x, this->vertex[n/2+i].y, this->vertex[n/2+i].z);
glVertex3f(this->vertex[i].x, this->vertex[i].y, this->vertex[i].z);
glVertex3f(this->vertex[i+n/2+1].x, this->vertex[i+n/2+1].y, this->vertex[i+n/2+1].z);
}
glVertex3f(this->vertex[n-1].x, this->vertex[n-1].y, this->vertex[n-1].z);
glVertex3f(this->vertex[n/2-1].x, this->vertex[n/2-1].y, this->vertex[n/2-1].z);
glVertex3f(this->vertex[n/2].x, this->vertex[n/2].y, this->vertex[n/2].z);
glEnd();
/*
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0, 0, 1);
for(i = 1; i < n/2; i++) {
glVertex3f(this->vertex[i-1].x, this->vertex[i-1].y, this->vertex[i-1].z);
glVertex3f(this->vertex[i+n/2].x, this->vertex[i+n/2].y, this->vertex[i+n/2].z);
}
glVertex3f(this->vertex[7].x, this->vertex[0].y, this->vertex[0].z);
glVertex3f(this->vertex[n/2].x, this->vertex[n/2].y, this->vertex[n/2].z);
glEnd();
*/
glBegin(GL_LINE_LOOP);
glColor3f(0, 1, 1);
for(i = 1; i < n/2; i++) {
glVertex3f(this->vertex[i-1].x, this->vertex[i-1].y, this->vertex[i-1].z);
glVertex3f(this->vertex[i+n/2].x, this->vertex[i+n/2].y, this->vertex[i+n/2].z);
}
glEnd();
}
void animate(int time, Prism &A, Prism &B) {
for (int i = 0; i < time; i++) {
this->computate(i, A, B);
this->draw(i);
Sleep(240);
}
}
};
typedef struct GLfloatPoint
{
GLfloat x;
GLfloat y;
GLfloat z;
} GLfloatPoint;
static void cursor_callback(GLFWwindow* window, double x, double y)
{
}
static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
{
if(button == GLFW_MOUSE_BUTTON_RIGHT)
{
if(action == GLFW_PRESS) glfwSetInputMode( window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
if(action == GLFW_RELEASE) glfwSetInputMode( window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
static void resize_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, width, 0.0, height, -width*2, width*2);
glMultMatrixd((GLdouble *)&orth);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
A = GLfloat(width / 4.0);
B = 0.0;
C = D = GLfloat(height / 2.0);
printf("Reshape occured\n");
}
static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_LEFT) {
glTranslatef(A*2,D,0);
glRotatef(5,0.0,1.0,0.0);
glTranslatef(-2*A,-D,0);
}
if (key == GLFW_KEY_RIGHT) {
glTranslatef(A*2,D,0);
glRotatef(-5,0.0,1.0,0.0);
glTranslatef(-2*A,-D,0);
}
if (key == GLFW_KEY_UP) {
glTranslatef(A*2,D,0);
glRotatef(5,1.0,0.0,0.0);
glTranslatef(-2*A,-D,0);
}
if (key == GLFW_KEY_DOWN) {
glTranslatef(A*2,D,0);
glRotatef(-5,1.0,0.0,0.0);
glTranslatef(-2*A,-D,0);
}
if (key == GLFW_KEY_W)
fory += 5;
if (key == GLFW_KEY_S)
fory -= 5;
if (key == GLFW_KEY_D)
forx += 5;
if (key == GLFW_KEY_A)
forx -= 5;
if (key == GLFW_KEY_SPACE)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (key == GLFW_KEY_Z)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
void coor_axes()
{
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(100.0,100.0,0.0);
glVertex3f(600,100.0,0.0);
glColor3f(0,1,0);
glVertex3f(100,100,0.0);
glVertex3f(100,600,0.0);
glColor3f(0,0,1);
glVertex3f(100,100,0.0);
glVertex3f(100,100,500);
glEnd();
}
void cube()
{
GLfloatPoint cube_coor[8]={ {100,100,0},{100,100,30},
{130,100,30},{130,100,0},
{100,130,0},{100,130,30},
{130,130,30},{130,130,0}};
glBegin(GL_LINE_LOOP);
glColor3f(0,0,1);
glVertex3f(cube_coor[0].x,cube_coor[0].y,cube_coor[0].z);
glVertex3f(cube_coor[1].x,cube_coor[1].y,cube_coor[1].z);
glVertex3f(cube_coor[2].x,cube_coor[2].y,cube_coor[2].z);
glVertex3f(cube_coor[3].x,cube_coor[3].y,cube_coor[3].z);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0,1,0);
glVertex3f(cube_coor[4].x,cube_coor[4].y,cube_coor[4].z);
glVertex3f(cube_coor[5].x,cube_coor[5].y,cube_coor[5].z);
glVertex3f(cube_coor[6].x,cube_coor[6].y,cube_coor[6].z);
glVertex3f(cube_coor[7].x,cube_coor[7].y,cube_coor[7].z);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0,1,1);
glVertex3f(cube_coor[3].x,cube_coor[3].y,cube_coor[3].z);
glVertex3f(cube_coor[2].x,cube_coor[2].y,cube_coor[2].z);
glVertex3f(cube_coor[6].x,cube_coor[6].y,cube_coor[6].z);
glVertex3f(cube_coor[7].x,cube_coor[7].y,cube_coor[7].z);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1,0,0);
glVertex3f(cube_coor[0].x,cube_coor[0].y,cube_coor[0].z);
glVertex3f(cube_coor[1].x,cube_coor[1].y,cube_coor[1].z);
glVertex3f(cube_coor[5].x,cube_coor[5].y,cube_coor[5].z);
glVertex3f(cube_coor[4].x,cube_coor[4].y,cube_coor[4].z);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1,0,1);
glVertex3f(cube_coor[1].x,cube_coor[1].y,cube_coor[1].z);
glVertex3f(cube_coor[5].x,cube_coor[5].y,cube_coor[5].z);
glVertex3f(cube_coor[6].x,cube_coor[6].y,cube_coor[6].z);
glVertex3f(cube_coor[2].x,cube_coor[2].y,cube_coor[2].z);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1,1,0);
glVertex3f(cube_coor[0].x,cube_coor[0].y,cube_coor[0].z);
glVertex3f(cube_coor[4].x,cube_coor[4].y,cube_coor[4].z);
glVertex3f(cube_coor[7].x,cube_coor[7].y,cube_coor[7].z);
glVertex3f(cube_coor[3].x,cube_coor[3].y,cube_coor[3].z);
glEnd();
}
void draw_prisma() {
int i;
GLfloatPoint prisma_coor[16];
for(int i=0;i<8;i++){
prisma_coor[i].x=forx+250+100*cos((i+0.5)*M_PI/4);
prisma_coor[i].y=150+fory;
prisma_coor[i].z=100*sin((i+0.5)*M_PI/4);
}
for(int i=8;i<16;i++){
prisma_coor[i].x=forx+250+100*cos(i*M_PI/4);
prisma_coor[i].y=200+fory;
prisma_coor[i].z=100*sin(i*M_PI/4);
}
glBegin(GL_POLYGON);
glColor3f(1,0,0);
for(i=0; i<8; i++) {
glVertex3f(prisma_coor[i].x,prisma_coor[i].y,prisma_coor[i].z);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,0,1);
for(i=8; i<16; i++) {
glVertex3f(prisma_coor[i].x,prisma_coor[i].y,prisma_coor[i].z);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0,0,1);
for(i=0; i<8; i++) {
glVertex3f(prisma_coor[i].x,prisma_coor[i].y,prisma_coor[i].z);
glVertex3f(prisma_coor[i+8].x,prisma_coor[i+8].y,prisma_coor[i+8].z);
}
glVertex3f(prisma_coor[0].x,prisma_coor[0].y,prisma_coor[0].z);
glVertex3f(prisma_coor[8].x,prisma_coor[8].y,prisma_coor[8].z);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0,1,1);
int index;
//for(i=0, index = 0; i<16; i++, index = (index + 8)%15 ) {
for(i=0; i<8; i++) {
//glVertex3f(prisma_coor[index].x,prisma_coor[index].y,prisma_coor[index].z);
glVertex3f(prisma_coor[i].x,prisma_coor[i].y,prisma_coor[i].z);
glVertex3f(prisma_coor[i+8].x,prisma_coor[i+8].y,prisma_coor[i+8].z);
}
glEnd();
/*glBegin(GL_LINE_LOOP);
glColor3f(0,1,1);
glVertex3f(prisma_coor[0].x,prisma_coor[0].y,prisma_coor[0].z);
glVertex3f(prisma_coor[8].x,prisma_coor[8].y,prisma_coor[8].z);
glVertex3f(prisma_coor[1].x,prisma_coor[1].y,prisma_coor[1].z);
glVertex3f(prisma_coor[9].x,prisma_coor[9].y,prisma_coor[9].z);
glVertex3f(prisma_coor[2].x,prisma_coor[2].y,prisma_coor[2].z);
glVertex3f(prisma_coor[10].x,prisma_coor[10].y,prisma_coor[10].z);
glVertex3f(prisma_coor[3].x,prisma_coor[3].y,prisma_coor[3].z);
glVertex3f(prisma_coor[11].x,prisma_coor[11].y,prisma_coor[11].z);
glVertex3f(prisma_coor[4].x,prisma_coor[4].y,prisma_coor[4].z);
glVertex3f(prisma_coor[12].x,prisma_coor[12].y,prisma_coor[12].z);
glVertex3f(prisma_coor[5].x,prisma_coor[5].y,prisma_coor[5].z);
glVertex3f(prisma_coor[13].x,prisma_coor[13].y,prisma_coor[13].z);
glVertex3f(prisma_coor[6].x,prisma_coor[6].y,prisma_coor[6].z);
glVertex3f(prisma_coor[14].x,prisma_coor[14].y,prisma_coor[14].z);
glVertex3f(prisma_coor[7].x,prisma_coor[7].y,prisma_coor[7].z);
glVertex3f(prisma_coor[15].x,prisma_coor[15].y,prisma_coor[15].z);
glEnd();*/
/*glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex3f(prisma_coor[15].x,prisma_coor[15].y,prisma_coor[15].z);
glVertex3f(prisma_coor[0].x,prisma_coor[0].y,prisma_coor[0].z);
glVertex3f(prisma_coor[8].x,prisma_coor[8].y,prisma_coor[8].z);
glVertex3f(prisma_coor[1].x,prisma_coor[1].y,prisma_coor[1].z);
glVertex3f(prisma_coor[9].x,prisma_coor[9].y,prisma_coor[9].z);
glVertex3f(prisma_coor[2].x,prisma_coor[2].y,prisma_coor[2].z);
glVertex3f(prisma_coor[10].x,prisma_coor[10].y,prisma_coor[10].z);
glVertex3f(prisma_coor[3].x,prisma_coor[3].y,prisma_coor[3].z);
glVertex3f(prisma_coor[11].x,prisma_coor[11].y,prisma_coor[11].z);
glVertex3f(prisma_coor[4].x,prisma_coor[4].y,prisma_coor[4].z);
glVertex3f(prisma_coor[12].x,prisma_coor[12].y,prisma_coor[12].z);
glVertex3f(prisma_coor[5].x,prisma_coor[5].y,prisma_coor[5].z);
glVertex3f(prisma_coor[13].x,prisma_coor[13].y,prisma_coor[13].z);
glVertex3f(prisma_coor[6].x,prisma_coor[6].y,prisma_coor[6].z);
glVertex3f(prisma_coor[14].x,prisma_coor[14].y,prisma_coor[14].z);
glVertex3f(prisma_coor[7].x,prisma_coor[7].y,prisma_coor[7].z);
glEnd();*/
}
void draw(void)
{
glPushMatrix();
glLoadIdentity();
cube();
coor_axes();
glPopMatrix();
//draw_prisma();
Point prisma_coor[16];
for(int i = 0; i < 8; i++){
prisma_coor[i].x = forx+250+100*cos((i+0.5)*M_PI/4);
prisma_coor[i].y = 150+fory;
prisma_coor[i].z = 100*sin((i+0.5)*M_PI/4);
}
for(int i = 8; i < 16; i++){
prisma_coor[i].x = forx+250+100*cos(i*M_PI/4);
prisma_coor[i].y = 200+fory;
prisma_coor[i].z = 100*sin(i*M_PI/4);
}
Prism C(16);
Prism A(prisma_coor, 16);
for(int i = 0; i < 8; i++){
prisma_coor[i].x = forx+250+50*cos((i+0.5)*M_PI/4);
prisma_coor[i].y = 150+fory;
prisma_coor[i].z = 50*sin((i+0.5)*M_PI/4);
}
for(int i = 8; i < 16; i++){
prisma_coor[i].x = forx+250+50*cos(i*M_PI/4);
prisma_coor[i].y = 200+fory;
prisma_coor[i].z = 50*sin(i*M_PI/4);
}
Prism B(prisma_coor, 16);
C.animate(5000, A, B);
}
int main(int argc, char* argv[])
{
A = SCREEN_WIDTH / 4.0;
B = 0.0;
C = D = SCREEN_HEIGHT / 2.0;
// initialise GLFW
if(!glfwInit())
{
printf("glfwInit failed\n");
return -1;
}
glfwSetErrorCallback(error_callback);
GLFWwindow* window;
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
if (window == NULL)
{
printf("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?\n");
glfwTerminate();
return -2;
}
int attrib;
attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyboard_callback);
glfwSetFramebufferSizeCallback(window, resize_callback);
glfwSetMouseButtonCallback(window, mouse_callback);
glfwSetCursorPosCallback(window, cursor_callback);
resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window))
{
draw();
glfwSwapBuffers(window);
glfwPollEvents();
//glfwWaitEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
glfwDestroyWindow(window);
// clean up and exit
glfwTerminate();
return 0;
}