Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // glfw_30.cpp : Defines the entry point for the console application.
- // http://www.glfw.org/docs/latest/quick.html
- #include "stdafx.h"
- #define _CRT_SECURE_NO_DEPRECATE
- #include <time.h>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <vector>
- #include <Windows.h>
- #include <GL/glut.h>
- #include <GL/GL.h>
- #define SCREEN_WIDTH 1000
- #define SCREEN_HEIGHT 1000
- #define PC_MODE 16
- #define CONSOLE_MODE 0
- #define MAXRAD 4
- #define MINRAD 1
- #define MAXHEIGHT 7
- #define MINHEIGHT 1
- #define MAXFREQ_X 40
- #define MINFREQ_X 3
- #define ANGLE 2
- typedef struct {
- GLfloat x, y, z;
- } coord;
- std::vector<std::vector<coord>> cone_top;
- std::vector<std::vector<coord>> cone_bot;
- std::vector<std::vector<coord>> cone_side;
- std::vector<std::vector<coord>> cyl_top;
- std::vector<std::vector<coord>> cyl_bot;
- std::vector<std::vector<coord>> cyl_side;
- bool twinkie_flag, tex_flag, light_flag;
- int type, stop_i, stop_j, freq_x, freq_y;
- float t, twinkie_steps = 100, dt = 1 / twinkie_steps;
- static float alpha, beta, gamma, h, r;
- static GLuint texture;
- static void error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- #define BITMAP_ID 0x4D42
- BITMAPINFOHEADER bitmapInfoHeader; // temp bitmap info header
- BITMAPINFOHEADER landInfo; // land texture info header
- unsigned char* imageData; // the map image data
- unsigned char* landTexture; // land texture data
- unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
- {
- FILE *filePtr; // the file pointer
- BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
- unsigned char *bitmapImage; // bitmap image data
- int imageIdx = 0; // image index counter
- unsigned char tempRGB; // swap variable
- // open filename in "read binary" mode
- filePtr = fopen(filename, "rb");
- if (filePtr == NULL)
- return NULL;
- // read the bitmap file header
- fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
- // verify that this is a bitmap by checking for the universal bitmap id
- if (bitmapFileHeader.bfType != BITMAP_ID)
- {
- fclose(filePtr);
- return NULL;
- }
- // read the bitmap information header
- fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
- // move file pointer to beginning of bitmap data
- fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
- // allocate enough memory for the bitmap image data
- bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
- // verify memory allocation
- if (!bitmapImage)
- {
- free(bitmapImage);
- fclose(filePtr);
- return NULL;
- }
- // read in the bitmap image data
- fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
- // make sure bitmap image data was read
- if (bitmapImage == NULL)
- {
- fclose(filePtr);
- return NULL;
- }
- // swap the R and B values to get RGB since the bitmap color format is in BGR
- for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
- {
- tempRGB = bitmapImage[imageIdx];
- bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
- bitmapImage[imageIdx + 2] = tempRGB;
- }
- // close the file and return the bitmap image data
- fclose(filePtr);
- return bitmapImage;
- }
- /*
- GLuint raw_texture_load(const char *filename, int width, int height)
- {
- GLuint texture;
- unsigned char *data;
- FILE *file;
- // open texture data
- file = fopen(filename, "rb");
- if (file == NULL) return 0;
- // allocate buffer
- data = (unsigned char*)malloc(width * height * 4);
- // read texture data
- fread(data, width * height * 4, 1, file);
- fclose(file);
- // allocate a texture name
- glGenTextures(1, &texture);
- // select our current texture
- glBindTexture(GL_TEXTURE_2D, texture);
- // select modulate to mix texture with color for shading
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
- // when texture area is small, bilinear filter the closest mipmap
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
- // when texture area is large, bilinear filter the first mipmap
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // texture should tile
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // build our texture mipmaps
- gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
- // free buffer
- free(data);
- return texture;
- } */
- void free_vect(std::vector<std::vector<coord>>& vect)
- {
- for (int i = 0; i < freq_y + 1; i++) {
- vect[i].clear();
- vect[i].shrink_to_fit();
- }
- vect.clear();
- vect.shrink_to_fit();
- }
- void count_cylinder()
- {
- int i, j, rad = r;
- float angle, dangle, height, dheight, drad0;
- height = h;
- dheight = height / (float)freq_y;
- dangle = 2 * M_PI / (float)freq_x;
- drad0 = rad / (float)freq_y;
- cyl_top.resize(freq_y + 1);
- for (i = 0; i < freq_y + 1; i++) cyl_top[i].resize(freq_x + 1);
- cyl_bot.resize(freq_y + 1);
- for (i = 0; i < freq_y + 1; i++) cyl_bot[i].resize(freq_x + 1);
- cyl_side.resize(freq_y + 1);
- for (i = 0; i < freq_y + 1; i++) cyl_side[i].resize(freq_x + 1);
- for (i = 0; i < freq_y + 1; i += 1) {
- for (j = 0, angle = 0; angle <= 2 * M_PI + dangle; j += 1, angle += dangle) {
- cyl_top[i][j].x = drad0 * i * cos(angle);
- cyl_top[i][j].y = height;
- cyl_top[i][j].z = drad0 * i * sin(angle);
- cyl_side[i][j].x = rad * cos(angle);
- cyl_side[i][j].y = height - dheight * i;
- cyl_side[i][j].z = rad * sin(angle);
- cyl_bot[i][j].x = drad0 * i * cos(angle);
- cyl_bot[i][j].y = 0;
- cyl_bot[i][j].z = drad0 * i * sin(angle);
- }
- }
- stop_i = i;
- stop_j = j;
- printf("cyl_renewed: h=%.2f, r=%d, fr_x=%d, fr_y=%d s_i=%d s_j=%d\n", height, rad, freq_x, freq_y, stop_i, stop_j);
- }
- void count_cone()
- {
- int i, j, top_rad = r, bot_rad = r * 2;
- float angle, dangle, height, dheight, drad0, drad1, drad2;
- height = h;
- dheight = height / (float)freq_y;
- dangle = 2 * M_PI / (float)freq_x;
- drad0 = top_rad / (float)freq_y;
- drad1 = (bot_rad - top_rad) / (float)freq_y;
- drad2 = bot_rad / (float)freq_y;
- cone_top.resize(freq_y + 1);
- for (i = 0; i < freq_y + 1; i++) cone_top[i].resize(freq_x + 1);
- cone_bot.resize(freq_y + 1);
- for (i = 0; i < freq_y + 1; i++) cone_bot[i].resize(freq_x + 1);
- cone_side.resize(freq_y + 1);
- for (i = 0; i < freq_y + 1; i++) cone_side[i].resize(freq_x + 1);
- for (i = 0; i < freq_y + 1; i += 1) {
- for (j = 0, angle = 0; angle <= 2 * M_PI + dangle; j += 1, angle += dangle) {
- cone_top[i][j].x = drad0 * i * cos(angle);
- cone_top[i][j].y = height;
- cone_top[i][j].z = drad0 * i * sin(angle);
- cone_side[i][j].x = (top_rad + drad1 * i) * cos(angle);
- cone_side[i][j].y = height - dheight * i;
- cone_side[i][j].z = (top_rad + drad1 * i) * sin(angle);
- cone_bot[i][j].x = drad2 * i * cos(angle);
- cone_bot[i][j].y = 0;
- cone_bot[i][j].z = drad2 * i * sin(angle);
- }
- }
- stop_i = i;
- stop_j = j;
- printf("cone_renewed: h=%.2f, top_r=%d, fr_x=%d, fr_y=%d s_i=%d s_j=%d\n", height, top_rad, freq_x, freq_y, stop_i, stop_j);
- }
- void draw_figure(
- std::vector<std::vector<coord>>& top,
- std::vector<std::vector<coord>>& side,
- std::vector<std::vector<coord>>& bot
- )
- {
- int i, j;
- glRotatef(alpha, 1, 0, 0);
- glRotatef(beta, 0, 1, 0);
- glRotatef(gamma, 0, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- for (i = 0; i < stop_i-1; i += 1) {
- for (j = 0; j < stop_j-1; j += 1) {
- //top
- glBegin(GL_POLYGON);
- glColor3f(0.5, 0.5, 0.5);
- glVertex3f(top[i][j].x, top[i][j].y, top[i][j].z);
- glColor3f(0, 0, 0);
- glVertex3f(top[i][j + 1].x, top[i][j + 1].y, top[i][j + 1].z);
- glVertex3f(top[i + 1][j + 1].x, top[i + 1][j + 1].y, top[i + 1][j + 1].z);
- glVertex3f(top[i + 1][j].x, top[i + 1][j].y, top[i + 1][j].z);
- glEnd();
- //bot
- glBegin(GL_POLYGON);
- glColor3f(0.3, 0.3, 0.3);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(bot[i][j].x, bot[i][j].y, bot[i][j].z);
- glColor3f(0, 0, 0);
- glTexCoord2f(0.0f, 1.0f);
- glVertex3f(bot[i][j + 1].x, bot[i][j + 1].y, bot[i][j + 1].z);
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(bot[i + 1][j + 1].x, bot[i + 1][j + 1].y, bot[i + 1][j + 1].z);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(bot[i + 1][j].x, bot[i + 1][j].y, bot[i + 1][j].z);
- glEnd();
- //sidelines
- glBegin(GL_POLYGON);
- glColor3f(0, 0, 0);
- glVertex3f(side[i][j].x, side[i][j].y, side[i][j].z);
- glColor3f(0.8, 0, 0);
- glVertex3f(side[i][j + 1].x, side[i][j + 1].y, side[i][j + 1].z);
- glColor3f(0, 0.8, 0);
- glVertex3f(side[i + 1][j + 1].x, side[i + 1][j + 1].y, side[i + 1][j + 1].z);
- glColor3f(0, 0, 0.8);
- glVertex3f(side[i + 1][j].x, side[i + 1][j].y, side[i + 1][j].z);
- glEnd();
- }
- }
- }
- void tweenking()
- {
- int i, j, c;
- GLfloat ai, bi;
- //Bezier cube:
- //(1-t)^3*p0 + 3t(1-t)^2*p1 + 3*t^2*(1-t)*p2 + t^3*p3
- printf("twinkie\n");
- for (t = 0, c = 0; c < 20; t += dt, c++) {
- for (i = 0; i < stop_i; i += 1) {
- for (j = 0; j < stop_j; j += 1) {
- ai = cone_bot[i][j].y;
- bi = cone_bot[i][j].y * 2;
- cone_top[i][j].x = cone_top[i][j].x;
- cone_top[i][j].y = cone_top[i][j].y;
- cone_top[i][j].z = cone_top[i][j].z ;
- cone_bot[i][j].x = cone_bot[i][j].x;
- cone_bot[i][j].y = (1 - t*t)*ai + t*t*bi;
- cone_bot[i][j].z = cone_bot[i][j].z;
- cone_side[i][j].x = cone_side[i][j].x;
- cone_side[i][j].y = cone_side[i][j].y;
- cone_side[i][j].z = cone_side[i][j].z;
- }
- }
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw_figure(cone_top, cone_side, cone_bot);
- Sleep(10);
- }
- printf("twinkie stop\n");
- }
- void set_tex()
- {
- landTexture = LoadBitmapFile("photo.bmp", &landInfo);
- //if (!landTexture)
- // return false;
- glTexEnvf(GL_FRONT_AND_BACK, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, landInfo.biHeight, landInfo.biWidth, GL_RGB, GL_UNSIGNED_BYTE, landTexture);
- }
- void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- switch (key) {
- case (GLFW_KEY_SPACE) :
- if (action == GLFW_PRESS) {
- alpha = 0;
- beta = 0;
- gamma = 0;
- h = 2;
- r = 1;
- freq_x = 3;
- freq_y = 3;
- count_cone();
- count_cylinder();
- }
- break;
- case (GLFW_KEY_UP) :
- if (action == GLFW_PRESS) {
- if (h <= MAXHEIGHT) {
- h += 2;
- freq_y++;
- count_cone();
- count_cylinder();
- }
- }
- break;
- case (GLFW_KEY_DOWN) :
- if (action == GLFW_PRESS) {
- if (h > MINHEIGHT) {
- h -= 2;
- freq_y--;
- count_cone();
- count_cylinder();
- }
- }
- break;
- case (GLFW_KEY_RIGHT) :
- if (action == GLFW_PRESS) {
- if (r < MAXRAD) {
- r++;
- count_cone();
- count_cylinder();
- }
- }
- break;
- case (GLFW_KEY_LEFT) :
- if (action == GLFW_PRESS) {
- if (r > MINRAD) {
- r--;
- count_cone();
- count_cylinder();
- }
- }
- break;
- case (GLFW_KEY_R) :
- if (action == GLFW_PRESS) {
- if (freq_x <= MAXFREQ_X) {
- freq_x++;
- count_cone();
- count_cylinder();
- }
- }
- break;
- case (GLFW_KEY_F) :
- if (action == GLFW_PRESS) {
- if (freq_x > MINFREQ_X) {
- freq_x--;
- count_cone();
- count_cylinder();
- }
- }
- break;
- case (GLFW_KEY_V) :
- if (action == GLFW_PRESS) {
- if (type == 1) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- if (type == -1) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- }
- break;
- case (GLFW_KEY_T) :
- if (action == GLFW_PRESS) {
- twinkie_flag = !twinkie_flag;
- tweenking();
- }
- break;
- case (GLFW_KEY_Y) :
- if (action == GLFW_PRESS) {
- tex_flag = !tex_flag;
- if (tex_flag) {
- glEnable(GL_TEXTURE_2D);
- }
- else {
- glDisable(GL_TEXTURE_2D);
- }
- }
- break;
- }
- }
- GLfloat cabinet_view_matrix[] = {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- -0.5*cos(M_PI / 6), -0.5*sin(M_PI / 6), -1, 0,
- 0, 0, 0, 1
- };
- /*
- void animation_compression(double k){
- int i, j;
- double t;
- int counter;
- GLdouble ayc, byc, ayf, byf;
- long x;
- for (t = 0, counter = 20; counter >= 0; t += STEP, counter--){
- for (i = 0; i <= ACCURACY; i++){
- for (j = 0; j < ACCURACY; j++){
- ayc = my_cone[i][j].y; ayf = my_foundament[i][j].y;
- byc = my_cone[i][j].y * k; byf = my_foundament[i][j].y * k;
- my_cone[i][j].y = (1 - t*t)*ayc + t*t*byc;
- my_foundament[i][j].y = (1 - t*t)*ayf + t*t*byf;
- }
- }
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw();
- Sleep(20);
- }
- }*/
- void display(GLFWwindow* window)
- {
- tex_flag = false;
- twinkie_flag = false;
- t = 0;
- freq_x = 3;
- freq_y = 3;
- type = -1;
- h = 3;
- r = 1;
- alpha = 0;
- beta = 0;
- gamma = 0;
- count_cone();
- count_cylinder();
- imageData = LoadBitmapFile("photo.bmp", &bitmapInfoHeader);
- set_tex();
- while (!glfwWindowShouldClose(window))
- {
- glClearColor(0.4, 0.4, 0.4, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) alpha -= ANGLE;
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) alpha += ANGLE;
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) beta -= ANGLE;
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) beta += ANGLE;
- if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) gamma -= ANGLE;
- if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) gamma += ANGLE;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glLoadMatrixf(cabinet_view_matrix);
- glOrtho(-10, 10, -10, 10, 10, -10);
- if (twinkie_flag) tweenking();
- else draw_figure(cone_top, cone_side, cone_bot);
- glfwSwapBuffers(window);
- //glfwWaitEvents();
- glfwPollEvents();
- }
- }
- int main(int argc, char** argv)
- {
- // initialise GLFW
- if (!glfwInit())
- {
- printf("glfwInit failed\n");
- return -1;
- }
- glfwWindowHint(GLFW_SAMPLES, PC_MODE);
- glfwSetErrorCallback(error_callback);
- //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
- //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
- GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
- if (window == NULL)
- {
- printf("glfwOpenWindow failed.\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, controls);
- //glDepthFunc(GL_LEQUAL);
- glEnable(GL_DEPTH_TEST);
- GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 0.7f };
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
- glEnable(GL_LIGHT0);
- GLfloat lightPos[] = { -50.0f, 50.0f, 100.0f, 1.0f };
- glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
- //glLightModelf(GL_LIGHT_MODEL_AMBIENT, ambientLight);
- if (NULL != window)
- {
- display(window);
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement