opengl:调整大小时如何将对象保留在窗口中
我正在使用 Bresenham 的中点算法作为家庭作业,在 OpenGL 上开发一个类似于 MS 绘画的应用程序。到目前为止我可以画直线和椭圆。调整窗口大小时我把它们全部丢失了。我怎样才能让他们保持吸引力?
完整代码:
#include "GL/glut.h"
#include <stdio.h>
#include <math.h>
int i;
//int mainWindow, subWindow;
int X1, Y1, X2, Y2;
int modoDeDibujo;
int W = 1000, H = 1000;
/*void menuApp (int value)
{
if (value == 1) printf("Linea\n");
if (value == 2) printf("Circulo\n");
if (value == 3) printf("Elipsis\n");
if (value == 4) exit(0);
}
void crearMenu()
{
//inicio Creando el menu
int submenu;
submenu = glutCreateMenu(menuApp);
glutAddMenuEntry("Linea", 1);
glutAddMenuEntry("Elipse",3);
glutAddMenuEntry("Salir",4);
glutCreateMenu(menuApp);
glutAddSubMenu("SubMenu", submenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//fin Creando el menu
}*/
void renderPoint(void) /*REVISAR ESTO*/
{
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POINTS);
glVertex2f (-0.98, 0.98);
glEnd ();
glFlush ();
}
void renderPoint(double x, double y)
{
//printf("BEFORE TRANSFORM %f\t%f\t# renderPoint\n", x, y);
W = glutGet(GLUT_WINDOW_WIDTH);
H = glutGet(GLUT_WINDOW_HEIGHT);
float X;
float Y;
glBegin (GL_POINTS);
X = (2*x/W) - 1;
Y = (-2*y/H) + 1;
glVertex2f (X, Y);
//printf("TRANSFORMED POINT %f\t%f\t# renderPoint\n", X, Y);
glEnd ();
glFlush ();
}
/*wiki pseudo:
function line(x0, x1, y0, y1) //x1
boolean steep := abs(y1 - y0) > abs(x1 - x0)//x2
if steep then//x3
swap(x0, y0) //x4
swap(x1, y1) //x5
if x0 > x1 then //x6
swap(x0, x1) //x7
swap(y0, y1) //x8
int deltax := x1 - x0 //x9
int deltay := abs(y1 - y0) //x10
int error := deltax / 2 //x11
int ystep //x12
int y := y0 //x13
if y0 < y1 then ystep := 1 else ystep := -1 //x14
for x from x0 to x1 //x15
if steep then plot(y,x) else plot(x,y) //x16
error := error - deltay //x17
if error < 0 then //x18
y := y + ystep //x19
error := error + deltax //x20
*/
void bresenham1(GLint x0, GLint x1, GLint y0, GLint y1) //function line(x0, x1, y0, y1)
{
//double result1 = fabs((double)y1 - y0); //abs(y1 - y0)
//double result2 = fabs((double)x1 - x0); //abs(x1 - x0)
int result1 = abs(y1-y0);
int result2 = abs(x1-x0);
bool steep = (result1 > result2); //boolean steep := abs(y1 - y0) > abs(x1 - x0)
if (steep){ //if steep then
GLint aux1 = x0; //swap(x0, y0)
x0=y0;
y0 = aux1;
GLint aux2 = x1; // swap (x1,y1)
x1=y1;
y1=aux2;
}
if(x0>x1){ // if (x0>x1)
GLint aux3=x0; //swap(x0,x1)
x0=x1;
x1=aux3;
GLint aux4=y0;//swap(y0,y1)
y0=y1;
y1=aux4;
}
int deltax = x1-x0; // deltax = x1-x0
int deltay = abs(y1-y0); // int deltay := abs(y1 - y0) - revisar
int error = (deltax / 2); //int error := deltax / 2
int ystep; // int ystep
int y = y0; //int y := y0
if (y0<y1){ //if y0 < y1 then ystep := 1 else ystep := -1
ystep=1;
}
else {ystep=-1;}
for (int x=x0; x<=x1; x++){ //for x from x0 to x1
if (steep){ // if steep then plot(y,x) else plot(x,y)
renderPoint(y,x);
}
else {
renderPoint(x,y);
}
error = error - deltay; //error := error - deltay
if (error<0) { //if error < 0 then
y = y + ystep; // y := y + ystep
error = error + deltax; //error := error + deltax
} // end if (error<0)
}// end for from x0 to x1
}// end bresenham
void Plot4EllipsePoints(int X, int Y,int CX,int CY){
renderPoint(CX+X, CY+Y); //point in quadrant 1
renderPoint(CX-X, CY+Y); // point in quadrant 2
renderPoint(CX-X, CY-Y); // point in quadrant 3
renderPoint(CX+X, CY-Y); // point in quadrant 4
}
void PlotEllipse (int CX, int CY, int XRadius, int YRadius) {
int X, Y;
int XChange, YChange;
int EllipseError;
int TwoASquare, TwoBSquare;
int StoppingX, StoppingY;
TwoASquare = 2 * XRadius * XRadius;
TwoBSquare = 2 * YRadius * YRadius;
X = XRadius;
Y =0;
XChange = YRadius*YRadius*(1-(2*XRadius));
YChange = XRadius * XRadius;
EllipseError =0;
StoppingX = TwoBSquare*XRadius;
StoppingY = 0;
while(StoppingX >= StoppingY){
Plot4EllipsePoints(X,Y,CX,CY);
Y++;
StoppingY=StoppingY + TwoASquare;
EllipseError= EllipseError+ YChange;
YChange= YChange+ TwoASquare;
if( ((2*EllipseError) + XChange)>0)
{
X--;
StoppingX = StoppingX - TwoBSquare;
EllipseError= EllipseError + XChange;
XChange = XChange + TwoBSquare;
}
}
//1st set of points done, start second set
X=0;
Y= YRadius;
XChange= YRadius*YRadius;
YChange = XRadius*XRadius*(1-2*YRadius);
EllipseError=0;
StoppingX =0;
StoppingY= TwoASquare * YRadius;
while(StoppingX <= StoppingY){ // 2nd set of points, y'<-1
Plot4EllipsePoints(X,Y, CX,CY);
X++;
StoppingX = StoppingX + TwoBSquare;
EllipseError = EllipseError + XChange;
XChange = XChange + TwoBSquare;
if (((2*EllipseError) + YChange)>0){
Y--;
StoppingY = StoppingY - TwoASquare;
EllipseError = EllipseError + YChange;
YChange = YChange + TwoASquare;
}
}
}
void renderAll (void)
{
/*glutSetWindow(mainWindow);
glutPostRedisplay();
glutSetWindow(subWindow);
glutPostRedisplay();*/
}
void movimiento(int boton, int estado, int x, int y)
{
if((estado == GLUT_DOWN) && (boton == GLUT_LEFT_BUTTON))//mouse down
{
X1 = x; Y1 = y;
PlotEllipse (x, y, 200, 100);
renderPoint(x,y);
}
if((estado == GLUT_UP) && (boton == GLUT_LEFT_BUTTON))//mouse up
{
//printf(" Up|x:%d, y:%d\n",x,y);
X2 = x; Y2 = y;
//renderLine();
bresenham1(X1,X2,Y1,Y2);
//PRUEBA USANDO LA PRIMITIVA DE OPENGL
glBegin( GL_LINES );
glEnd();
//renderPoint(x, y);
}
}
void MouseMove(int x, int y)
{
//printf("x:%d | y:%d\n", x,y);
X2 = x; Y2 = y;
//renderLine();
//bresenham1(X1, Y1, X2, Y2);
}
void teclado(unsigned char key, int x, int y)
{
if(key==1){
modoDeDibujo=1; // dibuja lineas
printf("Modo de dibujo: linea");
}
if (key==2){
modoDeDibujo=2; //dibuja elipses
}
if(key == 27)exit(0);
}
void especiales(int key, int x, int y)
{
if(key == GLUT_KEY_F1) exit(0);
}
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 27: /* Escape */
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
int main (int argc, char *argv [])
{
i = 0;
//inicializa las operaciones de OpenGL/GLUT, db cr antes de usar funciones GLUT
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition (100, 100);
glutInitWindowSize (W, H);
//Crea una ventana de Opengl
glutCreateWindow ("tarea");
glutDisplayFunc (renderPoint);
glutMouseFunc(movimiento);
glutKeyboardFunc(teclado);//teclas ASCII
glutSpecialFunc(especiales);//captura las teclas [f1..f12]
//glutPassiveMotionFunc(pasivo);
glutKeyboardFunc(key);
glutMotionFunc(MouseMove);
//crearMenu();
glutMainLoop ();
}
I'm working a MS paint-like application on OpenGL using Bresenham's midpoint algorithm as homework. So far I can draw lines and ellipses. I lose them all when resizing the window. How can I keep them drawn?
Full code:
#include "GL/glut.h"
#include <stdio.h>
#include <math.h>
int i;
//int mainWindow, subWindow;
int X1, Y1, X2, Y2;
int modoDeDibujo;
int W = 1000, H = 1000;
/*void menuApp (int value)
{
if (value == 1) printf("Linea\n");
if (value == 2) printf("Circulo\n");
if (value == 3) printf("Elipsis\n");
if (value == 4) exit(0);
}
void crearMenu()
{
//inicio Creando el menu
int submenu;
submenu = glutCreateMenu(menuApp);
glutAddMenuEntry("Linea", 1);
glutAddMenuEntry("Elipse",3);
glutAddMenuEntry("Salir",4);
glutCreateMenu(menuApp);
glutAddSubMenu("SubMenu", submenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//fin Creando el menu
}*/
void renderPoint(void) /*REVISAR ESTO*/
{
glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POINTS);
glVertex2f (-0.98, 0.98);
glEnd ();
glFlush ();
}
void renderPoint(double x, double y)
{
//printf("BEFORE TRANSFORM %f\t%f\t# renderPoint\n", x, y);
W = glutGet(GLUT_WINDOW_WIDTH);
H = glutGet(GLUT_WINDOW_HEIGHT);
float X;
float Y;
glBegin (GL_POINTS);
X = (2*x/W) - 1;
Y = (-2*y/H) + 1;
glVertex2f (X, Y);
//printf("TRANSFORMED POINT %f\t%f\t# renderPoint\n", X, Y);
glEnd ();
glFlush ();
}
/*wiki pseudo:
function line(x0, x1, y0, y1) //x1
boolean steep := abs(y1 - y0) > abs(x1 - x0)//x2
if steep then//x3
swap(x0, y0) //x4
swap(x1, y1) //x5
if x0 > x1 then //x6
swap(x0, x1) //x7
swap(y0, y1) //x8
int deltax := x1 - x0 //x9
int deltay := abs(y1 - y0) //x10
int error := deltax / 2 //x11
int ystep //x12
int y := y0 //x13
if y0 < y1 then ystep := 1 else ystep := -1 //x14
for x from x0 to x1 //x15
if steep then plot(y,x) else plot(x,y) //x16
error := error - deltay //x17
if error < 0 then //x18
y := y + ystep //x19
error := error + deltax //x20
*/
void bresenham1(GLint x0, GLint x1, GLint y0, GLint y1) //function line(x0, x1, y0, y1)
{
//double result1 = fabs((double)y1 - y0); //abs(y1 - y0)
//double result2 = fabs((double)x1 - x0); //abs(x1 - x0)
int result1 = abs(y1-y0);
int result2 = abs(x1-x0);
bool steep = (result1 > result2); //boolean steep := abs(y1 - y0) > abs(x1 - x0)
if (steep){ //if steep then
GLint aux1 = x0; //swap(x0, y0)
x0=y0;
y0 = aux1;
GLint aux2 = x1; // swap (x1,y1)
x1=y1;
y1=aux2;
}
if(x0>x1){ // if (x0>x1)
GLint aux3=x0; //swap(x0,x1)
x0=x1;
x1=aux3;
GLint aux4=y0;//swap(y0,y1)
y0=y1;
y1=aux4;
}
int deltax = x1-x0; // deltax = x1-x0
int deltay = abs(y1-y0); // int deltay := abs(y1 - y0) - revisar
int error = (deltax / 2); //int error := deltax / 2
int ystep; // int ystep
int y = y0; //int y := y0
if (y0<y1){ //if y0 < y1 then ystep := 1 else ystep := -1
ystep=1;
}
else {ystep=-1;}
for (int x=x0; x<=x1; x++){ //for x from x0 to x1
if (steep){ // if steep then plot(y,x) else plot(x,y)
renderPoint(y,x);
}
else {
renderPoint(x,y);
}
error = error - deltay; //error := error - deltay
if (error<0) { //if error < 0 then
y = y + ystep; // y := y + ystep
error = error + deltax; //error := error + deltax
} // end if (error<0)
}// end for from x0 to x1
}// end bresenham
void Plot4EllipsePoints(int X, int Y,int CX,int CY){
renderPoint(CX+X, CY+Y); //point in quadrant 1
renderPoint(CX-X, CY+Y); // point in quadrant 2
renderPoint(CX-X, CY-Y); // point in quadrant 3
renderPoint(CX+X, CY-Y); // point in quadrant 4
}
void PlotEllipse (int CX, int CY, int XRadius, int YRadius) {
int X, Y;
int XChange, YChange;
int EllipseError;
int TwoASquare, TwoBSquare;
int StoppingX, StoppingY;
TwoASquare = 2 * XRadius * XRadius;
TwoBSquare = 2 * YRadius * YRadius;
X = XRadius;
Y =0;
XChange = YRadius*YRadius*(1-(2*XRadius));
YChange = XRadius * XRadius;
EllipseError =0;
StoppingX = TwoBSquare*XRadius;
StoppingY = 0;
while(StoppingX >= StoppingY){
Plot4EllipsePoints(X,Y,CX,CY);
Y++;
StoppingY=StoppingY + TwoASquare;
EllipseError= EllipseError+ YChange;
YChange= YChange+ TwoASquare;
if( ((2*EllipseError) + XChange)>0)
{
X--;
StoppingX = StoppingX - TwoBSquare;
EllipseError= EllipseError + XChange;
XChange = XChange + TwoBSquare;
}
}
//1st set of points done, start second set
X=0;
Y= YRadius;
XChange= YRadius*YRadius;
YChange = XRadius*XRadius*(1-2*YRadius);
EllipseError=0;
StoppingX =0;
StoppingY= TwoASquare * YRadius;
while(StoppingX <= StoppingY){ // 2nd set of points, y'<-1
Plot4EllipsePoints(X,Y, CX,CY);
X++;
StoppingX = StoppingX + TwoBSquare;
EllipseError = EllipseError + XChange;
XChange = XChange + TwoBSquare;
if (((2*EllipseError) + YChange)>0){
Y--;
StoppingY = StoppingY - TwoASquare;
EllipseError = EllipseError + YChange;
YChange = YChange + TwoASquare;
}
}
}
void renderAll (void)
{
/*glutSetWindow(mainWindow);
glutPostRedisplay();
glutSetWindow(subWindow);
glutPostRedisplay();*/
}
void movimiento(int boton, int estado, int x, int y)
{
if((estado == GLUT_DOWN) && (boton == GLUT_LEFT_BUTTON))//mouse down
{
X1 = x; Y1 = y;
PlotEllipse (x, y, 200, 100);
renderPoint(x,y);
}
if((estado == GLUT_UP) && (boton == GLUT_LEFT_BUTTON))//mouse up
{
//printf(" Up|x:%d, y:%d\n",x,y);
X2 = x; Y2 = y;
//renderLine();
bresenham1(X1,X2,Y1,Y2);
//PRUEBA USANDO LA PRIMITIVA DE OPENGL
glBegin( GL_LINES );
glEnd();
//renderPoint(x, y);
}
}
void MouseMove(int x, int y)
{
//printf("x:%d | y:%d\n", x,y);
X2 = x; Y2 = y;
//renderLine();
//bresenham1(X1, Y1, X2, Y2);
}
void teclado(unsigned char key, int x, int y)
{
if(key==1){
modoDeDibujo=1; // dibuja lineas
printf("Modo de dibujo: linea");
}
if (key==2){
modoDeDibujo=2; //dibuja elipses
}
if(key == 27)exit(0);
}
void especiales(int key, int x, int y)
{
if(key == GLUT_KEY_F1) exit(0);
}
static void
key(unsigned char k, int x, int y)
{
switch (k) {
case 27: /* Escape */
exit(0);
break;
default:
return;
}
glutPostRedisplay();
}
int main (int argc, char *argv [])
{
i = 0;
//inicializa las operaciones de OpenGL/GLUT, db cr antes de usar funciones GLUT
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition (100, 100);
glutInitWindowSize (W, H);
//Crea una ventana de Opengl
glutCreateWindow ("tarea");
glutDisplayFunc (renderPoint);
glutMouseFunc(movimiento);
glutKeyboardFunc(teclado);//teclas ASCII
glutSpecialFunc(especiales);//captura las teclas [f1..f12]
//glutPassiveMotionFunc(pasivo);
glutKeyboardFunc(key);
glutMotionFunc(MouseMove);
//crearMenu();
glutMainLoop ();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,你需要整理代码。您必须有且只有一个显示函数,用于清除缓冲区、调用其他绘制函数并将它们刷新到屏幕上(或者如果您使用双缓冲区则交换缓冲区)。
在调整窗口大小时,GLUT 将调用显示函数,即 renderPoint(),如您所知:
renderPoint 在重绘“点”之前清除缓冲区,例如:
由于缓冲区已被清除,因此所有在函数 renderPoint 之外绘制的点(圆的点、线的点..)没有任何意义,因为您没有从“主显示函数”renderPoint 中调用它们。
如何保持点在屏幕?
您必须将点(您想要绘制的任何点)存储在缓冲区中,例如数组、动态数组、std::vector 或显示函数中的其他任何内容,编写循环语句来。访问每个点并提取 x 和 y .. 然后绘制它们
,而不是上面的函数,将其替换为 :
如您所见,通过使用 (std::vector) 等动态数组来存储点。通过使用 for 循环语句,我们能够保持三个点可见
?
对其他形状执行相同的方法,以便每次“
鼠标单击 " 事件,您可以
add 或 push_back
两个点表示数组或名为lineArray
的 std::vector 中的线端点。在显示函数
中,制作一个for循环语句,在提取两个线点后绘制每条线。您应该使用
glutReshapeFunc
和glViewport
来确保在调整大小事件之后视口与窗口具有相同的尺寸。我认为 gluOrtho2d 比尝试从 Windows 坐标空间映射到 OpenGL 坐标空间更优雅,安排您的代码,以便您只使用一个显示函数。
你的程序可能是这样的:
==
First of all , you need to arrange the code. You must have one and only one display function, that clears the buffer,calls the other draw-functions and flushes them to the screen ( or swaps the buffer if you are using a double buffer).
On resizing the window,GLUT will call the Display Function which is renderPoint() as you know :
renderPoint clears the buffer before redrawing the "points" , for example :
Since the buffer has been cleared , all the points that are drawn outside the function renderPoint ( the points of circles, the points of lines .. ) has no meaning , because you did not call them from the "main display function which is renderPoint.
How to keep the points on the screen ?
You must store the points ( any point you want to draw ) in a buffer such as an array , a dynamic array, std::vector or anything else. in the display function , write a loop statement to visit each point and extract the x and y .. then draw them.
for example , instead of the above function , replace it with :
as you can see , by using a dynamic array such as (std::vector) to store the points and by using a for loop statement , we able to keep the three points visible.
what else ?
do the same method with other shapes , so that for each "
mouse click
" event , you mayadd or push_back
two points represents a line end points in an array or std::vector calledlineArray
. in thedisplay function
, make a for loop statement to draw each line after extracting the two line points.you should use
glutReshapeFunc
andglViewport
to make sure that the viewport has the same dimenstions as the window after the resizing event. and I think thatgluOrtho2d
is more elegant than trying to mapping from Windows coordinates space to OpenGL coordinates spacearrange your code , so that you just use one display function.
Your program could be Something like this:
==
不要认为这是“让他们保持吸引力”。相反,从事件和“我什么时候需要重画我的对象”的角度来思考。
为此,您需要处理调整大小事件并重新运行绘图代码。我还猜测,在另一个应用程序移到对象上后,或者将其移出屏幕一点然后再带回来时,您的对象可能不会重新绘制自己。如果是这种情况,您需要弄清楚要处理哪些事件也会导致重绘。
Don't think of it as "keeping them drawn". Think instead in terms of events and "when do I need to redraw my objects".
For this you will need to handle the resize event and rerun your drawing code. I'm also guessing that your objects might not redraw themselves after another application is moved over them, or if it is moved off screen a little and then brought back. If this is the case you'll need to figure out what events to handle that will cause a redraw as well.
这是 Windows 的一个简单示例:
http://www.falloutsoftware.com/tutorials/gl/gl2.htm
Here is a quick example for windows :
http://www.falloutsoftware.com/tutorials/gl/gl2.htm