这是代码
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>
#include <windows.h>
using namespace std;
struct GLintPoint {
GLint x, y;
};
void drawDot(GLint x, GLint y)
{
glBegin (GL_POINTS);
glVertex2i(x,y);
glEnd();
}
int screenWidth = 640, screenHeight = 480;
void myDisplay() {
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 1.0f, 1.0f, 1.0f );
}
/*
if( selected ) {
glBegin( GL_QUADS );
glVertex2i( corner[0].x, corner[0].y );
glVertex2i( corner[0].x, corner[1].y );
glVertex2i( corner[1].x, corner[1].y );
glVertex2i( corner[1].x, corner[0].y );
glEnd();
}
glutSwapBuffers();
*/
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
GLint x = mouseX;
GLint y = screenHeight - mouseY; // flip the y value as always
switch(theKey)
{
case 'p':
drawDot(x, y); // draw a dot at the mouse position
break;
case 'E':
exit(-1); //terminate the program
default:
break; // do nothing
}
glutPostRedisplay();
}
void myMouse(int button, int state, int x, int y) {}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
// initialize window
glutInitWindowSize( screenWidth, screenHeight );
glutInitWindowPosition( 0, 0 );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
// create window
glutCreateWindow( "Rubber Rect Demo" );
// set the projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, screenWidth, 0, screenHeight );
glMatrixMode( GL_MODELVIEW );
// clear rendering surface
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // background is black
glViewport(0, 0, screenWidth, screenHeight);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutKeyboardFunc(myKeyboard);
// glutPassiveMotionFunc( myPassiveMotion );
glutMainLoop();
return( 0 );
}
编译运行后,按'P',无法在光标位置绘制一个点,我不知道为什么不能正常工作。
这里是源代码来自http://www.4twk.com/shill/3rd-edition.html的链接 在第2章,第2章 - 入门绘图图 - zip文件
答案 0 :(得分:1)
将绘图操作保持为显示功能。 不要从事件处理程序调用OpenGL绘图调用。在事件处理程序中设置一个变量,该变量将在显示函数中进行点绘制,然后使用 glutPostRedisplay 发出重绘。
答案 1 :(得分:0)
试一试:
#include <GL/glut.h>
#include <vector>
using namespace std;
struct GLintPoint
{
GLint x, y;
};
vector< GLintPoint > points;
void myDisplay()
{
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
glViewport(0, 0, w, h);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // background is black
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 1.0f, 1.0f, 1.0f );
glBegin (GL_POINTS);
for( size_t i = 0; i < points.size(); ++i )
{
glVertex2i( points[i].x, points[i].y );
}
glEnd();
glutSwapBuffers();
}
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
int h = glutGet( GLUT_WINDOW_HEIGHT );
GLint x = mouseX;
GLint y = h - mouseY; // flip the y value as always
switch(theKey)
{
case 'p':
{
GLintPoint temp;
temp.x = x;
temp.y = y;
points.push_back( temp );
glutPostRedisplay();
}
break;
case 'E':
exit(-1); //terminate the program
default:
break; // do nothing
}
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
// initialize window
glutInitWindowSize( 640, 480 );
glutInitWindowPosition( 0, 0 );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
// create window
glutCreateWindow( "Rubber Rect Demo" );
glutDisplayFunc( myDisplay );
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return( 0 );
}