2013-06-30 36 views
0

這裏是代碼我想在屏幕上畫一個點,但萬一我按下「P」,但它不能正常工作

#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」,它不能在光標的位置繪製一個點,我不知道爲什麼它不能工作。

這裏是鏈接在源代碼comefrom在第2章,第2章http://www.4twk.com/shill/3rd-edition.html - 入門附圖 - zip文件

回答

1

保持繪圖操作的顯示功能。 不要從事件處理函數調用OpenGL繪圖調用。在事件處理函數中設置一個變量,該變量將在顯示函數中繪製點,然後使用glutPostRedisplay發出重繪。

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); 
} 
相關問題