This code works on my friend's computer, but on mine it only shows a blue screen.
#include <glm/glm.hpp>
#include <GL/glew.h>
//#include <TGUI/TGUI.hpp>
//#include <TGUI/Backend/SFML-Graphics.hpp>
#include <iostream>
#include <array>
#include <random>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>//oeo
void pauseExitErrorMessage(const std::string_view msg, int err = 1)
{
std::cout << msg;
std::cin.get();
exit(err);
}
int main()
{
sf::ContextSettings context_sets {};
context_sets.majorVersion = 3;
context_sets.minorVersion = 3;
context_sets.depthBits = 24;
context_sets.stencilBits = 0;
context_sets.attributeFlags = sf::ContextSettings::Default;
sf::Window window {
sf::VideoMode{800, 600},
"OpenGL test 1",
sf::Style::Close,
context_sets
};
window.setVerticalSyncEnabled(true);
window.setActive(true);
glewInit();
// Vertex data
glm::vec3 vertices[] {
{ .5f, .5f, .0f},
{ .5f, -.5f, .0f},
{-.5f, -.5f, .0f},
{-.5f, .5f, .0f}
};
// Indices
GLuint indices[] {
0, 1, 2,
2, 3, 0
};
// Create and bind vertex array object
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Create and bind buffer
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Load data into buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Element buffer object
GLuint EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Descrive how to interpret vertex buffer and enable its interpretation
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
glEnableVertexAttribArray(0);
// Unbind VAO
glBindVertexArray(0);
// Draw only lines
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// Load shaders
sf::Shader vert_shdr;
sf::Shader frag_shdr;
if(!vert_shdr.loadFromFile("../shader.vert", sf::Shader::Vertex))
{
pauseExitErrorMessage("Failed to load vertex shader!\n");
}
if(!frag_shdr.loadFromFile("../shader.frag", sf::Shader::Fragment))
{
pauseExitErrorMessage("Failed to load fragment shader!\n");
}
// Bind shaders
sf::Shader::bind(&vert_shdr);
sf::Shader::bind(&frag_shdr);
bool running {true};
bool fill_shape {true};
while(running)
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
{
running = false;
break;
}
case sf::Event::MouseMoved:
{
break;
}
case sf::Event::KeyPressed:
{
if(event.KeyPressed == sf::Keyboard::F)
{
if(fill_shape)
{
fill_shape = false;
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} else
{
fill_shape = true;
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
}
}
}
// Clear screen
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
// Swap buffers
window.display();
}
return 0;
}
#version 330 core
layout (location = 0) in vec3 pos;
void main()
{
gl_Position = vec4(pos.x, pos.y, pos.z, 1.0);
}
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.0f, 1.0f, 0.25f, 0.0f);
}