展示glfw回调函数,鼠标 键盘 窗口等等,输入输出都齐了

来源:互联网 发布:h网络是什么意思 编辑:程序博客网 时间:2024/06/05 22:44
展示glfw回调函数,鼠标 键盘 窗口等等,输入输出都齐了,以后就可以专心画画了,呵呵~,还有一些窗口最小化的回调函数没写,画个旋转的立方体,慢慢看,有味道哦,哈哈
#define GLEW_STATIC#include <stdio.h>#include <stdlib.h>#include <string.h>#include <GL/glew.h>#include <GLFW/glfw3.h>GLFWwindow* window;GLint width,height;GLfloat ratio = 1.f;static char msg[128] = {0};GLfloat xpos,ypos;GLfloat fScale     = 1.0f;// set inital scale value to 1.0fGLfloat rquad=0.0f;static void error_callback(int error, const char* description){return;}void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){if (action != GLFW_PRESS)return;switch (key){case GLFW_KEY_ESCAPE:glfwSetWindowShouldClose(window, GL_TRUE);break;default:break;}}void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){if (action == GLFW_PRESS) switch(button){case GLFW_MOUSE_BUTTON_LEFT:strcpy(msg,"Mosue left button clicked!");break;case GLFW_MOUSE_BUTTON_MIDDLE:strcpy(msg,"Mosue middle button clicked!");break;case GLFW_MOUSE_BUTTON_RIGHT:strcpy(msg,"Mosue right button clicked!");break;default:return;}return;}void cursor_position_callback(GLFWwindow* window, double x, double y){sprintf(msg,"Mouse position move to [%d:%d]",int(x),int(y));xpos=float((x-width/2)/width)*2;ypos=float(0-(y-height/2)/height)*2;return;}void scroll_callback(GLFWwindow* window, double x, double y){return;}void framebuffer_size_callback(GLFWwindow* window, int w, int h){if (height==0)  // Prevent A Divide By Zero By{height=1;// Making Height Equal One}if (h > 0)ratio = (float) w / (float) h;glViewport(0, 0, w, h); // Setup viewportwidth = w;height = h;glViewport(0,0,width,height);// Reset The Current ViewportglMatrixMode(GL_PROJECTION);// Select The Projection MatrixglLoadIdentity();// Reset The Projection Matrix// Calculate The Aspect Ratio Of The WindowgluPerspective(45.0f,ratio,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);// Select The Modelview MatrixglLoadIdentity();}void draw_scene(GLFWwindow* window){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glLoadIdentity();glTranslatef(-0.5f,-0.5f,-3.0f);glRotatef(rquad,1.0f,1.0f,1.0f);glBegin(GL_POLYGON);/* f1: front */glColor3f(1.0f,0.0f,0.5f);glNormal3f(-1.0f,0.0f,0.0f);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,0.0f,1.0f);glVertex3f(1.0f,0.0f,0.0f);glEnd();glBegin(GL_POLYGON);/* f2: bottom */glColor3f(1.0f,0.5f,0.0f);glNormal3f(0.0f,0.0f,-1.0f);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(1.0f,0.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glEnd();glBegin(GL_POLYGON);/* f3:back */glColor3f(0.0f,0.5f,1.0f);glNormal3f(1.0f,0.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(1.0f,1.0f,1.0f);glVertex3f(0.0f,1.0f,1.0f);glVertex3f(0.0f,1.0f,0.0f);glEnd();glBegin(GL_POLYGON);/* f4: top */glColor3f(0.5f,0.0f,1.0f);glNormal3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f);glVertex3f(1.0f,0.0f,1.0f);glVertex3f(0.0f,0.0f,1.0f);glVertex3f(0.0f,1.0f,1.0f);glEnd();glBegin(GL_POLYGON);/* f5: left */glColor3f(0.0f,1.0f,0.5f);glNormal3f(0.0f,1.0f,0.0f);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glVertex3f(0.0f,1.0f,1.0f);glVertex3f(0.0f,0.0f,1.0f);glEnd();glBegin(GL_POLYGON);/* f6: right */glColor3f(0.5f,1.0f,0.0f);glNormal3f(0.0f,-1.0f,0.0f);glVertex3f(1.0f,0.0f,0.0f);glVertex3f(1.0f,0.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f);glVertex3f(1.0f,1.0f,0.0f);glEnd();rquad+=0.8f;}void init_opengl(void){glShadeModel(GL_SMOOTH);// Enable Smooth ShadingglClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Black BackgroundglClearDepth(1.0f);// Depth Buffer SetupglEnable(GL_DEPTH_TEST);// Enables Depth TestingglDepthFunc(GL_LEQUAL);// The Type Of Depth Testing To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations}int main( int argc,char *argv[] ){glfwSetErrorCallback(error_callback);if( !glfwInit() ){exit(EXIT_FAILURE);}window = glfwCreateWindow( 640, 480, "opengl tutorial 002-color box", NULL, NULL);if( window == NULL ){glfwTerminate();exit(EXIT_FAILURE);}glfwSetKeyCallback(window, key_callback);glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);glfwSetMouseButtonCallback(window, mouse_button_callback);glfwSetCursorPosCallback(window, cursor_position_callback);glfwSetScrollCallback(window, scroll_callback);glfwMakeContextCurrent(window);glfwGetFramebufferSize(window, &width, &height);framebuffer_size_callback(window, width, height);glewExperimental = true; // Needed for core propbmpfileif (glewInit() != GLEW_OK){exit(EXIT_FAILURE);}//initialize openglinit_opengl();while(!glfwWindowShouldClose(window)){draw_scene(window);glfwSwapBuffers(window);glfwPollEvents();}glfwTerminate();exit(EXIT_SUCCESS);}


0 0