-
VastGamesLikes 0Problem Description
Hello, I told you previously about my problem with OpenGL and SFML. I want to generate a red rectangle in my window but when I run the build the black window only appears. I've compiled OpenGL right because I've been able to generate colored shapes prior but I'm not sure what to do. The error I get is
Warning. Compatibility profile not supported on this platform.
Error. Unable to create the context. Retrying without shared context.
Warning. New context created without shared context.
Warning: The created OpenGL context does not fully meet the settings that were requested
Requested: version = 3.3 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = false ; debug = false ; sRGB = false
Created: version = 4.1 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = true ; debug = false ; sRGB = false
Program ended with exit code: 0
I am on a iMac with OS X Capitan using Xcode 8.2.1 with c++14.
Frameworks:
GLEW 2.0
SFML WINDOW
SFML GRAPHICS
SFML SYSTEM
SFML NETWORK
SFML AUDIO
OPEN GL
The code is as followed
Simple_Fragment.glsl
#version 330
out vec4 color;
void main()
{
color = vec4 (1.0, 0.0, 0.0, 1.0);
}
Simple_Vertex.glsl
#version 330
layout (location = 0) in vec2 inVertexPosition;
void main()
{
gl_Position = vec4 (inVertexPosition.x, inVertexPosition.y, 0.0, 1.0);
}
Application.h
#ifndef Application_h
#define Application_h
#include <stack>
#include <memory>
#include "States/Game_States.h"
struct State;
class Application
{
public:
Application();
void runMainGameLoop();
void pushState(std::unique_ptr<State::Game_State> state);
void popState();
private:
std::stack<std::unique_ptr<State::Game_State>> m_states;
};
#endif /* Application_h */
Display.h
#ifndef Display_h
#define Display_h
namespace Display
{
void init();
void close();
void clear();
void update();
void checkForClose();
bool isOpen();
}
#endif /* Display_h */
Application.cpp
#include <stdio.h>
#include "Source/Application.h"
#include "Display.h"
#include "States/Playing.h"
Application::Application()
{
pushState(std::make_unique<State::Playing>(*this));
}
void Application::runMainGameLoop()
{
while (Display::isOpen())
{
Display::clear();
m_states.top()->input();
m_states.top()->update();
m_states.top()->draw();
// do stuff with states
Display::update();
Display::checkForClose();
}
}
void Application::pushState(<std::unique_ptr<State::Game_State> state)
{
m_states.push(std::move(state));
}
void Application::popState()
{
}
Display.cpp
#include "Display.h"
#include <memory>
#include <SFML/Graphics.hpp>
#include <GL/glew.h>
namespace Display
{
constexpr static int WIDTH = 1280;
constexpr static int HEIGHT = 720;
std::unique_ptr<sf::RenderWindow> window;
void init()
{
sf::ContextSettings settings;
settings.depthBits = 24;
settings.majorVersion = 3;
settings.minorVersion = 3; //OpenGL 3.3
window = std::make_unique<sf::RenderWindow>(sf::VideoMode(WIDTH, HEIGHT),
"Window",
sf::Style::Close,
settings);
glewInit();
glViewport(0, 0, WIDTH, HEIGHT)
glEnable(GL_DEPTH_TEST);
window->setMouseCursorVisible(false);
}
void close()
{
window->close();
}
void clear()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
void update()
{
window->display();
}
void checkForClose()
{
sf::Event e;
while (window->pollEvent(e))
{
if (e.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
close();
}
}
}
bool isOpen()
{
return window->isOpen();
}
const sf::Window& get()
{
return *window;
}
}
Main.cpp
#include "Display.h"
int main()
{
Display::init();
Application app;
app.runMainGameLoop();
return 0;
}
Model.cpp
#include <stdio.h>
#include "Model.h"
Model::Model(const std::vector<GLfloat>& vertxPositions)
{
glGenBuffers(1, &m_vertexId)
glBindBuffer(GL_ARRAY_BUFFER, m_vertexId);
glBufferData(GL_ARRAY_BUFFER,
vertexPositions.size() * sizeof(vertexPositions[0]),
vertexPositions.data(),
GL_STATIC_DRAW);
glVertexAttribPointer(0,
2,
GL_FLOAT,
GL_FALSE,
0,
(GLvoid*) 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Model::~Model()
{
glDeleteBuffers(1, &m_vertexId);
}
void Model::bind()
{
glBindBuffer(GL_ARRAY_BUFFER, m_vertexId);
}
void Model::unbind()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
Model.h
#ifndef Model_h
#define Model_h
#include <GL/glew.h>
#include <vector>
class Model
{
public:
Model(const std::vector<GLfloat>& vertxPositions);
~Model();
void bind();
void unbind();
private:
GLuint m_vertexId
};
#endif /* Model_h */
Shader_Loader.cpp
#include <stdio.h>
#include "Shader_Loader.h"
#include <fstream>
#include <sstream>
namespace Shader
{
GLuint compileShader(const GLchar* source, GLenum type)
{
auto id = glCreateShader(type);
glShaderSource(id, 1, &source, nullptr);
glCompileShader(id);
GLint isSuccess;
GLchar infoLog[512];
glGetShaderiv(id, GL_COMPILE_STATUS, &isSuccess)
if(!isSuccess)
{
glCreateShaderInfoLog(id, 512, nullptr, infoLog);
throw std::runtime_error ("Error compiling shader: " + std::string(infoLog));
}
return id;
}
std::string getSource(const std::string sourceFile)
{
std::ifstream inFile ("Data/Shaders/" + sourceFile + ".glsl");
std::string source;
std::stringstream stringStream;
if (!inFile.is_open())
{
throw std::runtime_error ("Could not open file: " + sourceFile);
}
stringStream << inFile.rdbuf();
source = stringStream.str();
return source;
}
GLuint createProgram(GLuint vertexShaderID, GLuint fragmentShaderID)
{
auto id = glCreateProgram();
glAttachShader(id, vertexShaderID);
glAttachShader(id, fragmentShaderID);
glLinkProgram(id);
return id;
}
GLuint loadShader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
{
auto vertexSource = getSource(vertexShaderFile);
auto fragmentSource = getSource(fragmentShaderFile);
auto vertexShaderID = compileShader(vertexSource.c_str(), GL_VERTEX_SHADER);
auto fragmentShaderID = compileShader(fragmentSource.c_str, GL_FRAGMENT_SHADER);
auto programID = createProgram(vertexShaderID, fragmentShaderID);
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
return programID;
}
}
Shader_Loader.h
#ifndef Shader_Loader_h
#define Shader_Loader_h
#include <GL/glew.h>
#include <string>
namespace Shader
{
GLuint loadShader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile);
}
#endif /* Shader_Loader_h */
Shader_Program.cpp
#include <stdio.h>
#include "Shader_Program.h"
#include "Shader_Loader.h"
namespace Shader
{
Shader_Program::Shader_Program(const std::string& vertexShaderFile, const std::string& fragmentShaderFile)
: m_programID (loadShader(vertexShaderFile, fragmentShaderFile))
{
}
Shader_Program::~Shader_Program()
{
glDeleteProgram(m_programID);
}
void Shader_Program::bind()
{
glUseProgram(m_programID);
}
void Shader_Program::unbind()
{
glUseProgram(0)
}
void Shader_Program::bindAttribute(GLuint location, const GLchar* name)
{
glBindAttribLocation(m_programID, location, name);
}
}
Shader_Program.h
#ifndef Shader_Program_h
#define Shader_Program_h
#include <GL/glew.h>
#include <string>
namespace Shader
{
class Shader_Program
{
public:
Shader_Program(const std::string& vertexShaderFile, const std::string& fragmentShaderFile);
~Shader_Program();
void bind();
void unbind();
protected:
void bindAttributes(GLuint location, const GLchar* name);
virtual void bindAttribute() = 0;
private:
GLuint m_programID;
};
}
#endif /* Shader_Program_h */
Simple_Shader.cpp
#include <stdio.h>
#include "Simple_Shader.h"
namespace Shader
{
Simple_Shader::Simple_Shader()
: Shader_Program ("Simple_Vertex", "Simple_Fragment")
{
}
void Simple_Shader::bindAttributes()
{
bindAttribute(0, "inVertexPosition");
}
}
Simple_Shader.h
#ifndef Simple_Shader_h
#define Simple_Shader_h
#include "Shader_Program.h"
namespace Shader
{
class Simple_Shader : public Shader_Program
{
public:
Simple_Shader();
private:
void bindAttributes() override;
};
}
#endif /* Simple_Shader_h */
Gamestate.cpp
#include <stdio.h>
#include "Game_State.h"
namespace State
{
Game_State::Game_State(Application& application)
: m_application (&application)
{
}
}
Game_State.h
#ifndef Game_State_h
#define Game_State_h
namespace State
{
class Game_State
{
public:
Game_State(Application& application);
virtual void input () = 0;
virtual void update () = 0;
virtual void draw () = 0;
protected:
Application* m_application;
};
}
#endif /* Game_State_h */
Playing.cpp
#include <stdio.h>
#include "Playing.h"
#include <iostream>
namespace State
{
Playing::Playing(Application& application)
: Game_State (Application)
, m_model ({0.5, 0.5,
-0.5, 0.5,
-0.5, -0.5,
-0.5, -0.5,
0.5, -0.5,
0.5, 0.5})
{
}
void Playing::input()
{
}
void Playing::update()
{
}
void Playing::draw()
{
m_shader.bind();
m_model.bind();
glDrawArrays(GL_TRIANGLES, 0, 6);
m_model.unbind();
m_shader.unbind();
}
}
Playing.h
#ifndef Playing_h
#define Playing_h
#include "Game_State.h"
#include "../Model.h"
#include "../Shaders/Simple_Shader.h"
namespace State
{
class Playing : public Game_State
{
public:
Playing(Application& application);
void input () override;
void update () override;
void draw () override;
private:
Model m_model;
Shader::Simple_Shader m_shader;
};
}
#endif /* Playing_h */
-
Sonar Systems adminLikes 0
Are you still having this problem?
Login to reply