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