glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}
When I execute this then comes a message:
Failed to initalize GLEW
And what is the problem? This message was written by me
What can I do?
Make sure that the OpenGL context is current before initializing GLEW.
Related
here's my code:
#include <iostream>
#include <GLFW/glfw3.h>
int main(void){
GLFWwindow* window;
if(!glfwInit()){
std::cout << "Failed to initialize GLFW!" << std::endl;
return -1;
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if(!window){
glfwTerminate();
std:: cout << "Failed to initialize Window!" << std::endl;
return -1;
}
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)){
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I successfully linked it and compile, but I'm having a runtime error
it says that glfw failed to initialize.I tried to use glfw in C but this error shows
Wayland: Failed to connect to display
The GLFW library is not initialized
main: ./src/posix_thread.c:64: _glfwPlatformGetTls: Assertion `tls->posix.allocated == GLFW_TRUE' failed.
Aborted (core dumped)
. I'm using popOS 22.04 LTS, I'm new to c++ and glfw pls help me fix this problem
I've been working with SDL2 for a long time and now I wanted to switch to GLFW with Dear ImGui. So I downloaded GLFW and integrated it into the project. The simple GLFW window works without problems. Then I integrated ImGui and I get these errors all the time:
Failed to initialize OpenGL loader!
Assertion failed: (bd != __null && "Did you call ImGui_ImplOpenGL3_Init()?"), function ImGui_ImplOpenGL3_NewFrame, file imgui_impl_opengl3.cpp, line 337.
although I'm on line 33:
ImGui_ImplOpenGL3_Init();
I have never worked with ImGui before and would be really happy if you could help me
My code:
int main() {
//init glfw
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW" << std::endl;
return -1;
}
//create window
GLFWwindow* window = glfwCreateWindow(640, 480, "Text Editor", NULL, NULL);
if (!window) {
std::cout << "Failed to create window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//init glew
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui::StyleColorsDark();
ImGui_ImplOpenGL3_Init();
//main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}
And my includes:
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
Thank you for your answer :)
Your code runs fine on my machine, it didn't crash at the ImGui_ImplOpenGL3_NewFrame function, but at ImGui::Render(); which might be the problem. You need to render in the order below:
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); // <-- Added
// do stuffs here
ImGui::Render();
ImGui::EndFrame(); // <-- Added
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
I began to study OpenGL on the course.Download glew-2.1.0(64) and glfw-3.2.1(64). Like everything connected and IDE not swear but initialization produces the following output in the command line:
Error initialization GLEW: Missing GL version
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
const char* APP_TITLE = "Introduction in modern openGL";
const int gWindowWidth = 800;
const int gWindowHeight = 600;
int main()
{
if (!glfwInit())
{
std::cerr << "Failed in initialization GLFW" << std::endl;
return -1;
}
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* pWindow = glfwCreateWindow(gWindowWidth, gWindowHeight, APP_TITLE, NULL, NULL);
if (pWindow)
{
std::cerr << "Failed in create Windows" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(pWindow);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
std::cerr << "Error initialization GLEW: " << glewGetErrorString(err) << std::endl;
glfwTerminate();
return -1;
}
while (!glfwWindowShouldClose(pWindow))
{
glfwPollEvents();
glfwSwapBuffers(pWindow);
}
glfwTerminate();
return 0;
}
What is the problem? I searched on the Internet, but there are solutions to problems of the type:
add glfwMakeContextCurrent(game_window); before glewInit()
Very stupid mistake
I wrote
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
I should have
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
I'm currently trying to draw a simple triangle with GLFW and GLEW.
I'm using CLion (which is itself using cmake with cygwin) on a Win 8.1 x64 PC.
My problem is : glewInit() returns error 1 (Missing GL version) when initialized. People (like in this thread) had a problem related to the OpenGL context, but mine is already created before I call glewInit().
Here is my code :
#include <iostream>
#include <stdlib.h>
#include <vector>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
//...
if(!glfwInit()) {
std::cerr << "Couldn't initialize GLFW" << std::endl;
}
glfwSetErrorCallback(glfw_error_callback);
glfwWindowHint(GLFW_SAMPLES, 4); //16x antialiasing sisi rpz
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if(window == NULL) {
std::cerr << "Failed to open GLFW window. Are your drivers up-to-date ?" << std::endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
if(!glfwGetCurrentContext()) {
std::cerr << "Couldn't create OpenGL context" << std::endl;
exit(EXIT_FAILURE);
}
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err != GLEW_OK) {
std::cerr << "Failed to initialize glew : " << glewGetErrorString(err) << " (" << err << ")" << std::endl;
}
and my CMakeLists.txt :
cmake_minimum_required(VERSION 2.8.4)
project(MY_PROJECT)
set(SOURCE_FILES main.cpp Host.cpp GraphicWindow.cpp)
add_executable(MY_PROJECT ${SOURCE_FILES})
target_link_libraries(MY_PROJECT wsock32 ws2_32)
#GL stuff
include_directories(lib/glew-1.11.0/include/)
include_directories(lib/glfw-3.0.4.bin.WIN64/include/)
include_directories(lib/glm)
target_link_libraries(MY_PROJECT glfw3 glu32 gdi32 opengl32 glew)
I set up a minimal application to open a blank window with GLFW3:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
void glfwErrorCallback(int error, const char *description)
{
std::cerr << "GLFW error " << error << ": " << description << std::endl;
}
int main(int argc, char **argv)
{
GLFWwindow* window;
glfwSetErrorCallback(glfwErrorCallback);
if(!glfwInit())
{
std::cerr << "Failed to initialize GLFW...\n";
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1024, 768, "GLFW window", NULL, NULL);
if(!window)
{
std::cerr << "Failed to open GLFW window...\n";
glfwTerminate();
return -1;
}
glewExperimental = GL_TRUE;
if (glewInit())
{
std::cerr << "Failed to initialize GLEW...\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
It results in the following error:
GLFW error 65540: Context profiles only exist for OpenGL version 3.2 and above
Failed to open GLFW window...
The application is run on Linux with Bumblebee's optirun. The code works when using freeglut instead of GLFW. What is wrong with the code that results in the error?
This is pretty simple:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // Major = 4
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Major was 4, now it is 3.
// Minor = ??? [Something < 2]
You need to use glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3); instead for the second hint.