Basically, I want to start writing my OpenGL code the "proper" way, i.e. not using deprecated functions.
Here's what I've come up with so far for drawing a green triangle to the screen. I've come up with this by means of the scant online resources for GL 3.3+ and the OpenGL superbible.
Sorry for the massive chunk of code, but any part of it could be relevant, so I figured it's probably safest just to post the entire thing.
#include <iostream>#include <SFML/Window.hpp>#define GLEW_STATIC#include <GL/glew.h>#include "math3d.h"int main(){ const int W = 800; const int H = 600; sf::Window window(sf::VideoMode(W, H), "Non-deprecated OpenGL", sf::Style::Close); bool keys[sf::Key::Count]; glewInit(); // Triangle vertex data GLfloat triangle[] = { -1.0f, -1.0f, -5.0f, 1.0f, -1.0f, -5.0f, 0.0f, 1.0f, -5.0f }; GLfloat colour[] = { 0.5f, 1.0f, 0.0f }; // Create a buffer to hold the data GLuint triangleBuffer; glGenBuffers(1, &triangleBuffer); // Hint that we'll be buffering a vertex array... glBindBuffer(GL_ARRAY_BUFFER, triangleBuffer); // And finally put the data into the buffer glBufferData(GL_ARRAY_BUFFER, sizeof(triangleBuffer), &triangleBuffer, GL_STATIC_DRAW); // Initialise OpenGL stuff glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set up some basic shaders const GLchar *vertShaderCode = "#version 140\n" "uniform mat4 mvpMatrix;\n" "in vec4 vertex;\n" "in vec3 inColour;\n" "out vec3 outColour;\n" "void main()\n" "{\n" "gl_Position = mvpMatrix * vertex;\n" "}\n"; const GLchar *fragShaderCode = "#version 140\n" "in vec3 inColour;\n" "out vec3 outColour;\n" "void main()\n" "{\n" "outColour = inColour;\n" "}\n"; // Create some new shaders GLuint vertShader = glCreateShader(GL_VERTEX_SHADER); GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); // Put the code into them glShaderSource(vertShader, 1, &vertShaderCode, NULL); glShaderSource(fragShader, 1, &fragShaderCode, NULL); // Compile them glCompileShader(vertShader); glCompileShader(fragShader); GLint status; glGetShaderiv(vertShader, GL_COMPILE_STATUS, &status); if(status == GL_FALSE) { std::cerr << "Failed to load vertex shader" << std::endl; GLint logLength = 0; glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &logLength); GLchar *log = new GLchar[logLength]; glGetShaderInfoLog(vertShader, logLength, &status, log); std::cerr << log << std::endl; delete[] log; } glGetShaderiv(fragShader, GL_COMPILE_STATUS, &status); if(status == GL_FALSE) { std::cerr << "Failed to load fragment shader" << std::endl; GLint logLength = 0; glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength); GLchar *log = new GLchar[logLength]; glGetShaderInfoLog(fragShader, logLength, &status, log); std::cerr << log << std::endl; delete[] log; } // Link them into a program to use in rendering GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertShader); glAttachShader(shaderProgram, fragShader); glLinkProgram(shaderProgram); glUseProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status); if(status == GL_FALSE) { std::cerr << "Failed to link shader program" << std::endl; GLint logLength = 0; glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &logLength); GLchar *log = new GLchar[logLength]; glGetProgramInfoLog(shaderProgram, logLength, &status, log); std::cerr << log << std::endl; delete[] log; } // Get the location of the mvp matrix in the vertex shader GLint mvpmLocation = glGetUniformLocation(shaderProgram, "mvpMatrix"); // And also get the location of the colour and vertex attributes GLint colourLocation = glGetAttribLocation(shaderProgram, "inColour"); GLint vertexLocation = glGetAttribLocation(shaderProgram, "vertex"); // Construct a projection matrix M3DMatrix44f projectionMatrix; m3dMakePerspectiveMatrix(projectionMatrix, 35.0f, W / H, 0.0f, 100.0f); // And a modelview matrix M3DMatrix44f modelviewMatrix; m3dLoadIdentity44(modelviewMatrix); // And combine them into a modelview-projection matrix! M3DMatrix44f mvpMatrix; m3dMatrixMultiply44(mvpMatrix, projectionMatrix, modelviewMatrix); while(window.IsOpened()) { sf::Event e; while(window.GetEvent(e)) { switch(e.Type) { case(sf::Event::Closed): window.Close(); break; case(sf::Event::KeyPressed): if(e.Key.Code == sf::Key::Escape) window.Close(); else keys[e.Key.Code] = true; break; case(sf::Event::KeyReleased): keys[e.Key.Code] = false; break; default: break; } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Tell the shader about our modelview projection matrix... glUniformMatrix4fv(mvpmLocation, 1, GL_FALSE, mvpMatrix); // Give it our triangle's vertices and colour glBindBuffer(GL_ARRAY_BUFFER, triangleBuffer); glVertexAttribPointer(vertexLocation, 9, GL_FLOAT, GL_FALSE, 0, triangle); glVertexAttribPointer(colourLocation, 3, GL_FLOAT, GL_FALSE, 0, colour); glUseProgram(shaderProgram); // Finally, we can do some drawing! glDrawArrays(GL_TRIANGLES, 0, 3); window.Display(); } // Remove our buffer objects glDeleteBuffers(1, &triangleBuffer);}
I've fiddled around with it for a while now, and I'm clueless what the problem is. The shaders are compiling and linking fine, so I don't think it's those.
Thanks in advance :)