PDA

View Full Version : How this code is working?



mahmad
01-29-2015, 11:53 PM
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\n" );
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.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
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;
}

uwi2k2
01-30-2015, 12:14 AM
hi,

you are using the so called: fixed function pipeline , from opengl .. lets say the OLD WAY :-)

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

mahmad
01-30-2015, 12:17 AM
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?

__bob__
01-30-2015, 04:46 AM
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...

Alfonse Reinheart
01-30-2015, 06:37 AM
//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.