Skip to content

Commit

Permalink
Upgraded to latest GLFW on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdalling committed Oct 26, 2014
1 parent fcde3d5 commit 1fa97ec
Show file tree
Hide file tree
Showing 15 changed files with 2,919 additions and 731 deletions.
31 changes: 22 additions & 9 deletions osx/01_project_skeleton/source/main.mm
Expand Up @@ -19,7 +19,7 @@
// third-party libraries
#import <Foundation/Foundation.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

// standard C++ libraries
Expand All @@ -35,6 +35,7 @@
const glm::vec2 SCREEN_SIZE(800, 600);

// globals
GLFWwindow* gWindow = NULL;
tdogl::Program* gProgram = NULL;
GLuint gVAO = 0;
GLuint gVBO = 0;
Expand Down Expand Up @@ -108,23 +109,32 @@ static void Render() {
glUseProgram(0);

// swap the display buffers (displays what was just drawn)
glfwSwapBuffers();
glfwSwapBuffers(gWindow);
}

void OnError(int errorCode, const char* msg) {
throw std::runtime_error(msg);
}

// the program starts here
void AppMain() {
// initialise GLFW
glfwSetErrorCallback(OnError);
if(!glfwInit())
throw std::runtime_error("glfwInit failed");

// open a window with GLFW
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
if(!glfwOpenWindow(SCREEN_SIZE.x, SCREEN_SIZE.y, 8, 8, 8, 8, 0, 0, GLFW_WINDOW))
throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
gWindow = glfwCreateWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, "OpenGL Tutorial", NULL, NULL);
if(!gWindow)
throw std::runtime_error("glfwCreateWindow failed. Can your hardware handle OpenGL 3.2?");

// GLFW settings
glfwMakeContextCurrent(gWindow);

// initialise GLEW
glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
Expand All @@ -148,7 +158,10 @@ void AppMain() {
LoadTriangle();

// run while the window is open
while(glfwGetWindowParam(GLFW_OPENED)){
while(!glfwWindowShouldClose(gWindow)){
// process pending events
glfwPollEvents();

// draw one frame
Render();
}
Expand Down
32 changes: 23 additions & 9 deletions osx/02_textures/source/main.mm
Expand Up @@ -19,7 +19,7 @@
// third-party libraries
#import <Foundation/Foundation.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

// standard C++ libraries
Expand All @@ -36,6 +36,7 @@
const glm::vec2 SCREEN_SIZE(800, 600);

// globals
GLFWwindow* gWindow = NULL;
tdogl::Texture* gTexture = NULL;
tdogl::Program* gProgram = NULL;
GLuint gVAO = 0;
Expand Down Expand Up @@ -125,22 +126,32 @@ static void Render() {
gProgram->stopUsing();

// swap the display buffers (displays what was just drawn)
glfwSwapBuffers();
glfwSwapBuffers(gWindow);
}

void OnError(int errorCode, const char* msg) {
throw std::runtime_error(msg);
}

// the program starts here
void AppMain() {
// initialise GLFW
glfwSetErrorCallback(OnError);
if(!glfwInit())
throw std::runtime_error("glfwInit failed");

// open a window with GLFW
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
if(!glfwOpenWindow(SCREEN_SIZE.x, SCREEN_SIZE.y, 8, 8, 8, 8, 0, 0, GLFW_WINDOW))
throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
gWindow = glfwCreateWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, "OpenGL Tutorial", NULL, NULL);
if(!gWindow)
throw std::runtime_error("glfwCreateWindow failed. Can your hardware handle OpenGL 3.2?");

// GLFW settings
glfwMakeContextCurrent(gWindow);

// initialise GLEW
glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
Expand Down Expand Up @@ -171,7 +182,10 @@ void AppMain() {
LoadTriangle();

// run while the window is open
while(glfwGetWindowParam(GLFW_OPENED)){
while(!glfwWindowShouldClose(gWindow)){
// process pending events
glfwPollEvents();

// draw one frame
Render();
}
Expand Down
37 changes: 25 additions & 12 deletions osx/03_matrices/source/main.mm
Expand Up @@ -19,7 +19,7 @@
// third-party libraries
#import <Foundation/Foundation.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

Expand All @@ -37,6 +37,7 @@
const glm::vec2 SCREEN_SIZE(800, 600);

// globals
GLFWwindow* gWindow = NULL;
tdogl::Texture* gTexture = NULL;
tdogl::Program* gProgram = NULL;
GLuint gVAO = 0;
Expand All @@ -63,7 +64,6 @@ static void LoadShaders() {

//set the "projection" uniform in the vertex shader, because it's not going to change
glm::mat4 projection = glm::perspective<float>(50.0, SCREEN_SIZE.x/SCREEN_SIZE.y, 0.1, 10.0);
//glm::mat4 projection = glm::ortho<float>(-2, 2, -2, 2, 0.1, 10);
gProgram->setUniform("projection", projection);

//set the "camera" uniform in the vertex shader, because it's also not going to change
Expand Down Expand Up @@ -187,7 +187,7 @@ static void Render() {
gProgram->stopUsing();

// swap the display buffers (displays what was just drawn)
glfwSwapBuffers();
glfwSwapBuffers(gWindow);
}


Expand All @@ -198,20 +198,30 @@ void Update(float secondsElapsed) {
while(gDegreesRotated > 360.0f) gDegreesRotated -= 360.0f;
}

void OnError(int errorCode, const char* msg) {
throw std::runtime_error(msg);
}

// the program starts here
void AppMain() {
// initialise GLFW
glfwSetErrorCallback(OnError);
if(!glfwInit())
throw std::runtime_error("glfwInit failed");

// open a window with GLFW
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
if(!glfwOpenWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, 8, 8, 8, 8, 16, 0, GLFW_WINDOW))
throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
gWindow = glfwCreateWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, "OpenGL Tutorial", NULL, NULL);
if(!gWindow)
throw std::runtime_error("glfwCreateWindow failed. Can your hardware handle OpenGL 3.2?");

// GLFW settings
glfwMakeContextCurrent(gWindow);

// initialise GLEW
glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
if(glewInit() != GLEW_OK)
Expand Down Expand Up @@ -247,7 +257,10 @@ void AppMain() {

// run while the window is open
double lastTime = glfwGetTime();
while(glfwGetWindowParam(GLFW_OPENED)){
while(!glfwWindowShouldClose(gWindow)){
// process pending events
glfwPollEvents();

// update the scene based on the time elapsed since last update
double thisTime = glfwGetTime();
Update(thisTime - lastTime);
Expand All @@ -259,7 +272,7 @@ void AppMain() {
// check for errors
GLenum error = glGetError();
if(error != GL_NO_ERROR)
std::cerr << "OpenGL Error " << error << ": " << (const char*)gluErrorString(error) << std::endl;
std::cerr << "OpenGL Error " << error << std::endl;
}

// clean up and exit
Expand Down
70 changes: 44 additions & 26 deletions osx/04_camera/source/main.mm
Expand Up @@ -19,7 +19,7 @@
// third-party libraries
#import <Foundation/Foundation.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

Expand All @@ -38,6 +38,8 @@
const glm::vec2 SCREEN_SIZE(800, 600);

// globals
GLFWwindow* gWindow = NULL;
double gScrollY = 0.0;
tdogl::Texture* gTexture = NULL;
tdogl::Program* gProgram = NULL;
tdogl::Camera gCamera;
Expand Down Expand Up @@ -179,7 +181,7 @@ static void Render() {
gProgram->stopUsing();

// swap the display buffers (displays what was just drawn)
glfwSwapBuffers();
glfwSwapBuffers(gWindow);
}


Expand All @@ -192,56 +194,69 @@ void Update(float secondsElapsed) {

//move position of camera based on WASD keys, and XZ keys for up and down
const float moveSpeed = 2.0; //units per second
if(glfwGetKey('S')){
if(glfwGetKey(gWindow, 'S')){
gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.forward());
} else if(glfwGetKey('W')){
} else if(glfwGetKey(gWindow, 'W')){
gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.forward());
}
if(glfwGetKey('A')){
if(glfwGetKey(gWindow, 'A')){
gCamera.offsetPosition(secondsElapsed * moveSpeed * -gCamera.right());
} else if(glfwGetKey('D')){
} else if(glfwGetKey(gWindow, 'D')){
gCamera.offsetPosition(secondsElapsed * moveSpeed * gCamera.right());
}
if(glfwGetKey('Z')){
if(glfwGetKey(gWindow, 'Z')){
gCamera.offsetPosition(secondsElapsed * moveSpeed * -glm::vec3(0,1,0));
} else if(glfwGetKey('X')){
} else if(glfwGetKey(gWindow, 'X')){
gCamera.offsetPosition(secondsElapsed * moveSpeed * glm::vec3(0,1,0));
}

//rotate camera based on mouse movement
const float mouseSensitivity = 0.1;
int mouseX, mouseY;
glfwGetMousePos(&mouseX, &mouseY);
double mouseX, mouseY;
glfwGetCursorPos(gWindow, &mouseX, &mouseY);
gCamera.offsetOrientation(mouseSensitivity * mouseY, mouseSensitivity * mouseX);
glfwSetMousePos(0, 0); //reset the mouse, so it doesn't go out of the window
glfwSetCursorPos(gWindow, 0, 0); //reset the mouse, so it doesn't go out of the window

//increase or decrease field of view based on mouse wheel
const float zoomSensitivity = -0.2;
float fieldOfView = gCamera.fieldOfView() + zoomSensitivity * (float)glfwGetMouseWheel();
float fieldOfView = gCamera.fieldOfView() + zoomSensitivity * (float)gScrollY;
if(fieldOfView < 5.0f) fieldOfView = 5.0f;
if(fieldOfView > 130.0f) fieldOfView = 130.0f;
gCamera.setFieldOfView(fieldOfView);
glfwSetMouseWheel(0);
gScrollY = 0;
}

// records how far the y axis has been scrolled
void OnScroll(GLFWwindow* window, double deltaX, double deltaY) {
gScrollY += deltaY;
}

void OnError(int errorCode, const char* msg) {
throw std::runtime_error(msg);
}

// the program starts here
void AppMain() {
// initialise GLFW
glfwSetErrorCallback(OnError);
if(!glfwInit())
throw std::runtime_error("glfwInit failed");

// open a window with GLFW
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
if(!glfwOpenWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, 8, 8, 8, 8, 16, 0, GLFW_WINDOW))
throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
gWindow = glfwCreateWindow((int)SCREEN_SIZE.x, (int)SCREEN_SIZE.y, "OpenGL Tutorial", NULL, NULL);
if(!gWindow)
throw std::runtime_error("glfwCreateWindow failed. Can your hardware handle OpenGL 3.2?");

// GLFW settings
glfwDisable(GLFW_MOUSE_CURSOR);
glfwSetMousePos(0, 0);
glfwSetMouseWheel(0);
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPos(gWindow, 0, 0);
glfwSetScrollCallback(gWindow, OnScroll);
glfwMakeContextCurrent(gWindow);

// initialise GLEW
glewExperimental = GL_TRUE; //stops glew crashing on OSX :-/
Expand Down Expand Up @@ -282,7 +297,10 @@ void AppMain() {

// run while the window is open
double lastTime = glfwGetTime();
while(glfwGetWindowParam(GLFW_OPENED)){
while(!glfwWindowShouldClose(gWindow)){
// process pending events
glfwPollEvents();

// update the scene based on the time elapsed since last update
double thisTime = glfwGetTime();
Update(thisTime - lastTime);
Expand All @@ -294,11 +312,11 @@ void AppMain() {
// check for errors
GLenum error = glGetError();
if(error != GL_NO_ERROR)
std::cerr << "OpenGL Error " << error << ": " << (const char*)gluErrorString(error) << std::endl;
std::cerr << "OpenGL Error " << error << std::endl;

//exit program if escape key is pressed
if(glfwGetKey(GLFW_KEY_ESC))
glfwCloseWindow();
if(glfwGetKey(gWindow, GLFW_KEY_ESCAPE))
glfwSetWindowShouldClose(gWindow, GL_TRUE);
}

// clean up and exit
Expand Down

0 comments on commit 1fa97ec

Please sign in to comment.