Skip to content

Commit

Permalink
Added Stochastic Jittering
Browse files Browse the repository at this point in the history
  • Loading branch information
DavinderMaverick committed May 14, 2019
1 parent 4eb749b commit 14263af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
40 changes: 37 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <math.h>
#include <fstream>
#include <ctime>

#include <glad\glad.h>
#include <GLFW\glfw3.h>
Expand Down Expand Up @@ -60,6 +61,7 @@ bool fileExists(string fileName);
ostream& operator<<(ostream& out, const glm::vec3 &v);
istream& operator>>(istream& in, glm::vec3 &v);
void readGradientsFromFile();
GLuint GenerateNoiseTexture();

//settings
const unsigned int SCR_WIDTH = 800;
Expand Down Expand Up @@ -145,6 +147,8 @@ int main()
GLuint volumeTex = GenerateVolumeTexture();
GLuint gradientTexture = loadGradientTexture();

GLuint noiseTex = GenerateNoiseTexture();

cubeTransform.rotQuat = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);

vector<TransferFunctionControlPoint> colorKnots = {
Expand Down Expand Up @@ -332,7 +336,7 @@ int main()
//Render to display
//Render to the screen, to render to the screen. Use 0 as the second parameter of glBindFramebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.01f, 0.01f, 0.01f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
Expand Down Expand Up @@ -379,11 +383,15 @@ int main()
glBindTexture(GL_TEXTURE_1D, transferFuncTexture);
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "transferFuncTex"), 3);

glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, noiseTex);
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "noiseTex"), 4);

if (currentShaderIndex == 2)
{
glActiveTexture(GL_TEXTURE4);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_3D, gradientTexture);
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "gradientTex"), 4);
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "gradientTex"), 5);
}

//Draw Scene
Expand Down Expand Up @@ -798,6 +806,32 @@ GLuint setUpEmptyTexture()
return textureID;
}

//Stochastic Jittering Noise Texture
GLuint GenerateNoiseTexture()
{
GLuint noiseTex;
int size = 32;
unsigned char* buffer = new unsigned char[size*size];
srand((unsigned)time(NULL));
for (int i = 0; i < (size*size); i++)
buffer[i] = 255.*rand() / (float)RAND_MAX;
glGenTextures(1, &noiseTex);
glBindTexture(GL_TEXTURE_2D, noiseTex);
glTexImage2D(
GL_TEXTURE_2D, // target
0, // level
GL_RED, // internal
size, // width
size, // height
0, GL_RED, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
delete buffer;

return noiseTex;
}

GLuint computeTransferFunction(vector<TransferFunctionControlPoint> colorKnots, vector<TransferFunctionControlPoint> alphaKnots)
{
Expand Down
6 changes: 4 additions & 2 deletions raycast.frag
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uniform sampler2D back_face;
//Actual Volume data
uniform sampler3D volumeTex;
uniform sampler1D transferFuncTex;
uniform sampler2D noiseTex;

// A very simple color transfer function
/*
Expand Down Expand Up @@ -42,8 +43,9 @@ void main()
float ray_length = length(ray);
vec3 step_vector = step_length * ray / ray_length;

float random = fract(sin(gl_FragCoord.x * 12.9898 + gl_FragCoord.y * 78.233) * 43758.5453);

//32 X 32 Size of noise Texture
float random = texture(noiseTex, gl_FragCoord.xy / vec2(32, 32)).x;

ray_start += step_vector * random;

vec3 position = ray_start;
Expand Down
5 changes: 4 additions & 1 deletion raycastDiffuse.frag
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ uniform sampler2D back_face;
//Actual Volume data
uniform sampler3D volumeTex;
uniform sampler1D transferFuncTex;
uniform sampler2D noiseTex;

uniform sampler3D gradientTex;

// A very simple color transfer function
Expand Down Expand Up @@ -47,7 +49,8 @@ void main()
float ray_length = length(ray);
vec3 step_vector = step_length * ray / ray_length;

float random = fract(sin(gl_FragCoord.x * 12.9898 + gl_FragCoord.y * 78.233) * 43758.5453);
//32 X 32 Size of noise Texture
float random = texture(noiseTex, gl_FragCoord.xy / vec2(32, 32)).x;

ray_start += step_vector * random;

Expand Down

0 comments on commit 14263af

Please sign in to comment.