S.O.S
The screen do not refresh although I use glFlush()
unless I press ALT+TAB twice
please tell me whats wroung
This is my simple program
/*---------------------------------------------------------------
header file for clipping project
----------------------------------------------------------------*/
//SCREEN SIZE
const SCREEEN_X = 600;
const SCREEEN_Y = 600;
//global variables
bool IsQuadPainted = false;//after 2 clicks of left mouse click becomes true
bool IsLinePainted = false;//after 2 clicks of right mouse click becomes true
// struct for represent point in the screen
struct sPoint
{
float x, y;
};
// node in list of lines
struct sLine
{
sPoint p1, p2;
sLine *NextLine;
};
sPoint corner1,corner2 ;//rectangle ''s values
float Line_x1,Line_y1;//line''s values
sLine *lines; // list of lines
float Rleft , Rright , Rtop ,Rbottom;//the rectanle''s values with meaning
int RightClickCounter = 0;//Right Click Counter
int LeftClickCounter = 0;//Left Click Counter
//calculets the code of given point
char CalcCode(float X,float Y);
// convert value of int to float.
float convertVal(int xyVal,bool isX);
// draw a blue rectangle
void PaintQuad();
// draw clipped line
void PaintLine(sPoint p1,sPoint p2);
// set borders of rectangle using for the clipping algorithm
void SetBorders();
void myDisplay();
void myMouse(int btn,int st, int x ,int y);
void myKey(unsigned char key, int x, int y);
void myInit();
// free list of lines.
void FreeLines(sLine *line);
#include
#include "clipping.h"
#include
// CalcCode : calculets the code of given point
// Parameters : x,y - value of given point.
// Returns : the code for this point.
char CalcCode(float X,float Y)
{
char result=0;
if (!IsQuadPainted)
return result;
if (X < Rleft)
result = result | 1;
else
result = result & 14;
if (Y > Rtop)
result = result | 2;
else
result = result & 13;
if (X > Rright)
result = result | 4;
else
result = result & 11;
if (Y < Rbottom)
result = result | 8;
else
result = result & 7;
return result;
}
// convertVal : converts integer position on screen to float position
// Parameters : xyVal - value in integer
// isX - if we have x value of point (x,y)
// Returns : float position
float convertVal(int xyVal,bool isX)
{
if (isX)
return ((float)(xyVal)/SCREEEN_X);
else
return ((float)(SCREEEN_Y - xyVal)/SCREEEN_Y);
}
// PaintQuad : paints rectangle on screen
// Parameters : Global
void PaintQuad()
{
glColor3f(0.0f,0.0f,1.0f);//blue
glRectf(corner1.x,corner1.y,corner2.x,corner2.y);
//glFlush();
}
// PaintLine : paints the line on screen with the right colors
// parameters : p1 , p2 - two points on screen which represent line to be drawn
void PaintLine(sPoint p1 ,sPoint p2)
{
float tmpX1,tmpX2,tmpY1,tmpY2;
do
{
if(CalcCode(p1.x,p1.y) & CalcCode(p2.x,p2.y))
{ // outside the rect in the same zone
glColor3f(1.0f,0.0f,0.0f); // red
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
//glFlush();
return;
}
if(!(CalcCode(p1.x,p1.y) | CalcCode(p2.x,p2.y)))
{ // inside the rect
glColor3f(0.0f,1.0f,0.0f);//green
glEnable(GL_LINE_STIPPLE);
glLineStipple(1,0x5555);//as asked for
glBegin(GL_LINES);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p2.y);
glEnd();
glDisable(GL_LINE_STIPPLE);
//glFlush();
return;
}
tmpX1 = p1.x;
tmpX2 = p2.x;
tmpY1 = p1.y;
tmpY2 = p2.y;
if (p1.xRright)
{
p1.y+=(((p2.y-p1.y)/(p1.x-p2.x))*(p1.x-Rright));
p1.x=Rright;
}
else
if (p2.x>Rright)
{
p2.y+=(((p1.y-p2.y)/(p2.x-p1.x))*(p2.x-Rright));
p2.x=Rright;
}
if (p1.yRtop)
{
p1.x+=(((p2.x-p1.x)/(p1.y-p2.y))*(p1.y-Rtop));
p1.y=Rtop;
}
else
if (p2.y>Rtop)
{
p2.x+=(((p1.x-p2.x)/(p2.y-p1.y))*(p2.y-Rtop));
p2.y=Rtop;
}
glColor3f(1.0f,0.0f,0.0f);//red
glBegin(GL_LINES);
glVertex2f(tmpX1,tmpY1);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(tmpX2,tmpY2);
glEnd();
//glFlush();
glColor3f(0.0f,1.0f,0.0f);//green
glEnable(GL_LINE_STIPPLE);
glLineStipple(1,0x5555);
glBegin(GL_LINES);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p2.y);
glEnd();
glDisable(GL_LINE_STIPPLE);
//glFlush();
return;
}
while(1);
}
// SetBorders : sets the rect values to know the borders
// Parameters : none.
void SetBorders()
{
if (corner1.x < corner2.x)
{
Rleft = corner1.x;
Rright = corner2.x;
}
else
{
Rleft = corner2.x;
Rright = corner1.x;
}
if (corner1.y < corner2.y)
{
Rbottom = corner1.y;
Rtop = corner2.y;
}
else
{
Rbottom = corner2.y;
Rtop = corner1.y;
}
}
// FreeLines : free allocated memory for all lines
// Parameters : line - pointer to a list of lines.
void FreeLines(sLine *line)
{
if (line != NULL)
{
FreeLines(line->NextLine);
delete line;
line = NULL;
}
}
// myDisplay : function that draw all things that we have in the window
//
void myDisplay()
{
sLine *line;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.6f,0.8f,0.0f);
if (IsQuadPainted)
{
PaintQuad();
SetBorders();
}
if (IsLinePainted)
{
line = lines;
while(line)
{
PaintLine(line->p1, line->p2);
line = line->NextLine;
};
}
glFlush();
}
void myMouse(int btn,int st, int x ,int y)
{
sLine *CurrentLine;
switch(btn)
{
case GLUT_MIDDLE_BUTTON:
if (st == GLUT_DOWN)
{
FreeLines(lines);
exit(0);
}
break;
case GLUT_LEFT_BUTTON:
if (IsQuadPainted)
if (st == GLUT_DOWN)
{
switch(LeftClickCounter%2)
{
case 0:
Line_x1=convertVal(x,true);
Line_y1=convertVal(y,false);
IsLinePainted = false;
break;
case 1:
if(lines == NULL)
{
CurrentLine = new sLine;
lines = CurrentLine;
} else
{
CurrentLine = lines;
while(CurrentLine->NextLine) CurrentLine = CurrentLine->NextLine;
CurrentLine->NextLine = new sLine;
CurrentLine = CurrentLine->NextLine;
};
CurrentLine->NextLine = NULL;
CurrentLine->p1.x = Line_x1;
CurrentLine->p1.y = Line_y1;
CurrentLine->p2.x = convertVal(x,true);
CurrentLine->p2.y = convertVal(y,false);
IsLinePainted = true;
break;
}
LeftClickCounter++;
}
break;
case GLUT_RIGHT_BUTTON:
if (!IsQuadPainted)
if (st == GLUT_UP)
{
switch(RightClickCounter)
{
case 0:
corner1.x=convertVal(x,true);
corner1.y=convertVal(y,false);
break;
case 1:
corner2.x=convertVal(x,true);
corner2.y=convertVal(y,false);
IsQuadPainted = true;
break;
}
RightClickCounter++;
}
break;
}
}
void myKey(unsigned char key, int x, int y)
{
switch(key)
{
case ''q'': FreeLines(lines);
exit(0);
}
}
void myInit()
{
glClearColor(1.0f,1.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
lines = NULL;
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(SCREEEN_X, SCREEEN_Y);
glutCreateWindow("ILan Rozen and Yaniv Cohen - Targil No 1");
myInit();
glutMouseFunc(myMouse);
glutKeyboardFunc(myKey);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
I don''t use glut so i can barely read that but don''t you need to swap the buffers in your program? If you don''t swap the buffers it won''t redraw anything to the best of my knowledge. Like i said though i don''t use glut so i don''t know if glut does it for you or not. If you don''t know how to swap the buffers it is OS specific.
I only know windows which is:
SwapBuffers( HDC );
where HDC is you handle for the device context(now that i think about it glut probably does it for you since it sets up the window and you don''t even have to worry about the device context)
"Those who want it, but can''''t get it, will complain about it.
I only know windows which is:
SwapBuffers( HDC );
where HDC is you handle for the device context(now that i think about it glut probably does it for you since it sets up the window and you don''t even have to worry about the device context)
"Those who want it, but can''''t get it, will complain about it.
At the end of your myDisplay() function, after glFlush(), add this line:
glutSwapBuffer();
That should do it
Keep in mind that if you had an idle function, that you would declare with glutIdleFunc(), you would also have to call glutPostRedisplay(void) within the idle function every time you wanted to redraw the window. Otherwise, all you need is the line above after your drawing code.
Good luck
glutSwapBuffer();
That should do it

Keep in mind that if you had an idle function, that you would declare with glutIdleFunc(), you would also have to call glutPostRedisplay(void) within the idle function every time you wanted to redraw the window. Otherwise, all you need is the line above after your drawing code.
Good luck
man... he''s lucky... whenever I forget to swap the buffers my computer then procedes to puke, crash, and sqeak at me until I reboot....
*** After glancing at the code ***
wait, he''s using single buffered mode.. he shouldnt need to swap them... just place a glutPostRedisplay(); at the end of the display function.. this tells it to draw again... Really though, if you want fast graphics... then I''d suggest using GLUT_DOUBLE, for double buffering, and using glutSwapBuffers(); as mentioned earlier...
wait, he''s using single buffered mode.. he shouldnt need to swap them... just place a glutPostRedisplay(); at the end of the display function.. this tells it to draw again... Really though, if you want fast graphics... then I''d suggest using GLUT_DOUBLE, for double buffering, and using glutSwapBuffers(); as mentioned earlier...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement