Slow rendering using basic code.

I am new to openGL. I’m using openGL on top of C/C++ using Linux (Debian 9)
All I want to do is plot a matrix of single 2D points on an x,y and a+bi plane. I wrote code using include #include “GL/glut.h” and #include “GL/gl.h” which rendered considerably slower than code I used using GLFW #include <GLFW/glfw3.h> #include <GL/gl.h>

I’d like to understand why there is such a difference in render time. thanks.

SLOW CODE


// line_0.c  openGL plot points
// 181225
// g++ line_0.c -lglut -lGL -o line0

#include <stdio.h>
#include "GL/glut.h"
#include "GL/gl.h"

void plot2Dpoint(float x,float y, float red, float green, float blue){
  glColor3f(red, green, blue);
  glPointSize(1.0); 
  glBegin(GL_POINTS);
    glVertex2f(x, y);
  glEnd();
}

void drawPoints()
{
    int i,j;
    float h,k;
    float red = 1.0,green = 0 ,blue = 0.0;
    glClearColor(0.4, 0.4, 0.4, 0.4);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    h = -1.0; k = -1.0;
    for (i = 0; i < 500; i++){
      h = (float)(250 -i)/250;
      for( j = 0; j < 500; j++){
        k = (float)(250 - j)/250;
        //printf("drawpoints  %f %f 
",h,k);
        plot2Dpoint(h,k,red,green,blue);
        glFlush();
      }
      green = green + 0.1;
      if (green > 1)green = 0;

    }
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("OpenGL - Draw Points");
    glutDisplayFunc(drawPoints);
    glutMainLoop();
    return 0;
}



// cwc  GLFW plot study
//g++ lines_2D_1.cpp -lglfw -lGLU -lGL -o line2d1.o
// glOrtho 	see https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

#include <GLFW/glfw3.h>
#include <GL/gl.h>

void plot2Dpoint(int x,int y, float red, float green, float blue){
	glColor3f(red, green, blue);
	glBegin(GL_POINTS);
		glVertex2f(x, y);
	glEnd();

}

void render_loop()
{
	int h,k,done;
	float red,green,blue;
	green = 0.0;
	glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPointSize(1);
	glLineWidth(1);
	for (h = 0; h < 401;h++){
		for (k = 0; k < 401; k++){
			plot2Dpoint(h,k,red,green,blue);
		}
		red = red + (1.0/400.0);
		green =red  + (1.0/400.0);
		blue = blue + (1.0/400.0);
		//cout << green << endl;
		if (red > 1)red = 0;
		if (green > 1)green = 0;
		if (blue > 1)blue = 0;
	}


}

// main function
int main(int argc, char *argv[])
{
	GLFWwindow* window;
	if( !glfwInit() )
	{
		fprintf( stderr, "Failed to initialize GLFW
" );
		exit( EXIT_FAILURE );
	}

	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
	window = glfwCreateWindow( 400, 400, "LearnOpenGL", NULL, NULL );
	if (!window)
	{
		fprintf( stderr, "Failed to open GLFW window
" );
		glfwTerminate();
		exit( EXIT_FAILURE );
	}

	glfwMakeContextCurrent(window);
	glfwSwapInterval( 1 );

	// set up view
	glViewport( 0, 0, 400, 400 );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();

	glOrtho(0.0,400.0,0.0,400.0,0.0,1.0); // this creates a canvas you can do 2D drawing on
	// main loop
	while( !glfwWindowShouldClose(window) )
	{
		render_loop(); 	// Draw gears
		glfwSwapBuffers(window); // Swap buffers
		glfwPollEvents();
	}
	glfwTerminate(); 	// Terminate GLFW
	exit( EXIT_SUCCESS ); 	// Exit program
}//end main function



[QUOTE=uomo1885;1293285]I am new to openGL. I’m using openGL on top of C/C++ using Linux (Debian 9)
All I want to do is plot a matrix of single 2D points on an x,y and a+bi plane. I wrote code using include #include “GL/glut.h” and #include “GL/gl.h” which rendered considerably slower than code I used using GLFW #include <GLFW/glfw3.h> #include <GL/gl.h>

I’d like to understand why there is such a difference in render time.
[/QUOTE]
Well, there are a number of differences aside from the use of GLUT versus GLFW. The one most likely to affect speed is the glFlush() call after each point in the GLUT version.

Because the GLUT version is single buffered, you need to call this after drawing to ensure that the commands are actually sent to the graphics hardware. But you don’t need to call it after each point. This isn’t an issue for the GLFW version because glfwSwapBuffers() performs an implicit flush.

Regardless of this issue, I’d suggest changing the GLUT version to use double buffering. Pass GLUT_DOUBLE instead of GLUT_SINGLE in the glutInitDisplayMode() call and call glutSwapBuffers() at the end of the drawPoints() function. Then you don’t need the glFlush(), and it may reduce flickering.

Also, move the glBegin/glEnd calls out of the loop. Call glBegin() at the start of the drawing function and glEnd() at the end (but before the swap-buffer call). You only need to call glColor() and glVertex() for each point.

[QUOTE=GClements;1293288]e glFlush() was removed
glfwSwapBuffers() {thanks}
GLUT_DOUBLE replaced of GLUT_SINGLE in the glutInitDisplayMode()
glutSwapBuffers() was called after the nested for loop in drawPoints() function.

glBegin/glEnd was issued before and after the nested for loop. .[/QUOTE]

Thank you so much for the post!
You have no idea how much I appreciate this.

I teach introductory computer science with an emphasis in Cyber Security at the High School level.
This is going to be a excellent lesson in double buffering which I use in Java.

I teach python semester 1 and am going to teach C/C++ (instead of Java now) semester 2.

I like to use graphics to teach code because it’s a way students can see what is going on.
Plus it makes programming fun for young programmers.

For me this will be fun. I like to write code to generate fractal designs.