Jump to content

  • Log In with Google      Sign In   
  • Create Account


Slushy

Member Since 05 Apr 2012
Offline Last Active Nov 24 2013 01:19 PM

Topics I've Started

Diffuse Lighting, GLSL.

26 October 2013 - 07:33 AM

Hello, im back again with another question. So I've been working on diffuse lighting for a couple of days now and I can't seem to get it working properly. I get the feeling that there is something messed up with my direction/position Vector of the light. But I can't seem to figure out what. Either way, here are my shaders:

VertexShader.glsl

#version 330

uniform mat4 perspectiveMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

layout(location = 0) in vec3 in_Position;
layout(location = 1) in vec2 in_TextureCoord;
layout(location = 2) in vec3 in_Normal;

out vec2 frag_TextureCoord;
out vec3 normal;

void main(void){
	gl_Position = perspectiveMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
	
	normal = mat3(modelMatrix) * in_Normal;
	normal = normalize(normal);
    
	frag_TextureCoord = in_TextureCoord;
}

FragmentShader.glsl

#version 330

uniform sampler2D texture_diffuse;

uniform vec3 lightPosition;
uniform vec3 lightColor;
uniform float lightPower;

in vec2 frag_TextureCoord;
in vec3 normal;

out vec3 out_Color;

void main(void){
	float lightIntensity;
	vec3 lightDir;
	
	lightDir = -lightPosition;
	lightIntensity = clamp(dot(normal, lightDir), 0.0f, 1.0f);
	
	out_Color = clamp((lightColor * lightIntensity), 0.0f, 1.0f);
	out_Color = out_Color * texture(texture_diffuse, frag_TextureCoord).rgb;
}

My position/direction vector that I send to my fragment shader in the uniform is: 0, -1, 0

and what I get is this:

GcNZPLM.png


XMVECTOR and XMMATRIX in header?

03 September 2013 - 10:21 AM

Hello! So I've been struggling with getting XMVECTOR and XMMATRIX working when defined in my header file. If I do:

XMMATRIX World;
XMVECTOR camPosition;

in the header file for example, and then do:

camPosition = XMVectorSet( 0.0f, 0.0f, -0.5f, 0.0f );

in my .cpp file, for example. It gives me an "access violation" error. I read that this is because of some kind of alignment thingy? Is there no way to get the XMVECTOR or XMMATRIX defined in the header file and then use it in the .cpp file?

 

If not, what should I do instead? Because defining them in the .cpp file works, but that looks so bad? Any other solutions, what do you guys do?


OpenGL Camera Up Problem.

12 April 2013 - 11:18 AM

Hello! So I recently went from OpenGL 2.X (using now deprecated functions like, glTranslate etc.) to using OpenGL 3.2. This also meant implementing matrices manually for all of my transformation. While this is extremely fun and very enjoyable, it's hard.

 

My problem is that when I move my Camera (Viewmatrix) in the +Y direction, my OBJ that I loaded into my game is moving upwards (hence im moving the camera "downwards" relative to the object). This also affects moving in +/- X. + Goes left, - Goes right.

 

Why is it that I have this problem? I've checked my Projectionmatrix and it seems to be correct, according to http://ogldev.atspace.co.uk/www/tutorial12/12_11.png

 

Projectionmatrix

perspectiveMatrix.setIdentity();

float aspectRatio = WIDTH / HEIGHT;
float zRange = zNear - zFar;

perspectiveMatrix.m00 = (float) (1.0f / (aspectRatio * Math.tan(degreesToRadians(fieldOfView / 2.0f))));
perspectiveMatrix.m11 =  (float) (1.0f / (Math.tan(degreesToRadians(fieldOfView / 2.0f))));
perspectiveMatrix.m22 = (-zNear - zFar) / zRange;
perspectiveMatrix.m23 = 1;
perspectiveMatrix.m32 = (2 * zFar * zNear) / zRange;

 

 

Modelmatrix is Identity.

 

Viewmatrix is changed when I move:

if(moveUp)
    Matrices.viewMatrix.translate(new Vector3f(0, 1.0f * moveSpeed, 0));

if(moveLeft)
    Matrices.viewMatrix.translate(new Vector3f(-1.0f * moveSpeed, 0, 0));

if(moveDown)
    Matrices.viewMatrix.translate(new Vector3f(0, -1.0f * moveSpeed, 0));

if(moveRight)
    Matrices.viewMatrix.translate(new Vector3f(1.0f * moveSpeed, 0, 0));

 

 

Vertex Shader:

#version 330

uniform mat4 perspectiveMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

layout(location = 0) in vec3 position;

out vec4 color;

void main(){
    gl_Position = perspectiveMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
    color = vec4(position, 1.0);
}

 

 

All sources:
http://pastebin.com/yZExLWpp - Window.java

http://pastebin.com/yBRcdnRK - Matrices.java

http://pastebin.com/mJa1j5ri - Camera.java

http://pastebin.com/UM7BQdrR - Cube.java

 

Tell me if you need more. I don't believe you should though.

 

Thanks!, Slushy.

 


What objects do I use matrices for?

08 November 2012 - 12:28 PM

So, I've been looking alot into matrices lately. I understand all the "math-parts" regarding multiplying, adding them together and all that stuff. What I can't seem to get a grasp of is when do I use them correctly? I know that they are very effective when it comes to camera's. But do I also use them for models?

So that I have a position vector and a rotation vector, load them into a Float buffer and do glLoadMatrix('floafbuffer'); for every model/object that I want to render aswell? Or do I not need to use matrices for theese stuff?

If so, what do I use them for? Only cameras?

Im using Java with OpenGL btw. (LWJGL)

EDIT: I accidentaly posted this in Math and Physics, I meant to post it in "For Beginners". So if any moderator sees this, could you please move it? :) Thanks!

First person camera

30 October 2012 - 10:59 AM

Hello!
Im trying to create a first person camera, and I've been successful in understanding how to do so thus far. I've read up on alot of vector math and matrix stuff and I know the basics. Although I got a few problems when I try to implement it into my code.

I have 3 main vectors, which are position, direction and up. I am trying to use
glTranslatef(x, y, z);
to update my camera-position, but Im not a 100% sure on what values I want to update it with. If I keep updating with my position vector, like so:
glTranslatef(position.x, position.y, position.z);

I will keep moving in one direction, even if im not pressing any buttons. (Moving in the direction that the results of all three values in the position vector will be.) I've been able to work around this by using
glLoadIdentity();
In the main loop, before I do all the translating. Is this the correct way of doing it? Or do I add another vector and translate with that one? Im a bit of a perfectionst, and I want to do it correctly the first time. So I don't have to relearn!

Thanks! I hope im clear enough on what my problem is, I'll be happy to post my sourcecode if you really need it, I felt like it was a bit unnecessary though.

PARTNERS