# Newb to OpenGL, ? about frames

This topic is 3474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am learning opengl on my own for school. I was asked to draw random dots on a screen. Then to have them change positions every three frames. Is there a frame counter function? or is it as simple as creating a counter? Here's a snip of the code for the dots, glEnable (GL_POINTS); for(int index=0; index<=800; index++) { x_coord = (rand()%(win_x-2)); y_coord = (rand()%(win_y-2)); drawOnePoint (x_coord, y_coord); } glDisable (GL_POINTS);

##### Share on other sites
3 frames or 3 seconds? because when your app runs at 60 frames per second, 3 frames is 1/20 of second, dots will change position 20 times per second.

anyway:

int frame_counter = 0;void draw(){  ... state initializations ...  glEnable (GL_POINTS);   for(int index=0; index<=800; index++)  {   x_coord = (rand()%(win_x-2));  y_coord = (rand()%(win_y-2));  drawOnePoint (x_coord, y_coord);  }  glDisable (GL_POINTS);   frame_counter++;  if (frame_counter == 3)  {    srand(time(0));    frame_counter = 0;  }  ... glSwapBuffers or whatever are you using ...}

##### Share on other sites
You are right, I did not think about frames/seconds. Teacher said 3 frames, but I don't think that's what he meant. Thanks for the tip, I'm going to try it out right now.

##### Share on other sites
I've ran into a problem. To make things simpler, i put the dots into a display list. I also added a moving square. I can't get the dots to change positions using the srand() function. I've stuck the code everywhere. What am i doing wrong? Please excuse my code!!

#include <windows.h>#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>#include <stdlib.h>#include <cstdlib>#include <ctime>#define drawOnePoint(x1,y1)  glBegin(GL_POINTS);     glVertex2f ((x1),(y1)); glEnd();static GLfloat move_x = 0.0;int win_x = 1280;	//Window widthint win_y = 1024;	//Window heightint square = 50.0;	//Square width & heightint x_dir = 1;// 1-right, 0-leftGLuint listDots;int x_coord;int y_coord;int frame_counter;void init(void) {	listDots = glGenLists(1);	glNewList(listDots, GL_COMPILE);	glClear (GL_COLOR_BUFFER_BIT);	glColor3f (1.0, 1.0, 1.0);	glEnable (GL_POINTS);		for(int index=0; index<=800; index++)	{ 		x_coord = (rand()%(win_x-2));		y_coord = (rand()%(win_y-2));		drawOnePoint (x_coord, y_coord);	}	glDisable (GL_POINTS);	glEndList();	glClearColor (0.0, 0.0, 0.0, 0.0);	glShadeModel (GL_FLAT);}void display(void){	glCallList(listDots);	glPushMatrix();	glTranslatef(win_x/2,win_y/2,0);	glTranslatef(move_x, 0.0, 0.0);	glColor3f(1.0, 0.0, 0.0);	glRectf(-square, -square, square, square);	glPopMatrix();	glutSwapBuffers();	glFlush ();}void moveSquare(void){	if (x_dir == 1 && move_x < 200.0){		move_x = move_x + 7.0;	}	else{		x_dir = 0;	}	if (x_dir == 0 && move_x > -200.0){		move_x = move_x - 7.0;	}	else{		x_dir = 1;	}	glutPostRedisplay();}void reshape (int w, int h){   glViewport (0, 0, (GLsizei) w, (GLsizei) h);   glMatrixMode (GL_PROJECTION);   glLoadIdentity ();   gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);}void mouse(int button, int state, int x, int y){	switch (button){		case GLUT_LEFT_BUTTON:			if (state == GLUT_DOWN)				glutIdleFunc(moveSquare);			break;		case GLUT_MIDDLE_BUTTON:			if (state == GLUT_DOWN)				glutIdleFunc(NULL);			break;		default:			break;	}}int main(int argc, char** argv){	glutInit(&argc, argv);	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);	glutInitWindowSize (win_x, win_y); 	glutInitWindowPosition (0, 0);	glutCreateWindow (argv[0]);	init ();	glutDisplayFunc(display); 	glutReshapeFunc(reshape);	glutMouseFunc(mouse);	glutMainLoop();	return 0;}

##### Share on other sites
Quote:
 Original post by bobby_nguyenI've ran into a problem. To make things simpler, i put the dots into a display list.*** Source Snippet Removed ***

Since display lists (DL's) are static, how would expect the dots to move? They cannot, unless you compile the DL every time you need to change the positions, but since that is very often, it would sort of defeat the idea of having a DL in the first place. You can only move all the dots (like if they are sitting in a cube, where you cannot change their relative positions to each other) with pushing/popping a matrix before glCallList, which is not what you want. So you probably do not want a DL after all :)
You want a dynamic/streaming structure, where you can upload the positions every frame or every few frames, of which the easiest is the good old glBegin/glEnd. From there you can enhance the performance by using vertex arrays (VA's) or streaming vertex buffer objects (VBO's). VA's are less complicated than VBO's to understand, and they should be fine for this, but you can probably just stick with glBegin/glEnd.
Then you can use the code from i_luv_cplusplus (perhaps with a bit of modification to the 3 seconds/frames part).

##### Share on other sites
Actually, he can rotate and translate the points. He is using srand() to generate random numbers even though it does not generate a random number. Look up rand().

You said that already, I just skimmed your posts at first. Sorry.