0

Been a while since I've needed to ask a question,

I'm trying out SDL2 and OpenGL (3.3 which is the compatibility limit with mesa) because the GLSL really interests me, however on my work machine I learned very quickly that it isn't easy to get things to work. Every tutorial I've used and even the Mesa demos themselves use headers that don't come with Ubuntu's base GL library, I've already resigned and installed GLEW but it doesn't feel right continously adding libraries to make things work, the GL headers I have are:

glcorearb.h, glew.h, glext.h, gl.h, gl_mangle.h, glu.h, glu_mangle.h,
glxew.h, glxext.h, glx.h, glxint.h, glx_mangle.h, glxmd.h, glxproto.h,
glxtokens.h, wglew.h

I tried following LazyFoo's tutorials but did not have the same result of getting a white quad to appear. I followed opengl-tutorial's tutorials and did not get the same result of a white triangle to appear (it mentions to not worry if you don't see it at first, but doesn't explain what to do in the case that it doesn't (I tried to follow the rest of the tutorial, but I'm writing this in C and not C++ so I'm worried about straying too far from the tutorials' and confusing the issue further. I've installed SDL2 and made sure I had everything. This is the code I have in my current SDL2/GL program, it does not at all reveal a white triangle, it's a combination of tutorials, but I've read all the SDL API material I could to make sure nothing on the SDL side affects what GL tries to do.

#define SDL_ASSERT_LEVEL 3

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_assert.h>
#include <SDL2/SDL_version.h>
#include <SDL2/SDL_events.h>
#include <GL/gl.h>

int main(){
    SDL_version compiledWith, linkedWith;
    SDL_VERSION(&compiledWith);
    SDL_GetVersion(&linkedWith);
    if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0){
        fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }
    SDL_Log("\nCompiled with: %d.%d.%d\n", compiledWith.major,
            compiledWith.minor, compiledWith.patch);
    SDL_Log("\nLinked with: %d.%d.%d\n", linkedWith.major,
            linkedWith.minor, linkedWith.patch);
    SDL_Window* window = SDL_CreateWindow("SDL2/OpenGL Demo", 0, 0, 640, 480,
            SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);

    //Can now make GL calls after the below line
    SDL_GLContext glContext = SDL_GL_CreateContext(window);

    GLuint vertexArrayID;
    glGenVertexArrays(1, &vertexArrayID);
    glBindVertexArray(vertexArrayID);
    static const GLfloat gVertexBufferData[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
    };
    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gVertexBufferData),
            gVertexBufferData, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(
            0,
            3,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0
    );
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapWindow(window);

    SDL_Event theEvent;
    bool running = true;

    while(running){
        while(SDL_PollEvent(&theEvent)){
            switch(theEvent.type){
                case SDL_QUIT:
                    SDL_Log("\nQuit request acknowledged\n");
                    //Finish up GL usage
                    SDL_GL_DeleteContext(glContext);
                    //Finish up SDL usage
                    SDL_Quit();
                    running = false;
                    break;
                default:
                    break;
            }
        }
    }
    return 0;
}

I use gcc main.c -lSDL2 -lGL -o test for linking, I suspect I might be missing linkage libraries but I'm not sure where I can check if I am or not, the compiler doesn't warn me about anything it can't find unless I follow a tutorial that uses something I don't have.

To conclude since this is a longer post than anticipated, the question is:

  • Am I missing any important library to actually get this to work on my system (Ubuntu 15.04 Intel Haswell Mobile x86/MMX/SSE2)?
  • Did I miss something in my code that is necessary to see the white triangle?
A016090
  • 499
  • 1
  • 4
  • 11
  • There are some issues with your code. First of all, I don't know where you get your opengl function pointers from. You might get away with directly linking those, but your compiler should complain about missing function declarations. Second, your code actually clears the window after drawing, Third, you are not actually drawing in the loop. And finally, you don't even try to get a modern GL context, so you will end up with a legacy version and unable to get 3.3 on mesa. – derhass May 30 '15 at 18:52
  • I appreciate the response, but could you elaborate? No warnings show up and I'm not actively suppressing any. What links should I be adding at compilation, do you have an example of an online resource that can prevent that in the future because the OpenGL 3.3 API does not mention them in the function description. On the second point, I've moved `glClearColor()` and `glClear()` prior and nothing has resulted for it. On the last two points, I'll try and find something but an example or at least mentioning the missed function/practice would be more helpful. Thanks for at least responding though. – A016090 May 30 '15 at 19:15
  • 1
    You probably don't get any compiler warnings because calling undeclared functions is valid C, consider using the `-Wall` flag for gcc. The point of linking the GL functions is that you _don't_ link them at all. There is no reuirement that these functions are exported by the GL library at all. You have to use the GL extension mechanism to use them. Maybe [this answer](http://stackoverflow.com/questions/30268061/mesa-linux-gl-h-does-not-contain-modern-opengl/30270794#30270794) can explain that in more detail. – derhass May 30 '15 at 19:22
  • Also note that the reason why that tutorials has the "don't worry if it does not appear" remark is because _the code there is not valid GL and actually should not draw anything with a confoming implementaion_. – derhass May 30 '15 at 19:25

0 Answers0