A basic Tracing Function for OpenGL

With the task of creating a http://hobo-ai.blogspot.com/2019/ , Harry Plotter - download source at https://github.com/housebyte/HarryPlotter for Windows instead of Linux, ive been tinkering at OpenGL to use as the renderer instead of Xwindows X11 library used by the Harry Plotter. After some frantic research about buffering Ive finally got a trace that works heres my shifty 'first go at it' code in C++. Enjoy:

(Hope it might help anyone wishing to do a data trace for windows from their Algo.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* Include Files */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <GL/freeglut.h>
#ifdef _MSC_VER
/* DUMP MEMORY LEAKS */
#include <crtdbg.h>
#endif

int trace_index;							/* index of trace */
double red_position[2][3];						/* X,Y,Z of red trace*/
double xcen = 0.0, ycen = 0.0, zcen = 0.0 ;   /* Coordinates of the point looked at */

int endofindex = 0;
double lines[300][3];
int numlines = 0;


The above is just the headers and the arrays for storing the trace which gets sent to the buffer using the following functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void draw_line2buffer(double position[2][3]){

/*if end of screen reached*/
if(position[1][0]==endofindex){
	numlines=1;
	lines[0][1] = position[0][1];
	lines[0][0] = position[0][0];
	
	lines[1][1] = position[1][1];
	lines[1][0] = position[1][0];
}else{
	
	numlines+=1;
	
	lines[numlines-1][1] = position[0][1];
	lines[numlines-1][0] = position[0][0];
	
	lines[numlines][1] = position[1][1];
	lines[numlines][0] = position[1][0];
	
	
}


glNewList(2,GL_COMPILE);
glBegin ( GL_LINES ) ;
glColor3f(1.0,0.0,0.0);

for(int i=1;i<numlines;i++){
	
glVertex3d ( lines[i-1][0], lines[i-1][1], 0.0 ) ;
glVertex3d ( lines[i][0], lines[i][1], 0.0 ) ;	

printf("Position:%f , %f \n",lines[i-1][0],lines[i-1][1]);
printf("**************************************************\n");
printf("Position:%f , %f \n",lines[i][0],lines[i][1]);
 
}
glEnd () ;

printf("End of buffer*************************************\n");

glEndList();
}



Thats the buffer having been written to now the display function for writing to the window followed by the timer which kicks out the data continously to the red_points array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void display ( void )
{
		
  glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;

//glCallList( 1 );  :- you can call multiple lists from the buffer!! means Multiple traces
//glColor3d (1.0,0.0,0.0);
//glCallList( 1 );
glCallList( 2 );
  
  glColor3d ( 1.0, 1.0, 0.0 ) ;  /* White */
  /* Draw some axes */
  glBegin ( GL_LINES ) ;
  glVertex3d ( 1.0, 0.0, 0.0 ) ;
  glVertex3d ( -1.0, 0.0, 0.0 ) ;
  
  glVertex3d ( 0.0, 1.0, 0.0 ) ; /*Y coord*/
  glVertex3d ( 0.0, -1.0, 0.0 ) ;
  
 glEnd () ;
  
 glutSwapBuffers( );
 glFlush( );
 
}



And the timer function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void timer(int value){

/* Make your data come in here */
double x = ((double)trace_index);
double y = sin(x);
double z = 0;

Sleep(10);
	
/* Set the next timed callback */
glutTimerFunc ( 30, timer, 0 ) ;
	
/*set the last data as the first*/
for(int i=0;i<3;i++){
red_position[0][i]	= red_position[1][i];
}

red_position[1][0] = x/100;
red_position[1][1] = y;
red_position[1][2] = z;

/*reset the index when trace gets to the end of screen*/
if(trace_index>100){
trace_index=0;}else{
trace_index = trace_index+1;
}

draw_sphere2(red_position);
draw_line2buffer(red_position);

glutPostRedisplay () ;

}



And tie it all together with some initializing and a GlutMainloop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main ( int argc, char *argv[] )
{
  int pargc = argc ;
 
  /* Initialize the array */
  srand ( 1023 ) ;
  for(int i=0;i<3;i++){
  red_position[1][i] = 0;
				}
  trace_index = 0;

  /* Set up the OpenGL parameters */
  glEnable ( GL_DEPTH_TEST ) ;
  glClearColor ( 0.0, 0.0, 0.0, 0.0 ) ;
  glClearDepth ( 1.0 ) ;

  /* Initialize GLUT */
  glutInitWindowSize ( 600, 600 ) ;
  glutInit ( &pargc, argv ) ;
  glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;

  /* Create the window */
  glutCreateWindow ( "Harry Plotter" ) ;
  
  glutDisplayFunc ( display ) ;
  //glutReshapeFunc ( reshape ) ; - Not used (not really what its for!!)
  glutTimerFunc   ( 30, timer, 0  ) ;
    /* Enter the GLUT main loop */
  glutMainLoop () ;

#ifdef _MSC_VER
  /* DUMP MEMORY LEAK INFORMATION */
  _CrtDumpMemoryLeaks () ;
#endif

  return 0 ;
  
}


I think that nailed it. I am sure there are other methods but this will do for me.
With Mingw64 installed on Windows compile with g++ -o Trace Trace.cpp -lopengl32 -lfreeglut -lglu32

freeglut can be [url=http://freeglut.sourceforge.net/]downloaded[/url] and placed in each of Mingw folders include/ bin/ system32/*dll lib/*lib

Source : http://spacetripping.just4books.co.uk/HiRobot/viewtopic.php?f=10&t=12
Last edited on
Topic archived. No new replies allowed.