1
votes

This OpenGL program is written to draw a triangle and it got crashed.

Basically a set of triangle vertices is define as array named coordinate, then this array is passed to a buffer and the method glDrawArrays will draw the triangle based on the mode GL_TRIANGLES start from vertex 0, 3 vertices in total.

Am I right? And could someone show me where is the error? Here is the code:

// Open an OpenGL window
GLFWwindow* window;
int k = 0;

/****Step 1: define vertices in (x, y, z) form****/
const GLfloat coordinates[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f, 1.0f, 0.0f
};
/************************/

/**Step 2: send this triangle vertices to OpenGL through a buffer**/
GLuint vertexBuffer; // identify vertex buffer

void Render(void){
    /************************/
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(0, 3 /*size*/, GL_FLOAT /*type*/, GL_FALSE, 0, (void*)0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    //glDisableVertexAttribArray(0);
    /************************/

//    glClearColor(0., 1., 1., 1.); // blue colour
    glClear( GL_COLOR_BUFFER_BIT );
    // Swap front and back rendering buffers
    glfwSwapBuffers(window);
    //Poll for and process events
    glfwPollEvents();
}

int main( void ) {
    /*Initializing steps here*/


    // Create a windowed mode window and its OpenGL context
    window = glfwCreateWindow(700, 500, "Hello World", NULL, NULL);

    // Make the window's context current
    glfwMakeContextCurrent(window);

    /**Step 2: send this triangle vertices to OpenGL through a buffer**/
    glGenBuffers(1, &vertexBuffer); // generating 1 buffer, put resulting identifier in this buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW);
    /************************/

    // Main loop
    while( glfwWindowShouldClose(window) == 0) {
            // OpenGL rendering goes here...
        Render();
    }
    // Close window and terminate GLFW
    glfwDestroyWindow(window);
    glfwTerminate();
    // Exit program
    exit( EXIT_SUCCESS );
    }

EDIT Here is a screenshot of the output:

enter image description here

UPDATE I found the error, this is because I create the OpenGL context after initializing GLEW. It causes program to crash.

1
Where it crashes? have you checked with the debugger?Jepessen

1 Answers

5
votes

Ok, first of all you should have provided more details, but I figured them by myself. Here is it:

Missed headers:

#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

Makefile:

CFLAGS = -Wall -O2
CFLAGS += $(shell pkg-config --cflags glfw3)
CFLAGS += $(shell pkg-config --cflags glew)
LDFLAGS += $(shell pkg-config --libs glfw3)
LDFLAGS += $(shell pkg-config --libs glew)
SOURCES = main.c
OUT = gl-test
CC = gcc

default: $(SOURCES)
    $(CC) $(CFLAGS) $(LDFLAGS) $(SOURCES) -o $(OUT)

clean:
    -rm -f $(OUT)

.PHONY: default clean

Error occured: Segmentation fault, in next command:

glGenBuffers(1, &vertexBuffer);

But what I see is that glfwCreateWindow() returns NULL, so the error is already in very first instruction. You can figure out why this error happend like this:

static void error_callback(int error, const char *description)
{
    puts(description);
}

int main(void)
{
    glfwSetErrorCallback(error_callback);
    ...
}

And also it worth to modify window creating code to be like that:

// Create a windowed mode window and its OpenGL context
window = glfwCreateWindow(700, 500, "Hello World", NULL, NULL);
if (window == NULL) {
    fprintf(stderr, "Error in glfwCreateWindow\n");
    return EXIT_FAILURE;
}

After this you can see next output in my terminal:

The GLFW library is not initialized
Error in glfwCreateWindow

Please try to do the same and confirm that you have the same problem. If no, please provide output from your terminal.

EDIT 1:

Ok, it seems like first of all you need to do the next just in the main() beginning:

if (!glfwInit()) {
    fprintf(stderr, "Error on GLWF init\n");
    return EXIT_FAILURE;
}

It solves previous error, but I still have segmentation fault error.

EDIT 2:

Second error is that GLEW wasn't initialized. It seems GLEW init must be put when window is already created, like this:

// Make the window's context current
glfwMakeContextCurrent(window);

glewinit_res = glewInit();
if (glewinit_res != GLEW_OK) {
    fprintf(stderr, "Glew init error: %s\n",
            glewGetErrorString(glewinit_res));
    return EXIT_FAILURE;
}

Now I don't have any runtime errors, window shows up, but it only has black background and nothing on it. Anyway, I believe original error is fixed.

EDIT 3:

If you want to see your triangles, just remove glClear() function call.

Here is what I see: screenshot.

Here is modified code that works for me (main.c file): http://pastebin.com/sx5nMQHc