I'm trying to create a snake game. I managed to create my field with of squares and I drew my red snake as a square with this:
void drawSnake()
{
mat4 modelView;
modelView = Translate(0,0,0);
glUniformMatrix4fv(modelViewUniform, 1, GL_TRUE, modelView);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
Right now I'm not storing my snake anywhere when I draw it. I am thinking maybe I can store the modelView as an object and then translate the object around, which would be the snake moving around. But maybe this is a stupid way to do it, so I thought I would ask for some better ways.
Vertex rectangleData1[rectangleSize] = {
{ vec2( -1.0, -1.0 ), color1 },
{ vec2( 1.0, -1.0 ), color1 },
{ vec2( 1.0, 1.0 ), color1 },
{ vec2( -1.0, -1.0 ), color1 },
{ vec2( 1.0, 1.0 ), color1 },
{ vec2(-1.0, 1.0 ), color1 }
};
You are correct to assume that using and modifying a matrix per game object is the proper technique for transition said game objects. In graphics, these matrices are usually referred to as 'Model' or 'World' matrices. When using OpenGL for 3D applications, you can additionally provide View and Projection matrices to your shader program (only one each used per render) to allow for a realistic, modifyable, presentation of your world space. This camera-like presentation of your world space is called a viewing frustum.
Related
I'm highlighting every pixel on my 3D objects with red color, if the dot-product of my mesh triangle normal with vector (0, 0, -1) is larger than 0.73. It means, if my mesh triangle has a slope larger than a certain value:
I'm using a fragment/pixel shader like this:
#define FP highp
varying FP vec3 worldNormal; // comes from vertex shader
void main()
{
vec3 n = normalize(worldNormal);
vec3 z = vec3(0.0, 0.0, -1.0); // Normalized already
float zDotN = dot(z, n); // Dot-product of "n" and "z"
if ( zDotN > 0.73 ) {
vec3 oc = vec3(1.0, 1.0-zDotN, 1.0-zDotN); // Color: shades of red!
gl_FragColor = vec4(oc, 1.0);
} else {
gl_FragColor = vec4( /* Compute color according to Phong material */ );
}
}
I need to be compatible with older versions of OpenGL and OpenGL ES 2.0 for embedded devices.
My problem is: I need to have access to vertex coordinates (of mesh triangles) of the highlighted areas from within my C++ code. One option might be to use SSBO but they are not available for older versions of OpenGL. Can anybody introduce an approach or workaround?
I am not quite sure what is missing, but I loaded a uniform matrix into a vertex shader and when the matrix was:
GLfloat translation[4][4] = {
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.2, 0.0, 1.0}};
or so, I seemed to be able to translate vertices just fine, depending on which values I chose to change. However, when swapping this same uniform matrix to apply projection, the image would not appear. I tried several matrices, such as:
GLfloat frustum[4][4] = {
{((2.0*frusZNear)/(frusRight - frusLeft)), 0.0, 0.0, 0.0},
{0.0, ((2.0*frusZNear)/(frusTop - frusBottom)), 0.0 , 0.0},
{((frusRight + frusLeft)/(frusRight-frusLeft)), ((frusTop + frusBottom) / (frusTop - frusBottom)), (-(frusZFar + frusZNear)/(frusZFar - frusZNear)), (-1.0)},
{0.0, 0.0, ((-2.0*frusZFar*frusZNear)/(frusZFar-frusZNear)), 0.0}
};
and values, such as:
const GLfloat frusLeft = -3.0;
const GLfloat frusRight = 3.0;
const GLfloat frusBottom = -3.0;
const GLfloat frusTop = 3.0;
const GLfloat frusZNear = 5.0;
const GLfloat frusZFar = 10.0;
The vertex shader, which seemed to apply translation just fine:
gl_Position = frustum * vPosition;
Any help appreciated.
The code for calculating the perspective/frustum matrix looks correct to me. This sets up a perspective matrix that assumes that your eye point is at the origin, and you're looking down the negative z-axis. The near and far values specify the range of distances along the negative z-axis that are within the view volume.
Therefore, with near/far values of 5.0/10.0, the range of z-values that are within your view volume will be from -5.0 to -10.0.
If your geometry is currently drawn around the origin, use a translation by something like (0.0, 0.0, -7.0) as your view matrix. This needs to be applied before the projection matrix.
You can either combine the view and projection matrices, or pass them separately into your vertex shader. With a separate view matrix, containing the translation above, your shader code could then look like this:
uniform mat4 viewMat;
...
gl_Position = frustum * viewMat * vPosition;
First thing I see is that the Z near and far planes is chosen at 5, 10. If your vertices do not lie between these planes you will not see anything.
The Projection matrix will take everything in the pyramid like shape and translate and scale it into the unit volume -1,1 in every dimension.
http://www.lighthouse3d.com/tutorials/view-frustum-culling/
So, I'm trying to rotate a light around a stationary object in the center of my scene. I'm well aware that I will need to use the rotation matrix in order to make this transformation occur. However, I'm unsure of how to do it in code. I'm new to linear algebra, so any help with explanations along the way would help a lot.
Basically, I'm working with these two right now and I'm not sure of how to make the light circulate the object.
mat4 rotation = mat4(
vec4( cos(aTimer), 0.0, sin(aTimer), 0.0),
vec4( 0, 1.0, 0.0, 0.0),
vec4(-sin(aTimer), 0.0, cos(aTimer), 0.0),
vec4( 0.0, 0.0, 0.0, 1.0)
);
and this is how my light is set up :
float lightPosition[4] = {5.0, 5.0, 1.0, 0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPositon);
The aTimer in this code is a constantly incrementing float.
Even though you want the light to rotate around your object, you must not use a rotation matrix for this purpose but a translation one.
The matrix you're handling is the model matrix. It defines the orientation, the position and the scale of your object.
The matrix you have here is a rotation matrix, so the orientation of the light will change, but not the position, which is what you want.
So there is two problems to fix here :
1.Define your matrix properly. Since you want a translation (circular), I think this is the matrix you need :
mat4 rotation = mat4(
vec4( 1.0, 0.0, 0.0, 0.0),
vec4( 0.0, 1.0, 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( cos(aTimer), sin(aTimer), 0.0, 1.0)
);
2.Define a good position vertex for your light. Since it's a single vertex and it's the job of the model matrix (above) to move the light, the light vector 4D should be :
float lightPosition[4] = {0.0f, 0.0f, 0.0f, 1.0f};
//In C, 0.0 is a double, you may have warnings at compilation for loss of precision, so use the suffix "f"
The forth component must be one since it's thanks to it that translations are possible.
You may find additional information here
Model matrix in 3D graphics / OpenGL
However they are using column vectors. Judging from your rotation matrix I do belive you use row vectors, so the translation components are in the last row, not the last column of the model matrix.
I am attempting to model a spotlight in a scene for an introduction to graphics class. The assignment specifies that I must do everything in modernGL, therefore I can't use anything from legacy.
I have been reading the OpenGL 4.0 Shading Language Cookbook for assistance with this, but I can't figure out how to get the eyespace coordinates for my lights. I know the position and direction that I want the lights to be in in worldspace and I have attempted to transform them to eyespace by the following.
//mv = inverse(mv);
vec3 light = vec3(vec4(0.0, 15.0, 0.0, 1.0) * mv);
vec3 direction = vec3(vec4(0.0, -1.0, 0.0, 1.0) * mv);
Where mv is my modelview matrix generated by
glm::mat4 modelview_matrix = glm::lookAt(glm::vec3(window.camera.x, window.camera.y, window.camera.z), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
without any transforms on it. As you can see I have attempted to multiply by the inverse of the modelview and the modelview to get the eyespace. I am positive that neither of these have worked as the specular highlight on my object follows as I move to the opposite side of the object. (ie if I am looking at the opposite side of the object that the light I shouldn't see a specular highlight.)
I'm fairly certain that you want:
vec3 light = vec3(mv * vec4(0.0, 15.0, 0.0, 1.0));
vec3 direction = vec3(mv * vec4(0.0, -1.0, 0.0, 0.0));
// ^ Note the 0 in the w coordinate
Where you have not inverted your mv matrix. Yes, the order of multiplication does matter. The reason you leave a 0 in the w coordinate is because you don't want direction vectors to be translated, only scaled or rotated.
My issue is that I have a (working) orthographic vertex and fragment shader pair that allow me to specify center X and Y of a sprite via 'translateX' and 'translateY' uniforms being passed in. I multiply by a projectionMatrix that is hardcoded and works great. Everything works as far as orthographic operation. My incoming geometry to this shader is based around 0, 0, 0 as its center point.
I want to now figure out what that center point (0, 0, 0 in local coordinate space) becomes after the translations. I need to know this information in the fragment shader. I assumed that I can create a vector at 0, 0, 0 and then apply the same translations. However, I'm NOT getting the correct answer.
My question: what I am I doing wrong, and how can I even debug what's going on? I know that the value being computed must be wrong, but I have no insight in to what it is. (My platform is Xcode 4.2 on OS X developing for OpenGL ES 2.0 iOS)
Here's my vertex shader:
// Vertex Shader for pixel-accurate rendering
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
uniform float translateX;
uniform float translateY;
// Set up orthographic projection
// this is for 640 x 960
mat4 projectionMatrix = mat4( 2.0/960.0, 0.0, 0.0, -1.0,
0.0, 2.0/640.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
void main()
{
// Set position
gl_Position = a_position;
// Translate by the uniforms for offsetting
gl_Position.x += translateX;
gl_Position.y += translateY;
// Translate
gl_Position *= projectionMatrix;
// Do all the same translations to a vector with origin at 0,0,0
vec4 toPass = vec4(0, 0, 0, 1); // initialize. doesn't matter if w is 1 or 0
toPass.x += translateX;
toPass.y += translateY;
toPass *= projectionMatrix;
// this SHOULD pass the computed value to my fragment shader.
// unfortunately, whatever value is sent, isn't right.
//v_translatedOrigin = toPass;
// instead, I use this as a workaround, since I do know the correct values for my
// situation. of course this is hardcoded and is terrible.
v_translatedOrigin = vec4(500.0, 200.0, 0.0, 0.0);
}
EDIT: In response to my orthographic matrix being wrong, the following is what wikipedia has to say about ortho projections, and my -1's look right. because in my case for example the 4th element of my mat should be -((right+left)/(right-left)) which is right of 960 left of 0, so -1 * (960/960) which is -1.
EDIT: I've possibly uncovered the root issue here - what do you think?
Why does your ortho matrix have -1's in the bottom of each column? Those should be zeros. Granted, that should not affect anything.
I'm more concerned about this:
gl_Position *= projectionMatrix;
What does that mean? Matrix multiplication is not commutative; M * a is not the same as a * M. So which side do you expect gl_Position to be multiplied on?
Oddly, the GLSL spec does not say (I filed a bug report on this). So you should go with what is guaranteed to work:
gl_Position = projectionMatrix * gl_Position;
Also, you should use proper vectorized code. You should have one translate uniform, which is a vec2. Then you can just do gl_Position.xy = a_position.xy + translate;. You'll have to fill in the Z and W with constants (gl_Position.zw = vec2(0, 1);).
Matrices in GLSL are column major. The first four values are the first column of the matrix, not the first row. You are multiplying with a transposed ortho matrix.
I have to echo Nicol Bolas's sentiment. Two wrongs happening to make things work is frustrating, but doesn't make them any less wrong. The fact that things are showing up where you expect is likely because the translation portion of your matrix is 0, 0, 0.
The equation you posted is correct, but the notation is row major, and OpenGL is column major:
I run afoul of this stuff every new project I start. This site is a really good resource that helped me keep these things straight. They've got another page on projection matrices.
If you're not sure if your orthographic projection is correct (right now it isn't), try plugging the same values into glOrtho, and reading the values back out of GL_PROJECTION.