【发布时间】:2012-12-06 22:33:04
【问题描述】:
我正在尝试在 OpenGL 窗口中绘制一个 50x50 的网格。我绘制网格的代码是
void GLGrid::draw() {
int y=-width;
int yIncrement = width / 50;
int x=-length;
int xIncrement = length / 50;
glColor3f(0.0f,0.0f,0.0f);
for(y = -width; y < width; y+=yIncrement) {
glBegin(GL_LINES);
glVertex3f(-width,y,0);
glVertex3f(width,y,0);
glEnd();
}
for(x = -length; x < length; x+=xIncrement) {
glBegin(GL_LINES);
glVertex3f(-length,x,0);
glVertex3f(length,x,0);
glEnd();
}
}
请注意,在我执行 x=0;x
我唯一看到的是屏幕中间的一条水平线。我认为问题是我不知道我的窗口大小实际上是多少。每当我用
点击时打印出来static void mouseEvent(int button, int state, int x, int y) {
cout<<"\nMouse Event!";
cout<<"\n\tbutton:"<<button;
cout<<"\n\tstate:"<<state;
cout<<"\n\tx:"<<x;
cout<<"\n\ty:"<<y<<"\n";
}
打印出来左上角是0,0,右下角是300,300。所以我将我的 GLGrid 长度和宽度设置为 300。我应该将窗口长度和宽度设置为其他值吗?如果是这样,是什么?我对OpenGL很陌生,所以请原谅我的无知。为了彻底,因为我不知道是否有其他微妙的问题可能是问题,我将包含更多代码
static void initOpenGL() {
//set clear color to white
glClearColor(0.0f,0.0f,0.0f,1.0f);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
/*OpenGL calls*/
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if (!init)
{
initOpenGL();
}
renderScene();
//...more code below in this function but pretty positive its irrelevant
static void renderScene() {
drawBackground();
drawGrid();
}
static void drawBackground() {
//draw a white rectangle for background
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-windowMaxX, -windowMaxY, 0);
glVertex3f(windowMaxX, -windowMaxY, 0);
glVertex3f(windowMaxX, windowMaxY, 0);
glVertex3f(-windowMaxX, windowMaxY, 0);
glEnd();
}
static void drawGrid() {
GLGrid.draw();
}
【问题讨论】:
-
你用什么来获取鼠标事件?你有没有打过
glMultMatrix、glScale或glTranslate的电话? -
我只是在做 glutMouseFunc (mouseEvent);调用该函数。我不做任何多矩阵、缩放或翻译,调用..
标签: opengl grid coordinates