Skip to content

Commit 14263af

Browse files
Added Stochastic Jittering
1 parent 4eb749b commit 14263af

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

main.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <math.h>
66
#include <fstream>
7+
#include <ctime>
78

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

6466
//settings
6567
const unsigned int SCR_WIDTH = 800;
@@ -145,6 +147,8 @@ int main()
145147
GLuint volumeTex = GenerateVolumeTexture();
146148
GLuint gradientTexture = loadGradientTexture();
147149

150+
GLuint noiseTex = GenerateNoiseTexture();
151+
148152
cubeTransform.rotQuat = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);
149153

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

386+
glActiveTexture(GL_TEXTURE4);
387+
glBindTexture(GL_TEXTURE_2D, noiseTex);
388+
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "noiseTex"), 4);
389+
382390
if (currentShaderIndex == 2)
383391
{
384-
glActiveTexture(GL_TEXTURE4);
392+
glActiveTexture(GL_TEXTURE5);
385393
glBindTexture(GL_TEXTURE_3D, gradientTexture);
386-
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "gradientTex"), 4);
394+
glUniform1i(glGetUniformLocation(currentRaycastShader->ID, "gradientTex"), 5);
387395
}
388396

389397
//Draw Scene
@@ -798,6 +806,32 @@ GLuint setUpEmptyTexture()
798806
return textureID;
799807
}
800808

809+
//Stochastic Jittering Noise Texture
810+
GLuint GenerateNoiseTexture()
811+
{
812+
GLuint noiseTex;
813+
int size = 32;
814+
unsigned char* buffer = new unsigned char[size*size];
815+
srand((unsigned)time(NULL));
816+
for (int i = 0; i < (size*size); i++)
817+
buffer[i] = 255.*rand() / (float)RAND_MAX;
818+
glGenTextures(1, &noiseTex);
819+
glBindTexture(GL_TEXTURE_2D, noiseTex);
820+
glTexImage2D(
821+
GL_TEXTURE_2D, // target
822+
0, // level
823+
GL_RED, // internal
824+
size, // width
825+
size, // height
826+
0, GL_RED, GL_UNSIGNED_BYTE, buffer);
827+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
828+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
829+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
830+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
831+
delete buffer;
832+
833+
return noiseTex;
834+
}
801835

802836
GLuint computeTransferFunction(vector<TransferFunctionControlPoint> colorKnots, vector<TransferFunctionControlPoint> alphaKnots)
803837
{

raycast.frag

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ uniform sampler2D back_face;
1313
//Actual Volume data
1414
uniform sampler3D volumeTex;
1515
uniform sampler1D transferFuncTex;
16+
uniform sampler2D noiseTex;
1617

1718
// A very simple color transfer function
1819
/*
@@ -42,8 +43,9 @@ void main()
4243
float ray_length = length(ray);
4344
vec3 step_vector = step_length * ray / ray_length;
4445

45-
float random = fract(sin(gl_FragCoord.x * 12.9898 + gl_FragCoord.y * 78.233) * 43758.5453);
46-
46+
//32 X 32 Size of noise Texture
47+
float random = texture(noiseTex, gl_FragCoord.xy / vec2(32, 32)).x;
48+
4749
ray_start += step_vector * random;
4850

4951
vec3 position = ray_start;

raycastDiffuse.frag

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ uniform sampler2D back_face;
1616
//Actual Volume data
1717
uniform sampler3D volumeTex;
1818
uniform sampler1D transferFuncTex;
19+
uniform sampler2D noiseTex;
20+
1921
uniform sampler3D gradientTex;
2022

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

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

5255
ray_start += step_vector * random;
5356

0 commit comments

Comments
 (0)