Results 1 to 5 of 5

Thread: How this code is working?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2015
    Posts
    13

    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\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;
    }

  2. #2
    Senior Member Regular Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    138
    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
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    The OpenGL and VULKAN Podcast ( OpenGL2GO ) : OpenGL & Vulkan Podcast
    ---------------------------------------------------------
    timbling.com - Indie TeamUp

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2015
    Posts
    13
    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?

  4. #4
    Member Contributor
    Join Date
    Mar 2014
    Posts
    59
    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...

  5. #5
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    Code :
    //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.

Similar Threads

  1. OpenGL code not working
    By openglnoob123 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-03-2014, 07:05 AM
  2. gluLootAt not working in this code..
    By kawaikx15 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-26-2014, 07:19 AM
  3. Replies: 1
    Last Post: 11-18-2011, 08:13 AM
  4. code not working
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-26-2001, 06:22 AM
  5. code not working!!! :-(
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-14-2001, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean