展示glfw回调函数,鼠标 键盘 窗口等等,输入输出都齐了,以后就可以专心画画了,呵呵~,还有一些窗口最小化的回调函数没写,画个旋转的立方体,慢慢看,有味道哦,哈哈
#define GLEW_STATIC
#include
#include
#include
#include
#include
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);
}