1

I am looking to create a simple OpenGL example on which to build. The following program compiles (Ubuntu 14.04, freeglut) and displays a GL_POINT of size 10 in the middle of the screen. The problem is that it does this regardless of what values I put into the Vertices array.

I have tried multiple different way to define the Vertices, such as, floats, in a struct and the GLfloats shown but the screen position does not change.

#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>

GLuint VBO;

static void RenderSceneCB()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glPointSize(10.0);
    glDrawArrays(GL_POINTS, 0, 1);
    glDisableVertexAttribArray(0);
    glutSwapBuffers();
}


static void InitializeGlutCallbacks()
{
    glutDisplayFunc(RenderSceneCB);
}

static void CreateVertexBuffer()
{
    GLfloat Vertices[] = {  0.5f, 0.5f, 0.0f  };
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Example");

    InitializeGlutCallbacks();

    GLenum res = glewInit();
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      return 1;
    }

    printf("GL version: %s\n", glGetString(GL_VERSION));
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    CreateVertexBuffer();
    glutMainLoop();

    return 0;
}
StuGrey
  • 1,479
  • 9
  • 20
  • That's strange. I compiled your program, and the point changes it's location as I alter `Vertices`. Maybe the problem is in your IDE, which runs one and the same version of the program? – Sergey Nov 11 '16 at 12:38
  • Thanks @sergey I am not using an IDE, I am just compiling and running with the following command: g++ -Wall example.cpp -lGL -lglut -lGLEW -o example && ./example – StuGrey Nov 11 '16 at 12:40
  • @Sergey: How do you change the vertex position? Do you change the number literals in the source code and recompile? If so how does the change look like? – datenwolf Nov 11 '16 at 13:09
  • @datenwolf Yes, I change the literals and recompile. For example, `0.5f, 0.5f` means that the point is in the middle of the first quadrant of the window (i.e. it's displaced towards upper right corner). If I change those numbers to `0.0f, 0.0f`, the point is in the center of the window. – Sergey Nov 11 '16 at 13:17
  • That is very strange, with `GLfloat Vertices[] = { 0.5f, 0.5f, 0.0f };` the point is in the centre of the window for me – StuGrey Nov 11 '16 at 13:20
  • That's strange. Are you sure g++ is correctly compiling the program after the changes and replacing the output file? Also, (0,0,0) should appear in the center of the window as Sergey said. – Banex Nov 11 '16 at 15:53
  • Yes, i've tried deleting the output before running the program to no avail. Regardless of what I set the floats to in `Vertices` the point appears in the centre. – StuGrey Nov 11 '16 at 16:09
  • Where are your shaders? You're using API calls for generic vertex attributes, which are only supported in combination with shaders. – Reto Koradi Nov 12 '16 at 05:55

0 Answers0