I have to solve a problem and I realize it is a bit oldschool code..
I need to write down the order of transformations from 1 to 4 and the result for pruple vertex. Would someone help me check whether it is correct and if not - why?
It is a bit tough for me to find answers to this and be 100% sure it is correct.
What I think is correct:
1. Start from bottom, take MODELVIEW first, then PROJECTION
- Yet I am not sure I did it right...
EDIT, code rewritten to text:
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glTranslatef(-1, -1, -0);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glScalef(2, 1, 3);
gl.glRotatef(-90, 0, 0, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glScalef(2, 3, 1);
gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0, 0, 1);
gl.glVertex3f(-2, -2, -2);
gl.glColor3f(1, 1, 0);
gl.glVertex3f(2, 1, 3);
gl.glColor3f(1, 0, 1);
gl.glVertex3f(1, 1, -2);
gl.glColor3f(0, 1, 0);
gl.glVertex3f(-1, 1, 2);
gl.glEnd();
Write the transformations as they go in order and write the coordinate changes of purple vertex for each transformation.
Transform 1:________________
Coordinates x:_______ y:_______ z: _______
Transform 2:________________
Coordinates x:_______ y:_______ z: _______
Transform 3:________________
Coordinates x:_______ y:_______ z: _______
Transform 4:________________
Coordinates x:_______ y:_______ z: _______
Problem solved
supposed to start with model transforms and then projection, always from the bottom
apply transforms from bottom
Also, I was accidentally using wrong coordinates..
~thanks for help though!
Related
I want to create realistic terrain using java and lwjgl.
At now i have double[100][100] with my heightmap and function redering every triangle from a list.
In example - drawing smal "floor" chunk with two triangles:
triangles.add(new Triangle(new Vector3f(x, (float) map[x][z] * 100, z),
new Vector3f(+0.5f, 1, -0.5f),
new Vector3f(-0.5f, 1, -0.5f),
new Vector3f(+0.5f, 1, +0.5f), rgb, rgb, rgb));
triangles.add(new Triangle(new Vector3f(x, (float) map[x][z] * 100, z),
new Vector3f(-0.5f, 1, +0.5f),
new Vector3f(-0.5f, 1, -0.5f),
new Vector3f(+0.5f, 1, +0.5f), rgb, rgb, rgb));
triangles parameters is:
start point XYZ coordinates,
first triangle point "sub" coords,
second triangle point "sub" coords,
third triangle point "sub" coords,
rgb for first point,
rgb color for second point,
rgb color for third point,
I cant do it... I think it is very easy and Im so stupid or so tired. Can you help me?
I'm in the process of displaying j3d GeometryArrays as combined TriangularMeshes in JavaFX. I know the GeometryArrays I am receiving are TriangleStripArrays, and the order allows me to build the correct faces and display the meshes in the scene.
However, I am at a loss on how to determine the vertex winding order based on the TriangleStripArray alone. The faces currently have no correct notion of backface culling, leaving me a complete TriangleMesh that appears distorted from any given angle. By changing the CullFaceMode from BACK to NONE, and by plotting the vertices in the scene, I can tell that all faces are being correctly mapped, just culling inconsistently.
I have two methods which build a TriangleMesh from any given triangular face containing 3 vertices, one for CW winding and one for CCW.
Essentially:
float[] points = {v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, v3.x, v3.y, v3.z};
float[] texCoords = {1, 1, 1, 0, 0, 1};
int[] faces = new int[]{0, 0, 1, 1, 2, 2};
vs
float[] points = {v2.x, v2.y, v2.z, v1.x, v1.y, v1.z, v3.x, v3.y, v3.z};
float[] texCoords = {1, 0, 1, 1, 0, 1};
int[] faces = new int[]{0, 0, 1, 1, 2, 2};
My question is, how can I determine the vertex winding order, and which method should be used per face, based on the TriangleStripArray alone? (TexCoordinates are being supplied, but not used)
Thanks in advance!
- R. Melville
This can be solved by computing the cross product of the vectors (b-a) x (c-y) if a,b and c are the vertices of your triangles.
Our frinds from Math-Stackoverflow have a more detailed explanation :-)
why-does-cross-product-tell-us-about-clockwise-or-anti-clockwise-rotation
I have been trying to add mouse click detection on the triangles of the mesh, but it seems that I am doing something wrong and I cannot figure out how to solve the problem.
So before explaining the problem I will define the environment(the full code is available at http://pastebin.com/TxfNuYXZ):
Camera position
cam = new OrthographicCamera(10, 9);
cam.position.set(0, 5.35f, 2f);
cam.lookAt(0, 0, 0);
cam.near = 0.5f;
cam.far = 12f;
Mesh renders 4 vertices.
mesh = new Mesh(true, NUM_COLUMNS * NUM_LINES, (NUM_COLUMNS * 6 - 6) * (NUM_LINES - 1), VertexAttribute.Position(), VertexAttribute.ColorUnpacked());
mesh.setVertices(new float[] {
0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 1, 0, 1,
0, 0, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 0, 1 });
mesh.setIndices(new short[] { 2, 0, 1, 2, 3, 1 });
So when I run the application I try to check if the click was done inside some of the triangles of the mesh. Now the result depends on the position of the camera. When the camera has almost top view(like in the following picture), corresponding to around 6 on Y axes, the click point is being correctly translated to the coordinates and corresponds to what is actually being seen.
When I move camera on the Y axes to lower position (around 2 or 3), so the image looks like the following one
the click is being detected in the completely wrong positions (the red line shows the place where the click is detected).. Which seems to be right according to the coordinates, but not according to what is being seen..
I would like to understand what an I missing to be able to detect clicks on what actually is being seen? The code I use to detect the click is the following:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Ray ray = cam.getPickRay(screenX, screenY);
Vector3 intersection = new Vector3();
float[] v = new float[NUM_COLUMNS * NUM_LINES * VERTEX_SIZE];
short[] i = new short[(NUM_COLUMNS * 6 - 6) * (NUM_LINES - 1)];
mesh.getIndices(i);
if (Intersector.intersectRayTriangles(ray, mesh.getVertices(v), i, VERTEX_SIZE, intersection)) {
System.out.println(intersection);
}
return false;
}
Thanks a lot for your help!
Basically, after several days of drawing and some math I have found the source of the problem. The Vertex Shader, in order to determine the position of the vertices, was performing a_position * u_projectionViewMatrix multiplication, which was resulting on what was looking fine on the screen, but actually when you compare with actual coordinates of the mesh it was wrong. Now if you check the examples at enter link description here, you can see, that gl_Position is being calculated by multiplying u_projectionViewMatrix * a_position. Making the correct calculation made the trick.
I also had to change the camera to perspective, since the mesh was not rendered how I wanted it to.
I'm following along with the OpenGL tutorial found here. I'm on chapter 2 right now and it's going over the advantages of using glArrayElement to render objects. Currently, my code is as follows:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
double vertices[] = {100, 200, 0, 200, 100, 0, 100, 100, 0};
double colors[] = {1, .5, .8, .3, .5, .8, .3, .5, .8};
DoubleBuffer vertexBuffer = BufferUtils.createDoubleBuffer(9).put(vertices);
DoubleBuffer colorBuffer = BufferUtils.createDoubleBuffer(9).put(colors);
glVertexPointer(3, 0, vertexBuffer);
glColorPointer(3, 0, colorBuffer);
while(!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glVertex3d(300, 200, 0);
glVertex3d(400, 100, 0);
glVertex3d(300, 100, 0);
glEnd();
//Display.sync(60);
Display.update();
}
The second triangle, defined explicitly by calls to glVertex3d is rendered fine. Bu the first triangle does not render at all. Am I making a simple mistake?
While scouring for more sample code, I came across a snippet that said you had to "flip each buffer." Adding
vertexBuffer.flip();
colorBuffer.flip();
Right before the while loop solved my problem!
I have managed to get a cube rendered in OpenGL using a VBO. My next goal is actually creating a for loop to create multiple cubes. I'm stuck on this part though, do I put this code:
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL11.glDrawArrays(GL11.GL_QUADS, 0, 24);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
Into a for loop? Wouldn't I have to use some sort of glPopMatrix command along with a translate function? I barely understand how to create one cube in a VBO, so sorry if its obvious whats wrong.
You can use the following way:
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
for (int i = 0; i < cubeCount; i++) {
GL11.glPushMatrix();
// do translation/rotation for cube no i
GL11.glDrawArrays(GL11.GL_QUADS, 0, 24);
GL11.glPopMatrix();
}
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
Please note that the glPushMatrix/glPopMatrix way is deprecated in newer openGl versions. For you it should work because you are using GL11.