How this code is working?

I am a little bit confused on how this application is working. I have neither created any shader nor used any MODAL VIEW Matrix in program below but it still renders successfully. Can anyone help me? I have pasted the code below

// Include standard headers
#include <stdio.h>
#include <stdlib.h>

// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <glfw3.h>
GLFWwindow* window;

// Include GLM
#include <glm/glm.hpp>
#include <math_3d.h>

GLuint VBO;
using namespace glm;

int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW
" );
return -1;
}
//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, “Tutorial 01”, NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.
" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW
");
return -1;
}

// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
//glPointSize(10.0f);//set point size to 10 pixels

Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
//glColor3f(1.0f,0.0f,0.0f);
do{
	// Clear screen
	glClear(GL_COLOR_BUFFER_BIT);

	//glBegin(GL_POINTS); //starts drawing of points
	//glColor3f(1.0f,0.0f,0.0f); //blue color
	//glVertex3f(1.0f,1.0f,0.0f);//upper-right corner
	//glVertex3f(-1.0f,-1.0f,0.0f);//lower-left corner
	//glEnd();//end drawing of points

	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glDrawArrays(GL_TRIANGLES, 0, 3);

	glDisableVertexAttribArray(0);


	// Swap buffers
	glfwSwapBuffers(window);
	glfwPollEvents();

} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
	   glfwWindowShouldClose(window) == 0 );

// Close OpenGL window and terminate GLFW
glfwTerminate();

return 0;

}

hi,

you are using the so called: fixed function pipeline , from opengl … lets say the OLD WAY :slight_smile:

So you dont need shaders and dont need to manage matrix on your own …

thats the best / easy way to begin… later you should turn to shaders.

cu
uwi

Thanks for the answer. When i use same program with Glut it does not work and i have to set glMatrixMode and gluPerspective as well. Is it that glfw does that for me behind the scene?

Hi!

To my mind, you have to learn modern openGL. Dont learn openGL 1.2 or 2.0 style : it’s a loose of time and a lot of bad habits to take…

//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

This is why your code “is working”. By commenting these lines out, glfw will not force the creation of a core profile context. And therefore, it is possible that you’ll get a compatibility context, which supports the old legacy OpenGL. Legacy OpenGL doesn’t require the use of shaders. Or a VAO.

When i use same program with Glut it does not work and i have to set glMatrixMode and gluPerspective as well.

No, you don’t. The default matrices will show the triangles you asked to draw just fine.