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

展示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.0f
GLfloat 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 viewport
	width = w;
	height = h;
	glViewport(0,0,width,height);						// Reset The Current Viewport
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,ratio,0.1f,100.0f);
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();
}
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 Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(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 propbmpfile
	if (glewInit() != GLEW_OK)
		{
			exit(EXIT_FAILURE);
		}
	//initialize opengl
	init_opengl();

	while(!glfwWindowShouldClose(window))
		{
			draw_scene(window);
			glfwSwapBuffers(window);
			glfwPollEvents();
		}
	glfwTerminate();
	exit(EXIT_SUCCESS);
}



  • 11
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值