Problem with glew.h

Started by
14 comments, last by MarkS_ 8 years, 6 months ago

Hi all, I try use glew. This my code


if (glewInit() == GLEW_NO_ERROR)
    {
      GLuint vertexbuffer;
      glGenBuffers(1, &vertexbuffer);
    }

and I get an error on glGenBuffers An unhandled exception of type 'System.AccessViolationException' occurred in ManagedLib.dll, in compiled my dll c++/cli

Advertisement

I believe you still have to create a OpenGL Context before using OpenGL Calls like that.

https://open.gl/context

I create context on event form_load


GLManagedWrapper::GLManagedWrapper(INT_PTR hWnd)
	{

		if (!glewInit()) return;
		glewExperimental = GL_TRUE;


		m_Hwnd = (HWND)hWnd;
		if (!m_Hwnd) return;

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT));
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return;

		free(rect);

		m_Hdc = GetDC(m_Hwnd);

		if (m_Hdc)
		{
			GLSetPixelFormat(m_Hdc);
			ReSizeGLScene(m_Width, m_Height);
			InitGL();
		}
	}

I try minimal example and have error


#include "TestGlew.h"

using namespace TestTest;

void Test1::Test()
{
	glewExperimental = GL_TRUE;
	glewInit();
	GLuint vertexBuffer;
	glGenBuffers(1, &vertexBuffer);
	printf("%u\n", vertexBuffer);
}

and C# call dll


var t = new Test1();
            t.Test();

As you are using C# i assume your OS is Windows?

I had a similary error once, where everything worked fine until i tried to create the first buffer in OpenGL.

Unfortunately i don't remember the excact solution an have currently no acces to my code, but i believe it was some wrong header/header order.

Yes, Windows 7


#define GLEW_STATIC
#include <glew.h>
#include <stdio.h>

namespace TestTest
{
	public ref class Test1
	{
	public:
		void Test();
	};
};

and include glew32.lib

Shouldn't it be glew32s.lib if you define GLEW_STATIC

I replaced to glew32s.lib, don't help

I've found some other code which words for me on windows: IIRC the crash was somewhere deep in the driver.

I included my headers the following order:

#include <windows.h>
#include <glew.h>
#include <wglew.h>

this is how i initialized glew and OpenGL


	m_hDC = GetDC(m_hWnd);
	m_hGlrc = wglCreateContext(m_hDC);
	wglMakeCurrent(m_hDC, m_hGlrc);
	glewInit();
        wglMakeCurrent(m_hDC, m_hGlrc);

Hope you can find there what you are missing

Create the context before calling glewInit. Your code:


GLManagedWrapper::GLManagedWrapper(INT_PTR hWnd)
	{

		if (!glewInit()) return;
		glewExperimental = GL_TRUE;


		m_Hwnd = (HWND)hWnd;
		if (!m_Hwnd) return;

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT));
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return;

		free(rect);

		m_Hdc = GetDC(m_Hwnd);

		if (m_Hdc)
		{
			GLSetPixelFormat(m_Hdc);
			ReSizeGLScene(m_Width, m_Height);
			InitGL();
		}
	}

This will never work because you're calling glewInit before you create your context. You need to do it the other way around - move your glewInit call to after your InitGL call and it will work. You also need to set glewExperimental before calling glewInit too.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement