[英]OpenGL translate not working
好的,所以我的程序打開一個文件,讀取xyz點,然后從中繪制一條線帶。 我最初是用SharpGL(作為WPF窗口實現)編寫此程序的,並且可以運行,但是由於使用立即模式而不能很好地工作,因此我已使用C ++移植到OpenGL。 我(有些)想出了VBO,現在我想嘗試添加鼠標功能。 我的問題是我無法用鼠標移動圖片,我希望能夠單擊並“拖動”圖片。 我的mouseClickFunc和mouseMotion工作(我的cout語句執行),但是似乎我的翻譯調用從未執行過(即,圖片在場景中開始部分“剪切”,我希望能夠將其拖動並居中)。 我知道這是黑暗中的一槍,但我真的不確定該怎么做。
MotionFunc:
void mouseMotion(int x, int y)
{
if (moveable)
{
xMove += xTransform(x) - xTransform(xDown);
yMove += yTransform(y) - yTransform(yDown);
xDown = x;
yDown = y;
cout << yMove << "---" << xMove << endl;
glutSwapBuffers();
glutPostRedisplay();
}
}
顯示功能
void RenderFunction(void)
{
++FrameCount;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
// Reset the modelview matrix.
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
我確定還有更多代碼需要顯示,這就是我認為問題所在的位置。 任何幫助將不勝感激。
這是我看到的控制台輸出和屏幕(OpenGL上下文)的圖片。
更新:更新了我的代碼。 看起來我的坐標在移動,但是如果這樣的話,圖片就不行了。 如果您查看我的輸出,如果我一直“拖動”圖片,您會在控制台中看到變量xMove和yMove可以根據需要變大或變小,再次轉換就是永遠不會移動它。
您在繪制事物之前彈出矩陣,這會將矩陣重置為上一個推送矩陣的狀態。 移動glPopMatrix();
在抽獎電話下方
在調用glDrawArrays()
之前先彈出矩陣,因此自然會抵消轉換的效果。 它還會否定glOrtho()
調用,但是該調用應該在投影矩陣上發出,而不是在模型視圖矩陣上發出。
而且,當然,問題出在您的代碼中,而不是OpenGL中。
在此代碼摘錄中:
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
glPopMatrix();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
你是 :
因此,您的翻譯將被忽略。
這是正確的操作:
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
glPopMatrix();
您在轉換之前要乘以投影,請記住要始終閱讀應升級IMO的OpenGL 1.1中從底部到頂部的矩陣轉換。 另一個問題是您在繪制之前彈出矩陣。
正確的代碼:
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
// Reset the modelview matrix.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
glPopMatrix();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.