I recently started playing around with OpenGL on C++ and after getting some of the basics, a simple GLSL implementation. I tried to add very slight random noise to some simple shaded polygons, where the colors are described at the vertices (I don't know the proper name for this) in a pyramid.
I made it use FragCoord to get it coords to calculate the noise value, but this uses screen pixels and so the noise would stay the same no matter the prospective you're looking at the model from. I followed the description of a better method described in the top answer here, but this doesn't seem to be working. I can't move the shape around and it's stuck at z=0. I can't change any camera perspectives. I think this may be an error in my GLSL as I'm totally new to it and it's quite alien to me.
Here's an image of my problem. The first is what it's meant to look like (without the noise) the second what it looks like:
Here is my code:
frag.glsl
varying vec4 verpos;
float rand2d(vec2 x){
uint n = floatBitsToUint(x.y * 214013.0 + x.x * 2531011.0);
n = n * (n * n * 15731u + 789221u);
n = (n >> 9u) | 0x3F800000u;
return (2.0 - uintBitsToFloat(n));
}
float rand3d(vec3 x){
uint n = floatBitsToUint(x.y * 214013.0 + x.x * 2531011.0 + x.z * 27644437.0);
n = n * (n * n * 15731u + 789221u);
n = (n >> 9u) | 0x3F800000u;
return (2.0 - uintBitsToFloat(n));
}
void main(void)
{
vec4 outColor = gl_Color;
outColor *= ((0.2 * rand3d(vec3(verpos.x, verpos.y, verpos.z)) + 0.9);
outColor.w = 1.0;
gl_FragColor = outColor;
}
vert.glsl
uniform mat4 view_matrix;
uniform mat4 model_matrix;
varying vec4 verpos;
void main(){
verpos = model_matrix * gl_Vertex;
gl_FontColor = gl_Color;
gl_Position = gl_ProjectionMatrix * view_matrix * model_matrix * gl_Vertex;
}
and my render method where I separate my matrices:
void App::render(){
start = SDL_GetTicks();
//Clear the matrices and pixel/depth buffer(s)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Create model matrix and pass to shader
glTranslatef(x, y, -6.0f);//Modify the world matrix
GLfloat model_matrix_array[16];
glGetFloatv(GL_MODELVIEW_MATRIX, model_matrix_array);
glUniformMatrix4fv(model_matrix, 1, GL_FALSE, model_matrix_array);
glLoadIdentity();
//Create view matrix and pass to shader
glRotatef(roty, 1.0f, 0.0f, 0.0f);//Modify the view matrix
glRotatef(rotx, 0.0f, 1.0f, 0.0f);//Modify the view matrix
GLfloat view_matrix_array[16];
glGetFloatv(GL_MODELVIEW_MATRIX, view_matrix_array);
glUniformMatrix4fv(view_matrix, 1, GL_FALSE, view_matrix_array);
glLoadIdentity();
glBegin(GL_TRIANGLE_FAN);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glColor4f(1.0f, 1.0f, 0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glColor4f(0.0f, 1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glBegin(GL_QUADS);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glColor4f(1.0f, 1.0f, 0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glColor4f(0.0f, 1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glEnd();
SDL_GL_SwapBuffers();
frames++;
current = SDL_GetTicks();
}
I'm sorry that this is such a big post but I really hope it doesn't put people off and that someone can help me. I would really appreciate it.
Thanks.