首先,请注意,此代码中有很多已被弃用。但仅从代码 sn-ps 就更容易理解。我不会为您做所有事情,但我希望通过提供基本工作流程来帮助您入门。
您需要做一些事情才能获得所需的结果。
首先,您必须将纹理加载到视频内存中。这是通过以下方式完成的:
glGenTextures(1, texture_id); //generate a texture object
glBindTexture(GL_TEXTURE_2D, texture_id); //bind the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //set filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //set filters
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_width, texture_height, 0, GL_RGB, GL_UNSIGNED_BYTE, original_image_data); //create the actual texture in video ram
成功后,您可以使用以下方法绘制纹理:
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
//set to ortographic projection
glOrtho(0.0, window_width, 0.0, window_height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D,texture_id);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex2f(-1.0f, 1.0f);
glTexCoord2f(1, 1); glVertex2f( 1.0f, 1.0f);
glTexCoord2f(1, 0); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(0, 0); glVertex2f(-1.0f, -1.0f);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
接下来您需要做的是捕获用户的鼠标输入。如果您在 Windows 上,您可以使用 windowprocedure 回调并查找 WM_MOUSE 事件。如果您使用库进行窗口管理,那么该库可能会提供键盘和鼠标输入功能。
现在你有了鼠标输入,你应该在每次用户按住按钮移动鼠标时在屏幕上画一条线:
glLineWidth(2.5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(mouse_x_start, mouse_y_start);
glVertex2f(mouse_x_end, mouse_y_end);
glEnd();
glColor3f(1.0, 1.0, 1.0);
当上述所有操作顺利时,您应该会在屏幕上看到您的纹理,如果您按住鼠标按钮并移动鼠标,您应该会看到一条红线。你快到了。最后需要做的是读取像素。你可以像这样使用 glReadPixels() 来做到这一点:
void glReadPixels(0, 0, window_width, window_height, GL_RGB, GL_UNSIGNED_BYTE, new_image_data);
你现在有一个字节数组,上面有用户的笔画。我强烈建议您为此过程编写自己的代码,因为我使用的代码已被弃用,并且只应在针对旧平台时使用。工作流程应该保持不变。我希望这足以让你开始。祝你好运!