PDA

View Full Version : how to draw updated primatives in glut



onlyhuman23
07-24-2002, 06:38 PM
all that I want to display is created in a function passed to the glutDisplayFunc(). Then I call the glutTimerFunc() after a few seconds and update the position of the primatives, but how should I redisplay the updated primatives. I have tried calling glutPostRedisplay() but it does not redraw the contents of the function passed to the glutDisplayFunc(). Help. http://www.opengl.org/discussion_boards/ubb/frown.gif

JML
07-24-2002, 09:02 PM
glutPostRedisplay is indeed the way to signal to GLUT that you want the screen to be drawn again. Usually a call to glutPostRedisplay is placed in the idle callback or some other convenient place(s) in your code (but NOT in the display callback).

If you use a doublebuffered framebuffer, don't forget to SwapBuffers() at the end of the display function.

HTH

Jean-Marc

onlyhuman23
07-24-2002, 10:42 PM
I am using a single buffered approach as I am only making a 2D simulation. What I am doing right now is that I draw my 2D primitives in a function which is registered as a callback in the glutDisplayFunc(). So naturally after I update the primitives position, and call glutPostRedisplay() in another function, which inturn is called in glutTimerFunc(), it should display the updated position. I wonder what is wrong with my code. If anyone would like to see the code it is displayed bellow:

#define SCHEDULE 1
#define INSERT 2
#define REMOVE 3
#define RESTART 4
#define CLOSE 5


#include <windows.h>
#include <math.h> // Header File For Windows Math Library
#include <stdio.h> // Header File For Standard Input/Output
#include <stdarg.h> // Header File For Variable Argument Routines
//////////////////////////////////////////////////////////////////////////
#include <gl/GL.h>
#include <gl/glu.h>
#include <gl/glut.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

const int screenWidth = 800; //width of the screen in pixels
const int screenHeight = 600; //height of the screen in pixels
GLdouble A,B,C,D; //values used for shifting and scaling
int var=0;

static int width=50;
static int height=20;

class GLintPoint
{
public:
GLint x,y;
};

class qClass
{
public:
GLintPoint stPoint;
bool vis;

//<<<<<<<<<<<<<<<Custom Function for Hollow Rectangle >>>>>>>>>
void drawT()
{
if(vis != NULL)
{
glColor3f(0.00f,0.00f,1.00f);
glRecti(stPoint.x,stPoint.y,stPoint.x+width,stPoin t.y-height);
}
else
{
glColor3f(0.00f,0.00f,0.00f);
glRecti(stPoint.x,stPoint.y,stPoint.x+width,stPoin t.y-height);
}
glColor3f(1.00f,1.00f,1.00f);
glBegin(GL_LINE_LOOP);
glVertex2i(stPoint.x , stPoint.y);
glVertex2i(stPoint.x + width, stPoint.y);
glVertex2i(stPoint.x + width, stPoint.y - height);
glVertex2i(stPoint.x , stPoint.y - height);
glEnd();
}
};

GLintPoint myPoint1,myPoint2,myPoint3,myPoint4,myPoint5;
qClass thread1,thread2,thread3,thread4,thread5,thread6;

void myRectangle(GLintPoint stPoint, GLint width, GLint height);
void boardRect();
void stacks();
void createGLUTMenus();
void processMenuEvents(int option);
void myReshape(GLsizei W,GLsizei H);
void displayQ();
void myUpdate(int x);

void myInit(void)
{

glClearColor(0.0,0.0,0.0,0.0); //Background color is black
glColor3f(0.0f,1.0f,0.0f); //Drawing color is white
glPointSize(2.0); //A dot is 2*2 pixels
glMatrixMode(GL_PROJECTION); //Camera shape
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble) screenHeight);//This function sets the world co-ordinates
glViewport(0,0,screenWidth,screenHeight); //This function sets the viewport co-ordinates
}
//myDisplay
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); //clear the screen
// glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
boardRect();
displayQ();
stacks();
// glutStrokeCharacter(GLUT_STROKE_ROMAN, 33);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');//I am not able to position this character

glFlush(); //send all output to display
}

//<<<<<<<<<<<<<<<<Start of Main>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
glutInit(&argc, argv); //initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set display mode
glutInitWindowSize(screenWidth, screenHeight); //set window size
// glutInitWindowPosition(100,100); //set window position on screen
var=glutCreateWindow("Dot plot of a function"); //open the screen window
glutFullScreen();
// glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay); //register redraw function
myInit();
glutReshapeFunc(myReshape);
createGLUTMenus();
glutTimerFunc(3000,myUpdate,0);
glutMainLoop(); //go into a perpetual loop

}
//<<<<<<<<<<<<<<<<<<End of the Main Function>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<myReshape Func>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void myReshape(GLsizei W, GLsizei H)
{
int R = screenWidth/screenHeight;
if(R> W/H)
glViewport(0,0,W,W/R);
else
glViewport(0,0,H*R,H);
}
//<<<<<<<<<<<<<<<<<<Custom Func to create Menus>>>>>>>>>>>>>>>>
void createGLUTMenus()
{

int menu;

// create the menu and
// tell glut that "processMenuEvents" will
// handle the events
menu = glutCreateMenu(processMenuEvents);

//add entries to our menu
glutAddMenuEntry("Schedule",SCHEDULE);
glutAddMenuEntry("Insert",INSERT);
glutAddMenuEntry("Remove",REMOVE);
glutAddMenuEntry("Restart",RESTART);
glutAddMenuEntry("Close",CLOSE);

// attach the menu to the right button
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
//<<<<<<<<<<<<<<<<<Custom Func to process Menu events>>>>>>>>>>
void processMenuEvents(int option)
{

switch (option)
{
case SCHEDULE :
glColor3f(1.00f,0.00f,0.00f);
// myUpdate(0);
// displayQ();

break;
case INSERT :
//glColor3f(0.00f,1.00f,0.00f);
thread1.vis=NULL;
thread6.stPoint.x = thread1.stPoint.x;;
thread6.stPoint.y = thread5.stPoint.y - height - 1;
thread6.vis=NULL;
thread6.drawT();
glutPostRedisplay();
break;
case REMOVE :
glColor3f(0.00f,0.00f,1.00f);
break;
case RESTART :
glColor3f(1.00f,1.00f,1.00f);
break;
case CLOSE :
//glColor3f(1.00f,1.00f,1.00f);
//glutDestroyWindow(var);
exit(1);
break;
}
}
//<<<<<<<<<<<<<<<Custom Function for Hollow Rectangle >>>>>>>>>
void myRectangle(GLintPoint stPoint, GLint width, GLint height)
{
glBegin(GL_LINE_LOOP);
glVertex2i(stPoint.x , stPoint.y);
glVertex2i(stPoint.x + width, stPoint.y);
glVertex2i(stPoint.x + width, stPoint.y - height);
glVertex2i(stPoint.x , stPoint.y - height);
glEnd();
}
//<<<<<<<<<<<<<<Custom Func for Green Board>>>>>>>>>>>>>>>>>>>>
void boardRect()
{
//The big green chip
glColor3f(0.0f,1.0f,0.0f);
glRecti(158,590,430,490);
glColor3f(0.0f,0.70f,0.0f);
glRecti(154,594,426,494);
//The small gray chips
glColor3f(0.50f,0.50f,.50f);
glRecti(180,580,220,540);
glRecti(240,580,280,540);
glRecti(300,580,340,540);
glRecti(360,580,400,540);

glColor3f(0.40f,0.40f,.40f);
glRecti(178,582,218,542);
glRecti(238,582,278,542);
glRecti(298,582,338,542);
glRecti(358,582,398,542);
glColor3f(0.20f,0.30f,0.40f);
}
void stacks()
{
glColor3f(0.50f,0.00f,0.00f);
glRecti(180,440,220,390);
glColor3f(1.00f,1.00f,1.00f);
glBegin(GL_LINE_LOOP);
glVertex2i(180,440);
glVertex2i(220,440);
glVertex2i(220,390);
glVertex2i(180,390);
glEnd();
glColor3f(0.50f,0.00f,0.00f);
glRecti(240,440,280,390);
glColor3f(1.00f,1.00f,1.00f);
GLintPoint myPoint5;
myPoint5.x=240;
myPoint5.y=440;
myRectangle(myPoint5,40,50);
glRecti(300,440,340,390);
glRecti(360,440,400,390);
glColor3f(0.20f,0.30f,0.40f);
}

void displayQ()
{
thread1.stPoint.x = 50;
thread1.stPoint.y = 520;
thread1.vis=TRUE;
thread1.drawT();

thread2.stPoint.x = thread1.stPoint.x;
thread2.stPoint.y = thread1.stPoint.y - height - 1;
thread2.vis=TRUE;
thread2.drawT();

thread3.stPoint.x = thread1.stPoint.x;;
thread3.stPoint.y = thread2.stPoint.y - height - 1;
thread3.vis=TRUE;
thread3.drawT();

thread4.stPoint.x = thread1.stPoint.x;;
thread4.stPoint.y = thread3.stPoint.y - height - 1;
thread4.vis=TRUE;
thread4.drawT();

thread5.stPoint.x = thread1.stPoint.x;;
thread5.stPoint.y = thread4.stPoint.y - height - 1;
thread5.vis=TRUE;
thread5.drawT();
}
void myUpdate(int x)
{
/* thread5.stPoint.x = thread1.stPoint.x;;
thread5.stPoint.y = thread4.stPoint.y - height - 1;
thread5.vis=TRUE;
thread5.drawT();
*/
thread1.vis=NULL;
thread1.stPoint.x = 180;
thread1.stPoint.y = 440;
// thread2.vis=NULL;
// glutDisplayFunc(myDisplay);
glutPostRedisplay();
// glutSetWindow(var);
}

JML
07-25-2002, 12:09 AM
Check out the GLUT documentation for glutTimerFunc, as I think the timer function will only fire ONCE, so you have to set the timer again in your myUpdate method.

HTH

Jean-Marc.

Rob Fletcher
07-25-2002, 04:46 AM
You MUST set the timer to fire again in the timer function. Timers in GLUT are one-shot, not free running.

Like using an ISR or signal handler ... usually the *last* thing you do is to set the parameters for the next event. I say last, because if you set it at the start of the function, and you have code to be executed following it, which for some reason *could* take longer than the timer interval, then you get into a horrible race condition.

Rob.

nexusone
07-25-2002, 09:48 AM
You need to add another glutTimerFunc at the end of your func.

example:

my_timerfunc()
{
// do what is needed here

glutPostRedisplay();
glutTimerFunc( xxxx, my_timerfunc, 0);// last item in routine.
}




[This message has been edited by nexusone (edited 07-25-2002).]

Jamie
07-28-2002, 05:59 PM