Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I started working with OpenGL recently, having previously worked on mods. I'm using GLFW3, GLEW, and whatever tutorials I can find. So far I've come up with this code:

//Headers
#include <windows.h>
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>
#include <string>

//Libraries
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "glfw3.lib")


int main() //Console
//int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show) //Windows
{
    std::string vertex_shader =
"#version 420\n"
"in vec3 vertex_position;\n"
"void main() {\n"
"  gl_Position = vec4(position, 1.0);\n"
"}";

std::string fragment_shader =
"#version 420\n"
"out vec4 frag_colour;\n"
"void main() {\n"
"  frag_colour = (0.5, 0.0, 0.5, 1.0);\n"
"}";

    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    // Create a Window
    int windowWidth = 800;
    int windowHeight = 600;
    bool windowFullScreen = false;
    GLFWmonitor* windowScreen;
    if (windowFullScreen) {
        windowScreen = glfwGetPrimaryMonitor();
    }
    else {
        windowScreen = NULL;
    }
    window = glfwCreateWindow(windowWidth, windowHeight, "Grind v0.1", windowScreen ,NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    //Use OpenGL 4.2
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

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

    //Initialize Glew
    if (glewInit() != GLEW_OK) {
        glfwTerminate();
        return -1;
    }

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
    glEnable (GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

    float points[] = {
        0.0f,  0.5f,  0.0f,
        0.5f, -0.5f,  0.0f,
        -0.5f, -0.5f,  0.0f
    };

    // Vertex Buffer Object
    unsigned int vbo = 0;
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), &points[0], GL_STATIC_DRAW);

    // Vertex Array Object
    unsigned int vao = 0;
    glGenVertexArrays (1, &vao);
    glBindVertexArray (vao);
    glEnableVertexAttribArray (0);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

    // Compile Shader
    unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
    const char* str = vertex_shader.c_str ();
    glShaderSource (vs, 1, &str, NULL);
    glBindAttribLocation(vs, 0, "in_Position"); // Bind a constant attribute location for positions of vertices  
    glCompileShader (vs);
    unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
    const char* strb = fragment_shader.c_str ();
    glShaderSource (fs, 1, &strb, NULL);
    printf(strb);
    glBindAttribLocation(fs, 1, "in_Color"); // Bind another constant attribute location, this time for color  
    glCompileShader (fs);

    // Create Shader Program
    unsigned int shader_programme = glCreateProgram ();
    glAttachShader (shader_programme, fs);
    glAttachShader (shader_programme, vs);
    glLinkProgram (shader_programme);

    // Input Modes
    glfwSetInputMode( window, GLFW_CURSOR, GLFW_CURSOR_NORMAL );
    glfwSetInputMode( window, GLFW_STICKY_KEYS, GL_TRUE );
    glfwSetInputMode( window, GLFW_STICKY_MOUSE_BUTTONS, GL_TRUE );

    while (!glfwWindowShouldClose(window))
    {
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram (shader_programme);
        glBindVertexArray (vao);
        glDrawArrays (GL_TRIANGLES, 0, 3);

        // Swap front and back buffers
        glfwSwapBuffers(window);

        //Check for Escape key (Shutdown)
        if (glfwGetKey( window, GLFW_KEY_ESCAPE ) == GLFW_PRESS && glfwGetWindowAttrib( window, GLFW_FOCUSED )) {
            glfwTerminate();
        }

        // Poll for and process events
        glfwPollEvents();
    }

    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;
}

First of all, should I still be using GLEW? I read somewhere it has issues, but don't know what to use. Secondly, the real reason I'm posting this, the Fragment Shader doesn't seem to work, and I'm not sure about the vertex shader. I tried everything I could think of. It's supposed to give the triangle a purple color, but it doesn't seem to work. Third of all, what should I use to load external files? file_contents seems to need a header I haven't included. Lastly, what other libraries should I be using for an FPS, including audio, key input, and the like.

Thanks A LOT in advance!

share|improve this question
5  
Your fragment shader is not syntactically correct. You should add error logging to your shader compilation steps and you'd have caught your simple typo (you forgot the vec4 on the color assignment constructor). Note that the GLEW questions are largely off-topic as are all "what tech do I use" questions. –  Sean Middleditch Aug 15 '13 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.