A basic Tracing Function for OpenGL
Posted: Fri Sep 18, 2020 2:30 pm
With the task of creating a 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.)
The above is just the headers and the arrays for storing the trace which gets sent to the buffer using the following functions:
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:
And the timer function:
And tie it all together with some initializing and a GlutMainloop:
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 downloaded and placed in each of Mingw folders include/ bin/ system32/*dll lib/*lib
Sneak a peak @ Shepherds latest works DarkSide - Chapter 2 - The Watchers
Or Read Kromos now :
(Hope it might help anyone wishing to do a data trace for windows from their Algo.)
- Code: Select all
/* 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:
- Code: Select all
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:
- Code: Select all
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:
- Code: Select all
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:
- Code: Select all
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 downloaded and placed in each of Mingw folders include/ bin/ system32/*dll lib/*lib
Sneak a peak @ Shepherds latest works DarkSide - Chapter 2 - The Watchers
Or Read Kromos now :