I'm trying to build an OpenGL App with glew/glfw. I've downloaded the binaries, placed them in the root of my folder, added the paths to the include and lib directories and told my project to require glew32.lib, GLFW.lib and opengl32.lib.
I even copied glew32.lib to the root directory because my project couldn't see it.
I must keep all the dependencies in the project directory since I will be distributing this. I'm at a loss.
Now when I run my program, it fails at glewInit()
This is my implementation so far:
#include "Common.h"
GameEngine::GameEngine()
{
InitWithSize(1024, 768);
InitInput();
}
void GameEngine::InitWithSize(int _width, int _height)
{
try {
// Initialise GLFW
if( !glfwInit() )
throw std::exception("Failed to initialize GLFW\n");
//glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
if( !glfwOpenWindow( _width, _height, 0,0,0,0, 32,0, GLFW_WINDOW ) )
throw std::exception("Failed to initialize GLFW\n");
glfwSetWindowTitle( "Tutorial 01" );
// Initialize GLEW
if (glewInit() != GLEW_OK)
throw std::exception("Failed to initialize GLEW\n");
} catch ( std::system_error const& err) {
fprintf(stdout, "System Error: %s", err.what());
glfwTerminate(); // Free glfw if it has been allocated
// Try Again
this->InitWithSize(_width, _height);
} catch( std::exception const& err) {
fprintf(stdout, "Exception Found: %s", err.what());
} catch ( ... ) {
fprintf(stdout,"Unknown Exception Occurred\n");
}
}
void GameEngine::InitInput()
{
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
}
void GameEngine::StartGameLoop()
{
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
}
void GameEngine::InitTestData()
{
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
}
With my Common Header:
#ifndef _COMMON_H
#define _COMMON_H
// OpenGL Libraries
#define GLEW_STATIC
//#pragma comment(lib, "glew32.lib")
#include <GL\glew.h>
#include <GL\glfw.h>
// Core Libraries
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <fstream>
// C++ 11 Libraries
#include <memory>
#include <exception>
#include <thread>
#include <chrono>
// Manager Classes
#include "ThreadManager.h"
#include "GameEngine.h"
#include "ShaderManager.h"
// Lesser Classes
#include "Shader.h"
#endif
I know this answer comes a bit late, but I don't see you making the OpenGL context current by calling glfwMakeContextCurrent(window). You have to do that before calling glewInit()
If no error is returned from glewInit() it returns 0. For some reason the return wasn't comparing well to GLEW_OK but if the value is anything but 0, there's an error.
if(glewInit()) {
//Handle Error
}
You are using Core OpenGL version 3.3 so you must specify you are using "new" and by GLEW terms "experimental" API. Add this line before calling glewInit();
glewExperimental=GL_TRUE;
Here is a complete init code from my engine .It is for 4.2 but should work the same for you:
void Engine::InitWithGLFW(){
if(!glfwInit()){
exit(EXIT_FAILURE);
}
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
//glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
glfwDisable(GLFW_AUTO_POLL_EVENTS);
#ifdef DEBUG
glfwOpenWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
#endif
if(!glfwOpenWindow(_width,_height,8, 8, 8, 8, 24, 8,GLFW_WINDOW)){
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetWindowTitle("XDEngine V-1.0");
InitGlew();
}
void Engine::InitGlew(){
glewExperimental=GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_CLAMP);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glfwSetWindowSizeCallback(Reshape);
}
I had a similar problem and finally got the solution on opengl.org.
It seems that GLEW developers haven't fixed a core context problem yet.
Though there is a quite easy solution:
glewExperimental=TRUE;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
cout<<"glewInit failed, aborting."<<endl;
}
HTH
Compare the glewInit() return type with GLenum
if (glewInit() != GLEW_OK) {
printf("glew initialization failed!");
}
Related
I have a problem with opengl and SDL2.
When I want to trigger glGenBuffers, the application crashes.
Does anyone know where I made a mistake?
#define GLEW_STATIC
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <GL/glew.h>
using namespace std;
SDL_Window*win;
SDL_Event event;
const Uint8*keystate = SDL_GetKeyboardState(NULL);
SDL_GLContext context;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
glewExperimental = GL_TRUE;
glewInit();
win = SDL_CreateWindow("opengl",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,900,800,SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(win);
SDL_GL_MakeCurrent(win,context);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
glClearColor( 1.0, 1.0, 1.0, 1.0 );
float positions[6] =
{
-0.5f,-0.5f,
0.0f,0.5f,
0.5f,0.0f
};
GLuint buffer;
glGenBuffers(1,&buffer); //here
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6,positions,GL_STATIC_DRAW);
while(true)
{
while(SDL_PollEvent(&event))
{
if(event.type==SDL_QUIT) return 0;
}
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES,0,3);
SDL_GL_SwapWindow(win);
}
}
I really don't know where the problem is, I would really be grateful to have someone help me.
he GLEW library has to be initialized, by glewInit, after the OpenGL context has become current by SDL_GL_MakeCurrent(win,context). See Initializing GLEW.
win = SDL_CreateWindow("opengl",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
900,800,SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(win);
SDL_GL_MakeCurrent(win,context);
if (glewInit() != GLEW_OK)
return 0;
When you do it before, the initialization of glew fails and glewInit doesn't return GLEW_OK.
Here is the code which I'm trying to run:
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
// Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
glClear( GL_COLOR_BUFFER_BIT );
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
It only shows a black window with the text:
Failed to initialize GLFW
Also, I've already linked GL, GLEW, SDL2 and glfw3 to the build options.
Upon execution, the background should be rendered as a darker-blue, but I have clearly missed something. The window is instead rendered with a background that is identical to the image immediately behind it (e.g. other open windows or the desktop, etc.). I cannot identify the problem.
Currently I am not using any -std during compilation and I am utilizing the following linkages with the output executable:
-lGL -lGLU -lGLEW -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXcursor -lXinerama
Here are the contents of my .cpp file:
#include <stdio.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
//Initialize GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failes to intialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a Window and create it's OpenGL context
window = glfwCreateWindow( 1024, 768, "playground", NULL, NULL);
if( window == NULL) {
fprintf( stderr, "Failed to open GLFW window.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
glfwTerminate();
return 0;
}
I am an OpenGL newcomer. Feel free to remark on the over-all structure or if anything should/must be rewritten entirely. Thanks!
You just forgot to clear your window in your render loop:
do{
glClear(GL_COLOR_BUFFER_BIT); // Clear background with clear color.
glfwSwapBuffers(window);
glfwPollEvents();
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
glClear is often one of the first call of every render loop using OpenGL, giving you a new fresh frame to work on.
I've tried to follow this tutorial for OpenGL in C but when it comes to the second tutorial, the one that is supposed to draw a triangle on the window, I couldn't see anything. So this is what I did, I took the code that creates the OpenGL context, window and stuff and I tried to make it simpler: instead of using VAO I tried glBegin/glEnd.
I get this error: 1282 "invalid operation". I'm just using the same sentences taking directly from my LWJGL project. The main loop is so simple I can't understand how it does not work and the 1282 error is not giving me any information. Why do I still get an error?
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#define GLEW_STATIC
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
using namespace glm;
void checkErrors() {
int error = glGetError();
if (error != GL_NO_ERROR) {
printf("%s (%d)\n", gluErrorString(error), error);
}
}
int main(void)
{
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if (window == NULL){
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glEnd();
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
// Check for errors
checkErrors();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
return 0;
}
glBegin/glEnd, along with all other immediate mode drawing functions and some more, have been deprecated, and cannot be used with an OpenGL 3.1 (and up) core and forward compatible contexts.
You can try requesting a 3.0 compatibility context (which includes all of the deprecated functionality). To do this, remove the glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); and glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); lines, and change the minor version hint to 0. Indeed, according to the OpenGL wiki, you should not explicitly request a forward compatible context with 3.1 and newer anyway. However, your best bet is to figure out what's wrong with the VAO code instead of mucking around with deprecated functionality.
I've created a very simple implementation of a GLFW window. My implementation looks like so
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include glfw for window handling
#include <GLFW/glfw3.h>
int SCREEN_WIDTH = 1280;
int SCREEN_HEIGHT = 720;
GLFWwindow* window;
int main() {
if(!glfwInit()) {
fprintf( stderr, "Failed to initialize GLFW!\n" );
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Title", NULL, NULL);
if( !window )
{
fprintf( stderr, "Failed to create window!\n" );
glfwTerminate();
return -1;
}
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
do {
glfwSwapBuffers(window);
} while(!glfwGetKey(window, GLFW_KEY_ESCAPE));
glfwTerminate();
return 0;
}
It compiles fine, but when I run the application it just "freezes", not showing the correct clear color or anything. It just thinks like crazy (that "thinking"-cursor icon in Windows 7 spins and never stops).
I'm wondering why it freezes like such, does anyone have an idea?
EDIT:
Found the solution to my problem. I was learning from a GLFW2 example, but having the newest version (GLFW3) required me to redo the code some; the thing I didn't realize was that the glSwapBuffers(window) call doens't call glfwPollEvents() by itself, giving me the problem I had.
You're not clearing the buffers, hence the clear colour is not showing through.
You're not adding any kind of delay, so you're swapping empty buffers flat out ("thinking like crazy").