這裏是代碼我想在屏幕上畫一個點,但萬一我按下「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文件