Я новичок в OpenGl, и я немного борюсь с настройкой камеры glOrtho, чтобы соответствовать размеру окна, чтобы я мог рисовать линию, используя координаты окна. Например, если я хочу нарисовать линию из координат 0,10 (x, y) до 600,10. Мне удалось нарисовать линию (которая будет «Сепаратором» из окна просмотра и панель инструментов с кнопками) в моей текущей настройке, но это было под «попыткой завершения ошибки», и координаты, которые мне нужно было поставить, не делают любой смысл для меня. Когда я попытался провести линию, используя вышеупомянутые координаты, линия просто не отображалась. Что мне нужно изменить в glOrtho настройки для того, чтобы работать с этими (1000x600) размер экрана и рисовать свои вершины, а не этим:OpenGL glOrtho настроен соответствовать размеру окна
glVertex3f(-2.0, 11.0, 0.0);
glVertex3f(20.0, 11.0, 0.0);
Обратите внимание, мой текущий размер окна составляет 1000x600 (ширина/высота)
Это линия (на вершине, которая пересекает весь экран):
Это мой OGWindow класс, который обрабатывает все чертежа:
void OGWindow::MyReSizeGLScene(int fwidth, int fheight)
{
// Store window size in class variables so it can be accessed in myDrawGLScene() if necessary
wWidth = fwidth;
wHeight = fheight;
// Calculate aspect ration of the OpenGL window
aspect_ratio = (float) fwidth/fheight;
// Set camera so it can see a square area of space running from 0 to 10
// in both X and Y directions, plus a bit of space around it.
Ymin = -1;
Ymax = 12;
Xmin = -1;
// Choose Xmax so that the aspect ration of the projection
// = the aspect ratio of the viewport
Xmax = (aspect_ratio * (Ymax -Ymin)) + Xmin;
glMatrixMode(GL_PROJECTION); // Select The Projection Stack
glLoadIdentity();
glOrtho(Xmin, Xmax, Ymin, Ymax, -1.0, 1.0);
glViewport(0, 0, wWidth, wHeight); // Viewport fills the window
}
void OGWindow::myDrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the drawing area
OGWindow::myDrawModel();
drawToolbar();
glutSwapBuffers(); // Needed if we're running an animation
glFlush();
}
void OGWindow::myDrawModel(GLvoid)
{
switch (squareColour) {
case RED:
glColor3f(1.0, 0.0, 0.0);
break;
case BLUE:
glColor3f(0.0, 0.0, 1.0);
break;
}
glBegin(GL_QUADS);
glVertex3f(squareX, squareY, 0.0); // Coordinates of bottom-left corner of square
glVertex3f(squareX + squareWidth, squareY, 0.0);
glVertex3f(squareX + squareWidth, squareY + squareHeight, 0.0);
glVertex3f(squareX, squareY + squareHeight, 0.0);
glEnd();
}
// Convert from screen coords returned by mouse
// to world coordinates.
// Return result in worldX, worldY
void OGWindow::screen2World(int screenX, int screenY, double & worldX, double & worldY)
{
// Dimensions of rectangle viewed by camera projection
double projWidth = Xmax -Xmin;
double projHeight = Ymax - Ymin;
// Screen coords with origin at bottom left
int screenLeft = screenX;
int screenUp = wHeight - screenY;
worldX = Xmin + screenLeft * projWidth/wWidth ;
worldY = Ymin + screenUp * projHeight/wHeight ;
}
//Method to draw the toolbar separator line
void OGWindow::drawToolbar(GLvoid) {
//draw toolbar line separator
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3f(-2.0, 11.0, 0.0);
glVertex3f(20.0, 11.0, 0.0);
glEnd();
//draw create button
glPushMatrix();
glTranslatef(2.0, 10.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();
}
Это мой главный класс, где я ivoking методы из OGWindow:
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Demo");
glEnable(GL_DEPTH_TEST); // enable the depth buffer test
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glutPassiveMotionFunc(mousePassiveMotion);
glutIdleFunc(Idle);
theWindow.initGL();
glutMainLoop();
}
Вы инициализировали матрицу 'GL_MODELVIEW', используя' glLoadIdentity'? Также вы не меняете «glMatrixMode» обратно на «GL_MODELVIEW» после изменения размера. – aslg