moving and rotating vertices with LWJGL - java

Is it possible to move or rotate single vertices or collections of vertices(for example display lists)instead of changing the coordinates of every single vertex with the LWJGL?
Maybe something like GL11.glTranslatef(...) but only for moving parts of the scene.
In addition J have no idea how to rotate something.

You can use OpenGL 2.0 with vertex shader to do this. But it's not so simple like glTranslatef.
Google: OpenGL 2.0 Vertex Transformation. It allows rotate, scale and move vertices.

Related

Java OpenGL - Apply different color per side of glutSolidCube

For my Java OpenGL project I am trying to make a Rubik's Cube.
I've got all rotations calculated and working, but there's one thing I don't know how to do, namely give each side of the cube it's own color.
I used glRotatef and glTranslatef to position each of the 27 blocks, and glutSolidCube to draw each block.
How can I give each side of glutSolidCube a different color?
I've looked at textured cubes, but that seems hard as I don't know the (x, y, z) coordinates of each block, I only have the transformation matrix (rotation and translation).
What is the easiest way to do this?
This may not be possible directly: glutSolidCube does not generate color attributes (see the fghCube function in freeglut source code).
The simplest way would be to generate the geometry of the cube yourself. Generate 6 (faces) * 4 = 24 vertices total, with the expected positions, normals and an additional color attribute per vertex. Just like for the normal attribute, for each of the 8 distinct vertex positions on a cube, you should have 3 different colors (since the same vertex is shared by 3 faces but you need a different color per face).
Another way if you really insist on using glutSolidCube would be to assign the vertex color based on the vertex normal in the vertex shader. But maybe you are not using vertex shaders...

Creating a 2D game with modern opengl

I've tried for some time to create a simple 2D Engine with modern opengl, with modern opengl I mean I'm not going to be using this:
glBegin(); glEnd(); glVertex2i(); glOrtho();
etc,
I'm using GLSL.
And I have run into some problems. Here's some questions
I'm trying to create a 2D coordinate system without using old methods like glOrtho() and glViewport(), I know i'm supposed to use a matrix here and there but how? How do i create a coordinate system?
How do I use texCoords in GLSL to load a texture in 2D space.
If someone could explain or point to a source on how to set the size of a Texture, right now I've to use a position from -1 to 1, but I want to say that the size is 16 * 16 for example.
Is there someway to print out errors from GLSL? When I have an error it's just prints out Couldn't find uniform. Because the location is == -1
How to use and create matrix to to move, scale and rotate in 2D space.
Can I use a Matrix2f instead of Matrix4f?
Please, if you can, post the code in Java Thank you
Disclaimer: I don't have much experience with modern OpenGL but I worked with recent DirectX'es. Most of general conceptions should be the same here.
How do i create a coordinate system?
Nowadays there is no fixed function pipeline so one have to do all coordinate transformations by hand inside a vertex shader. It consumes a vertex then produces one transformed to the screen space. In case of 2D graphics there's even no need to bother with projection matrices.
Consider the input stream consisting from 2D vectors. Thus rotation, scaling and translation implemented with 3D matrices. But output vectors should be 4D. Pseudocode:
input = getInputVertex();
transformed = view * world * vec3(input, 1.0f);
output = vec4(transformed, 1.0f);
Where 'world' is a world matrix of sprite and 'view' is a matrix of camera.

Best way to render a non-cubical sandbox game?

In my game, the world is made of cubes, but the cubes are divided into 5 parts: a tetrahedron and 4 corners. Each type of block has two colors. This is what a block might look like if one corner was cut, although each corner/face may have different colors from the rest.
The problem is, on the tetrahedral faces, I want the edges between the triangles to be seamless. So I can't use textures. (I could, but they would need to be high-res, and if I want to animate the colors (for example on water) this is not an option).
I've found these approaches:
Drawing each triangle on each tetrahedral face, then each square on each cubical face (using a VBO and all that stuff)
Too many polys! Lag ensues. And this was only rendering the tetrahedrals.
Using a fragment shader on world geometry
The math is simple: for each axis, find if the point is less than 0.5 within the cube and xor the results. This determines which color to use. I got lag, but I think my code is bad.
3D textures on world geometry
This seems to be the best option given how perfectly it matches my situation, but I really don't know.
Using instanced geometry with any of the above
I'm not sure about this one; I've read instancing can be slow on large scales. I would need 31 meshes, or more if I want to optimize for skipping hidden surfaces (which is probably unnecessary anyways).
Using a geometry shader
I've read geometry shaders don't perform well on large scales.
Which of these options would be the most efficient? I think using 3d and 2d textures might be the best option, but if I get lag I want to be sure it's because I'm using bad code not an inefficient approach.
Edit: Here's my shader code
#version 150 core
in vec4 pass_Position;
in vec4 pass_Color1;
in vec4 pass_Color2;
out vec4 out_Color;
void main(void) {
if ((mod(abs(pass_Position.x),1f)<=0.5f)^^(mod(abs(pass_Position.y),1f)<=0.5f)^^(mod(abs(pass_Position.z),1f)<=0.5f)) out_Color = pass_Color1;
else out_Color = pass_Color2;
}
The problem is, on the tetrahedral faces, I want the edges between the triangles to be seamless. So I can't use textures. (I could, but they would need to be high-res, and if I want to animate the colors (for example on water) this is not an option).
That's not necessarily the case. Remember that OpenGL doesn't see whole objects, but just individual triangles. So when rendering that cut face, it's in no way different to just render its flat, "fleshless" counterpart.
Any hard edge on the inner tetrahedron doesn't suffer from a texture crease as the geometrical edge is much stronger. So what I'd do is to have a separate 2D planar texture space aligned with the tetrahedral surfaces, which is shared by all faces coplanar to this (on a side note: applying this you could generate the texture coordinates using a vertex shader from the vertex position).
That being said: Simple 2D flat textures will eventually hit some limitations. Since you're effectively implementing a variant of an implicit surface tesselator (with the scalar field creating the surface being binary valued) it makes sense to think about procedural volumetric texture generation in the fragment shader.

Adding a node to multiple Groups : Java3D

Well, I'm developing a application using Java3D, which can solve Rubik's cube showing the user step-step solution.
I did my Algorithm part perfectly, and now the issue is with 3D cube,
Till now, i've created 27 cubes as in real one.
The problem is that, inorder to rotate a face clockwise or anti-clockwise, I should add those individual cubes to a group which in turn can be used to rotate the face anti-clockwise or clockwise direction as i said, but indeed the nodes in edges have to be in multiple groups, so that they can rotate either on X axis or Y axis or else we can take two axis of rotation.
so, my question now is, how to add a node to multiple groups ?
Or if it is not possible, Well then there must be a way to construct a Rubiks cube and to rotate its faces, How to do that!
Can you help me with this!!
Adding the cubes to BranchGroups and dynamically updating them or managing multiple groups sounds complicated.
Perhaps consider keeping the cubes independent. Create three transforms: one to rotate clockwise/counter-clockwise 90 degrees about each of the three axes. To rotate a face, apply the same transform to each of the cubes in that face -- since they are all rotating about the same axis anyways.

Drawing multiple triangles with different colors on OpenGL ES 2.0

I'm trying to figure how to this on OpenGL for SDK using OpenGL ES 2.0 in Java. I want to draw multiple triangles (most like arbitrary) with different colors. I currently have a Triangle class that can draw a triangle with a color, coordinates passed in as a FloatBuffer and color passed in as an array (pretty much following this example since I'm just a beginner to OpenGL).
I tried adding more points to the coordinate array before putting it in the FloatBuffer, but unfortunately that just draws the triangles as one single polygon, and I can't change the color for each individual triangle.
May I get some help on this on how to best start this? I was thinking about creating each class for each triangle, but I don't know if that will be efficient when it comes to drawing a bunch of triangles with different colors. If there is any other simple way, I would really like to hear.

Categories

Resources