0

这是代码

  #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',它不能在光标的位置绘制一个点,我不知道为什么它不能工作。

这是第 2 章中源代码来自http://www.4twk.com/shill/3rd-edition.html 的链接,第 2 章 - 绘图图形入门 - zip 文件

4

2 回答 2

1

继续对显示功能进行绘图操作。不要从事件处理程序调用 OpenGL 绘图调用。在事件处理程序中设置一个变量,该变量将在显示函数中绘制点,然后使用glutPostRedisplay发出重绘。

于 2013-06-30T09:37:33.073 回答
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 );
}
于 2013-07-05T05:43:48.120 回答