Skip to content

Commit

Permalink
Merge pull request #57 from Zardoz89/fix_osx
Browse files Browse the repository at this point in the history
Close issue #53
  • Loading branch information
Zardoz89 committed Feb 18, 2015
2 parents 594ffc6 + 4aeda06 commit a5e7c7b
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 613 deletions.
22 changes: 14 additions & 8 deletions assets/shaders/basic_vs.vert
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// Basic Vertex Shader
#version 140
// Basic Vertex Shader Template with Model, View, Project matrixes
#version 140

in vec3 in_Position;
in vec3 in_Color;
in vec2 in_UV;

uniform mat4 in_Model;
uniform mat4 in_View;
uniform mat4 in_Proj;

out vec3 ex_Color;

void main(void) {
gl_Position.xyz = in_Position.xyz;
gl_Position.w = 1.0;
out vec2 ex_UV;

ex_Color = vec3(1.0, 0.0, 0.0);
void main(void) {
gl_Position = in_Proj * (in_View * (in_Model * vec4(in_Position, 1.0)));
ex_Color = vec3(in_Color);
ex_UV = in_UV;
}

26 changes: 13 additions & 13 deletions assets/shaders/retro_texture.frag
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ out vec4 out_Color;
// width of a pixel in texture units,
// should be set to 1 / width, 1 / height.
uniform vec2 pixelSize = vec2(1.0/(320.0), 1.0/(240.0) );

// how sharp the bilinear filter is, 0 - 1
const float sharpness = 0.75;

// How many are misalign the color beams
const float misalign = 0.4;

// how much a scanline should darken its line, 0-1
const float scanIntensity = 0.1;

Expand All @@ -36,7 +36,7 @@ const float distortion = 0.08;

// Flicker intesity
//const float flicker = 0.025;

// Time depedent FX
uniform float time = 0.0;

Expand All @@ -51,21 +51,21 @@ uniform float brightness = 0.0;
vec2 barrelDistortion(vec2 coord) {
vec2 cc = coord - 0.5;
float dist = dot(cc, cc) * distortion;
return coord + cc * (1.0 - dist) * dist;
return coord + cc * (1.0 - dist) * dist;
}

void main(void) {
// Apply curvature fx
vec2 uv = barrelDistortion(ex_UV);
if ( (uv.x <0 || uv.x > 1.0) || (uv.y <0 || uv.y > 1.0)) {
if ( (uv.x <0.0 || uv.x > 1.0) || (uv.y <0.0 || uv.y > 1.0)) {
// Ignore fragments that are outside of the screen
discard;
return;
}

// Precalculate misalign in function of horizontal pos
float mis = misalign * pixelSize.x * (misalign + (1.0 - misalign)*2 * abs(uv.x - 0.5));

// Precalculate bilinear filter things
float xInc = pixelSize.x * (1.0 - sharpness) / 2.0;
float yInc = pixelSize.y * (1.0 - sharpness) / 2.0;
Expand All @@ -75,17 +75,17 @@ void main(void) {
uvs[1] = uv + vec2(xInc, -yInc);
uvs[2] = uv + vec2(-xInc, yInc);
uvs[3] = uv + vec2(xInc, yInc);

for (int i=0; i < 4; i++) {
middle[i] = texture(texture0, uvs[i] ).rgb;

// Generate color border (misaligment)
vec3 col;
col.r = texture(texture0,vec2(uvs[i].x + mis ,uvs[i].y)).x;
col.g = texture(texture0,vec2(uvs[i].x ,uvs[i].y)).y;
col.b = texture(texture0,vec2(uvs[i].x - mis ,uvs[i].y)).z;
middle[i] = middle[i]*0.2 + col*0.8;

// scanlines
if (scanIntensity > 0.0 && mod(uvs[i].y, pixelSize.y ) > (pixelSize.y/2)) {
middle[i].r = max(middle[i].r - scanIntensity, 0);
Expand All @@ -100,14 +100,14 @@ void main(void) {

// noise
result = result + noise * fract(sin(dot(uv.xy , vec2(12.9898 + time, 78.233 + tan(time)))) * 43758.5453);

// contrast curve
//result.xyz = clamp(0.5*result.xyz + 0.5*result.xyz * 1.2*result.xyz, 0.0 , 1.0);

//flickering (semi-randomized)
// result *= 1.0 - flicker * fract(sin(dot(vec2(1.0) , vec2(12.9898 + time, 78.233 + tan(time)))) * 43758.5453);

out_Color.xyz = result * (1.0 + brightness);
out_Color.w = 1.0;

}
4 changes: 2 additions & 2 deletions src/devices/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,11 @@ void Timer::WriteDW (DWord addr, DWord val) {
break;

case 0x11E008:
tmr1 = tmr1;
tmr1 = val;
break;

case 0x11E00C:
re1 = re1;
re1 = val;
break;

case 0x11E010:
Expand Down
42 changes: 41 additions & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
SET(BUILD_TOOLS_SCREEN TRUE CACHE BOOL "Build Trillek VCOMPUTER tools with virtual screen")
SET(BUILD_TOOLS_AUDIO TRUE CACHE BOOL "Build Trillek VCOMPUTER tools with audio")

# Give these some dummy values and if the platform is LINUX or OSX they will be set accordingly.
SET(X11_LIBRARIES "")
SET(OSX_LIBRARIES "")

# FIND GLFW3 AND OPENGL libs
FIND_PACKAGE(GLFW3)
IF (NOT GLFW3_FOUND)
Expand Down Expand Up @@ -89,8 +93,40 @@ SET(MEDIA_LINK_LIBS

# If we have OpenGL / GLFW3 / GLM / GLEW libs
IF (GLFW3_ENABLE EQUAL 1)
MESSAGE(STATUS "vc executable have Virtual Screen enabled")
IF (NOT APPLE) # X11 and GLEW are not needed on OSX.
FIND_PACKAGE(X11)
SET(USE_STATIC_GLEW CACHE BOOL "Build against GLEW static (default no)")
FIND_PACKAGE(GLEW REQUIRED) # We find GLEW here as OSX doesn't need it.
ENDIF (NOT APPLE)

IF (APPLE) # Mac OSX
SET(GLEW_LIBRARY "") # Set a dummy value for GLEW.

SET(CMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS macosx)

# Need the 10.7 SDK or later.
EXECUTE_PROCESS(COMMAND xcodebuild -sdk macosx -version SDKVersion OUTPUT_VARIABLE OSX_SDK_VERSION)
IF (NOT (OSX_SDK_VERSION VERSION_GREATER 10.7 OR OSX_SDK_VERSION VERSION_EQUAL 10.7))
MESSAGE(FATAL_ERROR "The installed version of Xcode does not support the 10.7 SDK or later. Please upgrade Xcode and try again.")
ENDIF (NOT (OSX_SDK_VERSION VERSION_GREATER 10.7 OR OSX_SDK_VERSION VERSION_EQUAL 10.7))

# Configure the project to use the correct SDK.
IF (XCODE_VERSION)
SET(CMAKE_OSX_SYSROOT macosx)
ELSE (XCODE_VERSION)
# Non-Xcode generators need the full path.
EXECUTE_PROCESS(COMMAND xcodebuild -sdk macosx -version Path | head -n 1 OUTPUT_VARIABLE CMAKE_OSX_SYSROOT)
STRING(REGEX REPLACE "(\r?\n)+$" "" CMAKE_OSX_SYSROOT "${CMAKE_OSX_SYSROOT}")
ENDIF (XCODE_VERSION)

# Can deploy back to 10.7, the first OS X to support the GL Core.
SET(CMAKE_OSX_DEPLOYMENT_TARGET 10.7)

# Need Core Foundation and libobjc.
SET(OSX_LIBRARIES "-framework CoreFoundation /usr/lib/libobjc.dylib")
ENDIF (APPLE)

MESSAGE(STATUS "vc executable have Virtual Screen enabled")
# Some auxiliar libs
FILE(GLOB TOOL_OTHER_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
Expand All @@ -106,6 +142,8 @@ IF (GLFW3_ENABLE EQUAL 1)
)

SET(MEDIA_LINK_LIBS
${X11_LIBRARIES}
${OSX_LIBRARIES}
${MEDIA_LINK_LIBS}
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
Expand All @@ -116,6 +154,8 @@ IF (GLFW3_ENABLE EQUAL 1)
# TDA VIEWER Can only be build with OpenGL stuff
ADD_EXECUTABLE( tda_view
./tda_view.cpp
./src/gl_engine.cpp
./src/glerror.cpp
)
SET(TARGETS ${TARGETS} "tda_view")

Expand Down
8 changes: 5 additions & 3 deletions tools/config_main.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#if GLFW3_ENABLE == 0
#undef GLFW3_ENABLE
#else

#ifdef __APPLE__
#include <OpenGL/gl3.h>
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
// Needed so we can disable retina support for our window.
#define GLFW_EXPOSE_NATIVE_COCOA 1
#define GLFW_EXPOSE_NATIVE_NSGL 1
Expand All @@ -22,16 +24,16 @@
typedef void* SEL;
extern "C" id objc_msgSend(id self, SEL op, ...);
extern "C" SEL sel_getUid(const char *str);

#else
#include <GL/glew.h>

#ifndef __unix
#include <GL/wglew.h>
#endif
#include <GLFW/glfw3.h>
#endif

#include <GLFW/glfw3.h>

#ifndef GLM_FORCE_RADIANS
#define GLM_FORCE_RADIANS // Removes anoying messages of depracated angles on degress
#endif
Expand Down
28 changes: 20 additions & 8 deletions tools/include/gl_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ class GlEngine {

yaw = 0;
pith = 0;
zoom = 6.0;
zoom = 4.7;

frame_count = 0;
t_acu = 0;

vertShaderFile = "basic_vs.vert";
fragShaderFile = "retro_texture.frag";
this->pbo = 0;
}

~GlEngine () {
Expand All @@ -44,6 +48,13 @@ class GlEngine {
}
}

void setVertexShaderFile (const std::string& file) {
this->vertShaderFile = file;
}
void setFragmentShaderFile (const std::string& file) {
this->fragShaderFile = file;
}

//! Init OpenGL
int initGL(OS::OS& os);

Expand All @@ -64,7 +75,8 @@ class GlEngine {
//bool capture_keyboard = false;

GLuint screenTex; // ID of screen Texture
GLuint tex_pbo; // ID of the screen texture PBO
GLuint tex_pbo[2];// IDs of the screen texture PBO
size_t pbo;

// Handler of shader program
GLuint shaderProgram;
Expand All @@ -87,19 +99,16 @@ class GlEngine {
static const unsigned int sh_in_Color;
static const unsigned int sh_in_UV;

static const GLfloat N_VERTICES;
static const GLsizei N_VERTICES;

GLuint vertexbuffer;
GLuint vao, vbo[3]; // vbo = {vdata, color, uv}
static const float vdata[];

GLuint colorbuffer;
static const float color_data[];

GLuint uvbuffer;
static const float uv_data[];

glm::mat4 proj, view, model; //! MVP Matrixes

// Camera position
float yaw;
float pith;
float zoom;
Expand All @@ -108,6 +117,9 @@ class GlEngine {
double t_acu; //! Acumulated time

std::function<void(void*)> painter; //! Function that paints screen texture

std::string vertShaderFile;
std::string fragShaderFile;
};

#endif
Expand Down
33 changes: 33 additions & 0 deletions tools/include/glerror.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Trillek Virtual Computer - glerror.hpp
* Function/macros to debug OpenGL code
*/

#ifndef __TR_GL_ERROR_HPP_
#define __TR_GL_ERROR_HPP_ 1

namespace trillek {
/**
* Check if there is a OpenGL error and display it
* @param file Source file
* @param line Source line
*/
void tr_check_gl_error(const char *file, int line);
} // End of namespace trillek

#ifndef NDEBUG
/**
* Usage
* [... some opengl calls]
* glCheckError();
*/
#define check_gl_error() trillek::tr_check_gl_error(__FILE__,__LINE__)

#else

#define check_gl_error()

#endif

#endif // __TR_GL_ERROR_HPP_

Loading

0 comments on commit a5e7c7b

Please sign in to comment.