展示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
- 展示glfw回调函数,鼠标 键盘 窗口等等,输入输出都齐了
- 我设计的glfw按键检测类和其回调函数
- 键盘回调函数的使用
- 鼠标回调函数的使用
- 鼠标回调函数和模式
- CentOS6.8鼠标、键盘、SSH都不能用了!
- 我们都从spring学到了什么? 系列一 之 回调函数的使用
- OpenCV - 鼠标操作(鼠标回调函数)
- spring中使用了回调函数
- 键盘钩子回调函数中的lParam & 0x80000000
- visual C++ 调用颜色对话框,鼠标的回调函数
- Open CV 学习笔记:鼠标操作(回调函数)
- OpenCV2鼠标回调函数画矩形和圆
- OpenCV学习笔记_鼠标回调函数
- python-opencv 关于鼠标的回调函数
- OpenCV学习笔记之鼠标响应回调函数setMouseCallback
- c语言回调函数例子,看了就明白什么是回调函数了
- 用类的成员函数作为窗口回调函数
- UML
- 辛星漫谈软件思想
- 前端工程师面试题总结 - JavaScript(1)
- 云编程那些事2 - Java之父的啤酒密谋
- 前端工程师面试题总结 - Coding(1)
- 展示glfw回调函数,鼠标 键盘 窗口等等,输入输出都齐了
- how to convert a object to json file immediately during debug?
- 最小正子串问题
- 最长回文子串
- C++11 Async cURL
- 辛星浅谈大型网站构架优化思路之一简化
- Leetcode: Same Tree
- python 笔记
- 辛星浅谈大型网站架构思路之二分解