#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
glewInit();
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
std::cout << glGetError() << std::endl;
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Despite setting glClearColor, only a black screen is shown. Drawing the triangle doesn't work either, and glGetError returns 0 everywhere. What am I doing wrong?
(I know this is a legacy way to render a triangle, I'm just trying to test if opengl is working, and apparently, it isn't.)
You cannot use the legacy OpenGL (glBegin/glEnd sequences) with a Core profile OpenGL Context (GLFW_OPENGL_CORE_PROFILE) you must use a Compatibility profile OpenGL context (GLFW_OPENGL_COMPAT_PROFILE):
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
Related
Hello it took me two days to figure out why my window always closed instand.
It was because i called gladLoadGL(glfwGetProcAddress) before glfwMakeContextCurrent(window).
This does NOT work:
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
gladLoadGL(glfwGetProcAddress);
GLFWwindow * window = glfwCreateWindow(600, 600, "agag", NULL, NULL);
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.2f, 0.5f, 0.6f, 1);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}
But when i call gladLoadGL(glfwGetProcAddress); AFTER glfwMakeContextCurrent(window); it DOES work!
Do i have load glad everytime i am switching the window? (maybe later i have to)
But is it really like that or is it a bug?
When i write it before glfwMakeContextCurrent the error:
-1073741819 (0xC0000005)
comes up.
I am trying to get started with openGL and GLFW in c++. My expectation is that the following code will simply render a square on the screen, but I just get a black screen.
The actual setup is working though, because if I add glClearColor(0.1f, 0.4f, 0.8f, 1.0f);, I do indeed get a blue screen. But what is wrong with my use of GL_QUADS?
#include <iostream>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
//GLFW
#include <GLFW/glfw3.h>
const GLint WIDTH = 800, HEIGHT = 600;
int main() {
glfwInit();
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint( GLFW_RESIZABLE, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Isosurface Stuffing", nullptr, nullptr);
int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit()) {
std::cout << "Failed to create GLEW" << std::endl;
return -1;
}
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glViewport(0, 0, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, screenWidth, 0.0f, screenHeight, 0.0f, 1.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glfwPollEvents();
glColor3f(0.5f, 0.5f, 0.5f);
glBegin(GL_QUADS);
glVertex2f(100, 100);
glVertex2f(200, 100);
glVertex2f(200, 200);
glVertex2f(100, 200);
glEnd();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
You cannot use the legacy fixed-function pipeline instructions (glBegin, glEnd, glVertex2f, glMatrixMode, glLoadIdentity, glOrtho) in a Core profile OpenGL context (GLFW_OPENGL_CORE_PROFILE). You have to use Compatibility profile OpenGL Context:
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
If your system does not allow you to create a compatibility profile context (e.g. Mac), you must use either an OpenGL 2.0 context or a modern OpenGL with Shaders and Vertex Array Objects. A nice tutorial for that is LearnOpenGL
I was following the OpenGL tutorial from Freecodecamp.org (https://www.youtube.com/watch?v=45MIykWJ-C4) and I got this error while following the "Window" part of the tutorial.
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 800, "openGLTest", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
gladLoadGL();
glViewport(0, 0, 800, 800); //error starts when I wrote this
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
This error keeps happening even though my Additional Dependencies is set to glfw3.lib;opengl32.lib;%(AdditionalDependencies).
My GLFW window renders but the title bar does not render like it does in the tutorial I am following.
I am using fedora on wayland.
Here is the code :
#include <GLFW/glfw3.h>
int main(void) {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit()) return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
the output is here:
https://imgur.com/a/3kE94OI
I am trying to set the background color to blue, but glClearColor is completely ignored.
Code:
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(640, 480, "3D Shooter", NULL, NULL);
glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
You seem to be missing a
glfwMakeContextCurrent(window);
https://www.glfw.org/docs/3.3/quick.html