如果我将GL函数移动到另一个类,我的GLEW MX会崩溃...说Point类包含我所有的点和所需的VBO函数。
它基本上在第一次GL调用时崩溃,对我来说意味着当GL函数在点类中被调用时,GLEWContext点在某种程度上为空。
我对GLEW MX
缺少什么基础如果所有东西都在main中,那么它可以正常工作,我可以渲染出一个简单的三角形。
的main.cpp
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Global_variables.h"
#include <thread>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
std::thread::id thread = std::this_thread::get_id();
glfwMakeContextCurrent(window);
myGLEWCONTEXT = new GLEWContext();
GLEWContext* test = glewGetContext();
Points mPoints = Points();
mPoints.AddPoint(Point(glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec2(0, 0), glm::vec3(0, 1, 0), glm::vec4(0.1, 0.1, 0.1, 1)));
mPoints.AddPoint(Point(glm::vec3(1.0f, -1.0f, 0.0f), glm::vec2(0, 0), glm::vec3(0, 1, 0), glm::vec4(0.1, 0.1, 0.1, 1)));
mPoints.setGLMode(GL_TRIANGLES);
mPoints.create();
}
Global_variables.h
static GLEWContext* myGLEWCONTEXT;
static GLEWContext* glewGetContext()
{
std::thread::id thread = std::this_thread::get_id();
return myGLEWCONTEXT;
}
Points.h
class Points
{
public:
//Pack Vertex
struct PackedVertex{
glm::vec3 position;
glm::vec2 uv;
glm::vec3 normal;
glm::vec4 color;
bool operator<(const PackedVertex that) const{
return memcmp((void*)this, (void*)&that, sizeof(PackedVertex))>0;
};
};
Points();
Points(int number, glm::vec3 upperleftbound, glm::vec3 lowerrightbound);
Points(std::vector<Point> & passedinpoints);
~Points(void);
void Print();
void Render();
bool create();
.....
更新:-----------------尝试了Singlton
#include <GL/glew.h>
#pragma once
class glewSinglton
{
public:
glewSinglton (const glewSinglton&) = delete;
glewSinglton& operator=(const glewSinglton&) = delete;
GLEWContext* myGLEWCONTEXT;
GLEWContext* get_value()
{
return myGLEWCONTEXT;
}
void set_value(GLEWContext* v)
{
myGLEWCONTEXT = v;
}
static glewSinglton *instance()
{
if (!s_instance){
s_instance = new glewSinglton;
GLEWContext* myGLEWCONTEXT = new GLEWContext();
}
return s_instance;
}
GLEWContext* glewGetContext()
{
return myGLEWCONTEXT;
}
static glewSinglton *s_instance;
private:
glewSinglton();
~glewSinglton();
};
#endif
但是当我提出
时,这只会给我带来大量的错误#include "glewSinglton.h"
glewSinglton *mInstance = glewSinglton::instance();
答案 0 :(得分:1)
static GLEWContext* glewGetContext()
{
static GLEWContext* myGLEWCONTEXT = NULL;
std::thread::id thread = std::this_thread::get_id();
// Initialize on first use
if (! myGLEWCONTEXT)
myGLEWCONTEXT = new GLEWContext();
return myGLEWCONTEXT;
}