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"
- #include <time.h>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #define SCREEN_WIDTH 1000
- #define SCREEN_HEIGHT 1000
- #define PC_MODE 16
- #define CONSOLE_MODE 0
- #define ANGLE 2
- void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (action == GLFW_PRESS)
- if (key == GLFW_KEY_ESCAPE)
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- static void error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- void draw_cube(int type, int size, int angle_x, int angle_y, int angle_z)
- {
- glRotatef(angle_x, 1, 0, 0);
- glRotatef(angle_y, 0, 1, 0);
- glRotatef(angle_z, 0, 0, 1);
- if (type == 1) glBegin(GL_QUADS);
- if (type == -1) glBegin(GL_LINE_LOOP);
- //BOTTOM
- glColor3f(0, 0, 0);
- glVertex3f(-size, -size, size);
- glVertex3f(size, -size, size);
- glVertex3f(size, -size, -size);
- glVertex3f(-size, -size, -size);
- glEnd();
- if (type == 1) glBegin(GL_QUADS);
- if (type == -1) glBegin(GL_LINE_LOOP);
- //TOP
- glColor3f(1, 0, 0);
- glVertex3f(-size, size, size);
- glVertex3f(size, size, size);
- glVertex3f(size, size, -size);
- glVertex3f(-size, size, -size);
- glEnd();
- if (type == 1) glBegin(GL_QUADS);
- if (type == -1) glBegin(GL_LINE_LOOP);
- //BACK
- glColor3f(1, 0, 1);
- glVertex3f(-size, -size, -size);
- glVertex3f(size, -size, -size);
- glVertex3f(size, size, -size);
- glVertex3f(-size, size, -size);
- glEnd();
- if (type == 1) glBegin(GL_QUADS);
- if (type == -1) glBegin(GL_LINE_LOOP);
- //FRONT
- glColor3f(0, 0, 1);
- glVertex3f(-size, -size, size);
- glVertex3f(size, -size, size);
- glVertex3f(size, size, size);
- glVertex3f(-size, size, size);
- glEnd();
- if (type == 1) glBegin(GL_QUADS);
- if (type == -1) glBegin(GL_LINE_LOOP);
- //LEFT
- glColor3f(1, 1, 0);
- glVertex3f(-size, -size, size);
- glVertex3f(-size, -size, -size);
- glVertex3f(-size, size, -size);
- glVertex3f(-size, size, size);
- glEnd();
- if (type == 1) glBegin(GL_QUADS);
- if (type == -1) glBegin(GL_LINE_LOOP);
- //RIGHT
- glColor3f(1, 1, 1);
- glVertex3f(size, -size, size);
- glVertex3f(size, -size, -size);
- glVertex3f(size, size, -size);
- glVertex3f(size, size, size);
- glEnd();
- }
- void draw_cone(int type, int top_rad, int bot_rad, int height, int smoothness, int angle_x, int angle_y, int angle_z)
- {
- glRotatef(angle_x, 1, 0, 0);
- glRotatef(angle_y, 0, 1, 0);
- glRotatef(angle_z, 0, 0, 1);
- if (smoothness <= 3)
- smoothness = 3;
- float angle;
- //top
- if (type == 1)
- glBegin(GL_POLYGON);
- if (type == -1)
- glBegin(GL_LINE_LOOP);
- glColor3f(0.8, 0.8, 0.8);
- for (angle = 0; angle <= 2 * M_PI; angle += 2 * M_PI / smoothness) {
- glVertex3f(top_rad*sin(angle), height / 2, top_rad*cos(angle));
- }
- glEnd();
- //bot
- if (type == 1)
- glBegin(GL_POLYGON);
- if (type == -1)
- glBegin(GL_LINE_LOOP);
- glColor3f(0, 0, 0);
- for (angle = 0; angle <= 2 * M_PI; angle += 2 * M_PI / smoothness) {
- glVertex3f(bot_rad*sin(angle), -height / 2, bot_rad*cos(angle));
- }
- glEnd();
- //sidelines
- if (type == 1)
- glBegin(GL_QUAD_STRIP);
- if (type == -1)
- glBegin(GL_LINES);
- for (angle = 0; angle <= 2 * M_PI + 2*M_PI/smoothness; angle += 2 * M_PI / smoothness) {
- glColor3f(0.7, 0.7, 0.7);
- glVertex3f(top_rad*sin(angle), height / 2, top_rad*cos(angle));
- glColor3f(0.2, 0.2, 0.2);
- glVertex3f(bot_rad*sin(angle), -height/2, bot_rad*cos(angle));
- }
- glEnd();
- }
- void draw_axis()
- {
- glBegin(GL_LINES);
- //X
- glColor3f(1, 0, 0);
- glVertex3f(-100, 0, 0);
- glVertex3f(100, 0, 0);
- //Y
- glColor3f(0, 1, 0);
- glVertex3f(0, -3, 0);
- glVertex3f(0, 100, 0);
- //Z
- glColor3f(0, 0, 1);
- glVertex3f(0, 0, -1);
- glVertex3f(0, 0, 100);
- glEnd();
- }
- void draw_net()
- {
- int i;
- float j;
- for (i = 0; i < 20; i++) {
- glColor3f(0, 0.7, 0);
- for (j = -10; j <= 10; j ++) {
- glBegin(GL_LINES);
- glVertex3f(j, -3, 10);
- glVertex3f(j, -3, -10);
- glEnd();
- }
- for (j = -10; j <= 10; j+=2) {
- glBegin(GL_LINES);
- glVertex3f(10, -3, j);
- glVertex3f(-10, -3, j);
- glEnd();
- }
- }
- }
- void display(GLFWwindow* window)
- {
- int smoothness, type;
- static float alpha, beta, gamma, a, b, c, h, r;
- alpha = 0;
- beta = 0;
- gamma = 0;
- smoothness = 3;
- type = -1;
- h = 2;
- r = 1;
- 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
- }; //0.5*cos(M_PI/6), 0.5*sin(M_PI/6), 0, 0,
- 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_SPACE) == GLFW_PRESS) {
- alpha = 0;
- beta = 0;
- gamma = 0;
- h = 2;
- r = 1;
- }
- 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;
- }
- if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
- if (h <= 18) h++;
- }
- if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) {
- if (h > 2) h--;
- }
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
- if (r < 10) r++;
- }
- if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
- if (r > 1) r--;
- }
- if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
- if (smoothness <= 50) smoothness++;
- }
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
- if (smoothness > 3) smoothness--;
- }
- if (glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS) {
- type = -type;
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glLoadMatrixf(cabinet_view_matrix);
- //gluPerspective(60, SCREEN_WIDTH / SCREEN_HEIGHT, 0, 100);
- glOrtho(-10, 10, -10, 10, 10, -10);
- //gluLookAt(5, 5, 5, 0, 0, 0, 0, 1, 0);
- //DO I REALLY NEED THIS???????????????
- //glMatrixMode(GL_MODELVIEW);
- //glLoadIdentity();
- //DO I REALLY NEED THIS???????????????
- draw_net();
- draw_axis();
- glTranslatef(-4, 0, 0);
- draw_cube(type, 2, 0, 0, 0);
- glTranslatef(8, 0, 0);
- //draw_cube(type, 2, alpha, beta, gamma);
- draw_cone(type, r, r*2, h, smoothness, alpha, beta, gamma);
- glfwSwapBuffers(window);
- glfwWaitEvents();
- }
- }
- 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);
- if (NULL != window)
- {
- display(window);
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement