Для ветеранов opengl это, вероятно, легко, но я совершенно новичок в opengl и в настоящее время работаю над учебным пособием. настройку движения камеры).
Однако, хотя я запускаю исходный код от автора, я получаю ошибку сегментации.
Я думаю, он, вероятно, забыл настроить что-то дополнительное (просто угадать).
Но, поскольку я абсолютный новичок в opengl, я совершенно не понимаю, что это могло быть.
Проблема в том, что захват движения мыши дает ошибку сегментации.
Мышь и клавиатура полностью помещены в отдельный класс под названием Window.cpp
.
Я опубликую полный код, так как не знаю, с чего начать:
Window.h:
#pragma once
#include <stdio.h>
#include </usr/local/Cellar/glew/2.1.0_1/include/GL/glew.h>
#include </usr/local/Cellar/glfw/3.3.2/include/GLFW/glfw3.h>
class Window
{
public:
Window();
Window(GLint windowWidth, GLint windowHeight);
int initialize();
GLint getBufferWidth() { return bufferWidth; }
GLint getBufferHeight() { return bufferHeight; }
bool getShouldClose() { return glfwWindowShouldClose(mainWindow); }
bool* getKeys() { return keys; };
GLfloat getXChange();
GLfloat getYChange();
void swapBuffer() { glfwSwapBuffers(mainWindow); }
~Window();
private:
GLFWwindow *mainWindow;
GLint width, height;
GLint bufferWidth, bufferHeight;
bool keys[1024];
GLfloat lastX, lastY, xChange, yChange;
bool mouseFirstMoved;
void createCallBacks();
static void handleKeys(GLFWwindow* window, int key, int code, int action, int mode);
static void handleMouse(GLFWwindow* window, double xPos, double yPos);
};
И его реализация Window. cpp:
#include "Window.h"
Window::Window() {
width=800;
height=600;
xChange = 0.0f;
yChange = 0.0f;
mouseFirstMoved = false;
for(size_t i = 0; i < 1024; ++i)
keys[i] = 0;
}
Window::Window(GLint windowWidth, GLint windowHeight) {
width = windowWidth;
height = windowHeight;
}
int Window::initialize() {
//Initialize GLFW
if( !glfwInit() ) {
printf("GLFW initialization failed");
glfwTerminate();
return 1;
}
//Setup GLFW window properties
// OpenGL Version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//Core profile means no backwards compatibility for OpenGL minor to 3
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Make this code compatible for later version for OpenGL 3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
mainWindow = glfwCreateWindow(width, height, "1st OpenGL Project Window", NULL, NULL);
if(!mainWindow) {
printf("GLFWwindow creation failed");
glfwTerminate();
return 1;
}
//Get buffer size information
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
//set context for GLEW to use
glfwMakeContextCurrent(mainWindow);
//handle key and mouse input
createCallBacks();
glfwSetInputMode(mainWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//Allow modern extension features
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK) {
printf("GLEW initilization failed");
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
//Video 15: enable depth buffer to order the triangles of the pyramid
glEnable(GL_DEPTH_TEST);
//Set viewport size
glViewport(0, 0, bufferWidth, bufferHeight);
glfwSetWindowUserPointer(mainWindow, this);
return 0;
}
void Window::createCallBacks() {
glfwSetKeyCallback(mainWindow, handleKeys);
glfwSetCursorPosCallback(mainWindow, handleMouse);
}
GLfloat Window::getXChange() {
GLfloat theChange = xChange;
xChange = 0.0f;
return theChange;
}
GLfloat Window::getYChange() {
GLfloat theChange = yChange;
yChange = 0.0f;
return theChange;
}
void Window::handleKeys(GLFWwindow* window, int key, int code, int action, int mode) {
Window* theWindow = static_cast<Window*> (glfwGetWindowUserPointer(window));
if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if( 0<= key && key < 1024 ) {
if(action == GLFW_PRESS) {
theWindow->keys[key] = true;
} else if(action == GLFW_RELEASE) {
theWindow->keys[key] = false;
}
}
}
void Window::handleMouse(GLFWwindow* window, double xPos, double yPos) {
Window* theWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
if (theWindow->mouseFirstMoved)
{
theWindow->lastX = xPos;
theWindow->lastY = yPos;
theWindow->mouseFirstMoved = false;
}
theWindow->xChange = xPos - theWindow->lastX;
theWindow->yChange = theWindow->lastY - yPos;
theWindow->lastX = xPos;
theWindow->lastY = yPos;
}
Window::~Window() {
glfwDestroyWindow(mainWindow);
glfwTerminate();
}
Окно загружается и открывается.
Но как только я двигаю мышкой, программа вылетает и я получаю ошибку сегментации.
Это результат:
Segmentation fault: 11
Однако нажатие кнопки ESCAPE закрывает окно правильно, как и следовало ожидать от этого кода.
Я заблудился, поэтому очень ценю любой совет.