I have done a lot of programming in OpenGL before, but somehow I am now unable to draw a triangle...
Here's the code (this is a one-file program). I have no idea which part of the code is failing so I'm posting all of it. All help appreaciated.
This code displays a blank, black window.
#include <MaddCompute.h>
#include <SDL2/SDL.h>
#include <iostream>
#include <GL/glew.h>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
struct Vertex
{
float pos[2];
float color[3];
};
Vertex vertices[3] = {
{{0.0, -1.0}, {1.0, 0.0, 0.0}},
{{-1.0, 1.0}, {0.0, 1.0, 0.0}},
{{1.0, 1.0}, {0.0, 0.0, 1.0}}
};
GLuint CompileShader(GLuint type, string filename)
{
string code = "";
ifstream ifs(filename.c_str());
string line;
while (getline(ifs, line))
{
code += line + '\n';
};
ifs.close();
//cout << code << endl;
//exit(1);
GLuint shader = glCreateShader(type);
char *str = (char*) code.c_str();
glShaderSource(shader, 1, (const GLchar**)&str, NULL);
glCompileShader(shader);
#if 0
int len, ignore;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
char *buffer = new char[len+1];
glGetShaderInfoLog(shader, len, &ignore, buffer);
string log = buffer;
delete buffer;
ofstream ofs((filename + ".log").c_str());
ofs << log << endl;
ofs.close();
#endif
return shader;
};
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cerr << "SDL_Init: " << SDL_GetError() << endl;
return 1;
};
SDL_Window *win = SDL_CreateWindow("MaddCompute OpenGL Shared Context",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_OPENGL);
if (win == NULL)
{
cerr << "SDL_CreateWindow: " << SDL_GetError() << endl;
return 1;
};
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GLContext glcontext = SDL_GL_CreateContext(win);
if (glcontext == NULL)
{
cerr << "SDL_GL_CreateContext: " << SDL_GetError() << endl;
return 1;
};
if (glewInit() != GLEW_OK)
{
cerr << "glewInit fail" << endl;
return 1;
};
if (!glewIsSupported("GL_VERSION_3_0"))
{
cerr << "OpenGL 3.0 not supported!" << endl;
return 1;
};
while (glGetError() != GL_NO_ERROR);
GLuint vshader = CompileShader(GL_VERTEX_SHADER, "vertex.glsl");
GLuint fshader = CompileShader(GL_FRAGMENT_SHADER, "fragment.glsl");
GLuint prog = glCreateProgram();
glAttachShader(prog, vshader);
glAttachShader(prog, fshader);
glLinkProgram(prog);
#if 0
int len, ignore;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
char *buffer = new char[len+1];
glGetProgramInfoLog(prog, len, &ignore, buffer);
string log = buffer;
delete buffer;
ofstream ofs("link.log");
ofs << log << endl;
ofs.close();
#endif
glUseProgram(prog);
if (glGetError() != GL_NO_ERROR)
{
cerr << "Something went wrong in shader compilation." << endl;
return 1;
};
GLint attrPos = glGetAttribLocation(prog, "pos");
if (attrPos == -1)
{
cerr << "couldn't find 'pos' !" << endl;
return 1;
};
GLint attrColor = glGetAttribLocation(prog, "color");
if (attrColor == -1)
{
cerr << "couldn't find 'color' !" << endl;
return 1;
};
GLuint vbo;
glEnableVertexAttribArray(attrPos);
glEnableVertexAttribArray(attrColor);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3*sizeof(Vertex), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(attrPos, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
glVertexAttribPointer(attrColor, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, color));
MC::Context *context = NULL;
try
{
context = new MC::Context(MC::Context::OPENGL);
}
catch (MC::Error e)
{
cerr << "MC::Error: " << e.what() << endl;
return 1;
};
bool quit = false;
unsigned long time = SDL_GetTicks();
while (!quit)
{
SDL_Event ev;
while (SDL_PollEvent(&ev))
{
if (ev.type == SDL_QUIT)
{
quit = true;
};
};
if ((SDL_GetTicks()-time) >= 33)
{
time = SDL_GetTicks();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(win);
};
};
delete context;
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
};