LWJGL Rotating Only One Object On Screen - java

I'm trying to rotate one of two objects on the screen. How should I do this? I can only get it to rotate all the objects on the screen.
Code:
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex3f(0f, 0f, 0f);
glVertex3f(0f, .5f, 0f);
glVertex3f(.5f, .5f, 0f);
glVertex3f(.5f, 0f, 0f);
glEnd();
glRotated(.1, 0f, 1f, 0f);
glRotated(.1, 1f, 0f, 0f);
glRotated(.1, 0f, 0f, 1f);
glBegin(GL_QUADS);
glVertex3f(2f, 0f, 0f);
glVertex3f(2f, .5f, 0f);
glVertex3f(2.5f, .5f, 0f);
glVertex3f(2.5f, 0f, 0f);
glEnd();
code I have so far.

Calling glRotated endlessly without resetting the matrices is a bad idea. Rounding errors will accumulate. You currently don't call glIdentity each frame. Render each frame the following way:
Call glIdentity
Draw the first quad
Rotate
Draw the second quad
The way you render things the rotation difference of the two quads is only 1/10th of a degree on the three axis - which is not noticeable.
Be sure to read up on OpenGL, to learn the current way of OpenGL programming and try to improve your code. It looks as if you don't quite understand what you're doing, no offence.

use glPushMatrix(); before your rotation transforms and glPopMatrix(); after the second quad is rendered then only the second quad will be affected by the rotation transforms.

Related

Libgdx Stencil Buffer overlap

I have a question about using the Stencil Buffer with OpenGL and libgdx
As example I need to draw to circles with overlap.
The code of render method is here:
Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_STENCIL_BUFFER_BIT);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
Gdx.gl.glEnable(GL20.GL_STENCIL_TEST);
Gdx.gl.glClearStencil(0);
Gdx.gl.glClear(GL20.GL_STENCIL_BUFFER_BIT);
Gdx.gl.glStencilFunc(GL20.GL_ALWAYS, 1, 0);
Gdx.gl.glStencilOp(GL20.GL_KEEP, GL20.GL_INCR, GL20.GL_INCR);
shapeRenderer.setColor(new Color(1f, 0f, 0f, 1f)); // red
shapeRenderer.circle(200, 200, 100);
shapeRenderer.circle(180, 180, 100);
Gdx.gl.glStencilFunc(GL20.GL_EQUAL, 2, 0xFF);
Gdx.gl.glStencilOp(GL20.GL_REPLACE, GL20.GL_REPLACE, GL20.GL_REPLACE);
shapeRenderer.setColor(new Color(0f, 1f, 0f, 1f)); // green
shapeRenderer.circle(200, 200, 100);
shapeRenderer.circle(180, 180, 100);
Gdx.gl.glDisable(GL20.GL_STENCIL_TEST);
shapeRenderer.end();
Result is:
Expected something like this:
Overlap should be in other color.
What is a problem here?
The logic looks OK as far as it goes, so I suspect the issue is that your framebuffer that you are rendering in to doesn't actually have a stencil attachment. If there is no stencil buffer OpenGL ES behaves as if the stencil test is disabled, so your final two circles will always overdraw the first two.
Check your context creation (for window surfaces), or your FBO creation for off-screen render targets.

LWJGL shading textured rects, how to rotate mask?

I have been learning how to use shaders in lwjgl. Now i have ran into an issue, where i need to rotate my mask for shading to get different results. How can i modify the mask transformation/rotation after it is bind, or do i need to modify it before. I tried almost anything but couldn't find an existing question / instructions. Any help will be greatly appreciated! This is my code for drawing a textured rectangle using my custom shader program.
private static void drawTexture(int shaderProgram, int texID1, int texID2) {
glUseProgram(shaderProgram);
setTextureUnits(shaderProgram);
bindTextures(texture1.getTextureID(), texture2.getTextureID(), mask.getTextureID());
glBegin(GL11.GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0,0);
glVertex2f(-0.5f,-0.5f);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1,0);
glVertex2f(+0.5f,-0.5f);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1,1);
glVertex2f(+0.5f,+0.5f);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0,1);
glVertex2f(-0.5f,+0.5f);
glEnd();
glUseProgram(0);
}
Here is the answer to my own question:
create a matrix2 variable, and rotate the textCoords of your mask. the -+0.5's are for rotate around center. To fix any issues like exposed corners, clamp your textures on to the edge.
shader:
mat2 textureMatrix = mat2(cos(angle), sin(angle), -sin(angle), cos(angle));
float mask = texture2D(mask, textureMatrix*(gl_TexCoord[0].xy-0.5)+0.5).a;

My Engine flickers when I try to render 2D and 3D

I am working with LWJGL to make a game. It's very basic. Before even implementing any sort of gpu rendering, or fancy model loaders, I wanted to make sure I could at least render 2D and 3D at the same time; My game has a gui while you walk around. Or at least, it is supposed to. Here is my initialization code; The flickering does not happen when I only render 3D.
public void clearGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
}
public void init3D() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective((float) 68, Engine.size[0] / Engine.size[1], 0.3f, 1000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
public void render3D(Camera c) {
init3D();
clearGL();
//Do translations here
glTranslatef(0f, -5f, 0f);
glColor3f(0, 1, 0);
glBegin(GL_QUADS);
glVertex3f(-50f, 0f, -50f);
glColor3f(0, 0, 1);
glVertex3f(50f, 0f, -50f);
glColor3f(1, 0, 0);
glVertex3f(50f, 0f, 50f);
glColor3f(0, 1, 1);
glVertex3f(-50f, 0f, 50f);
glEnd();
}
public void init2D() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Engine.size[0], 0, Engine.size[1], -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
}
public void render2D() {
init2D();
glBegin(GL_QUADS);
glVertex2f(0f, 50f);
glVertex2f(50f, 50f);
glVertex2f(50f, 0f);
glVertex2f(0f, 0f);
glPopMatrix();
}
I can tell its rendering at all because I am drawing a quad to represent the floor in JBullet. For some reason it is above the cameras head, but when I translate the camera up towards it it get's further away, which is why I translated the Camera to -5. That's another problem, for another day.
You should really consider disabling the depth test when you "switch" from 3D to 2D if you are going to draw at Z=0 (middle of your depth range). Half of the visible space in your 3D scene will potentially obstruct your 2D drawing if you do not do this. Alternatively, you could replace your glVertex2f (...) calls with glVertex3f (x,y, -1.0) to bring everything in 2D to the very front of the depth range.
But the really weird thing about all of this is the end of your render2D (...) function: You never call glEnd (...) and you pop a matrix that you appear never to have pushed. That is two sources of mismatched weirdness, either one of them could be causing your problem.

Fixed (in world coordinates) lighting position in LWJGL?

I have some cube models and a lantern model in the "world" (with normals) and a light source which is placed in specific coordinates. The problem comes when I try move around the models, position of lightsource changes every frame to viewport position. Here is my initialization part of code, which contain lighting init:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearDepth(1.0f);
GL11.glClearColor(0.0f, 0.75f, 1.0f, 1);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LESS);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
byf = temp;
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip()); // Position The Light
GL11.glEnable(GL11.GL_LIGHT1);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glFogi(GL11.GL_FOG_MODE, fogMode[fogfilter]);
temp.asFloatBuffer().put(fogColor).flip();
GL11.glFog(GL11.GL_FOG_COLOR, temp.asFloatBuffer());
GL11.glFogf(GL11.GL_FOG_DENSITY, 0.35f);
GL11.glHint(GL11.GL_FOG_HINT, GL11.GL_DONT_CARE);
GL11.glFogf(GL11.GL_FOG_START, 1.0f);
GL11.glFogf(GL11.GL_FOG_END, 5.0f);
GL11.glEnable(GL11.GL_FOG);
I have seen some similar problems but their solutions didn't help me at all. I know that the problem is with irregular order of setting Matrixes, but I have no idea which one (Matrix) and where I should change.
I tried to place setting Modelview matrix before the init of lighting but it didn't work for me.
By the way, I don't want to set the correct light position every frame, it works for me but it can slow the render.
Your'e probably using a code similar to this or glulookat().
GL11.glTranslatef(Camera.x, Camera.y, Camera.z);
GL11.glRotatef(Camera.roatationz, 0.0f, 0.0f, 1.0f);
GL11.glRotatef(Camera.roatationx, 1.0f, 0.0f, 0.0f);
GL11.glRotatef(Camera.roatationy, 0.0f, 1.0f, 0.0f);
//Render stuff here
Your light has to move as well. :)

Problem texture mapping a quad in OpenGL with Ortho2D

The following code produces the image that follows. The image I am using for the background is 640 x 480, as is the displayMode. The texture background is a .bmp and is loaded with the Slick texture loader. I am confuse to why it is not filling the Quad and why it is reflected.
EDIT: The background of my OpenGL scene is pink, the black you see is from the Quad created. The background image is the green block with a 2px light blue border with "test" plastered on it.
private void renderBackground(){
float w = displayMode.getHeight()/2;
float h = displayMode.getWidth()/2;
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GLU.gluOrtho2D(-w, w, -h, h);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_DEPTH_TEST);
if(useTextures)background.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2f(-w,-h);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2f(w,-h);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2f(w, h);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2f(-w, h);
GL11.glEnd();
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
}
Now when I add GL11.glTranslatef(20.0f, 20.0f, 0.0f); you will notice that the pink appears, which is the colour created int my "initGL" method:
GL11.glClearColor(1.0f, 0.75f, 0.796f, 0.0f);
My GL_PROJECTION contains the following before pushing it, my GL_MODELVIEW is unmodified when renderBackground() is called.
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(45.0f, (float) displayMode.getWidth() / (float) displayMode.getHeight(), 0.1f, 25.0f);
//position camera
GLU.gluLookAt(5.0f, 3.0f, -5.0f, 0.0f, 0.0f, -10.0f, 0.0f, 1.0f, 0.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
I need more information to determine the problem, but here is a list with some possibilities.
1) You are using an older video card, which does not support texture non-power of 2, since you are using a library to load the texture, maybe it is detecting it, creating a power of two image, and filling it with black.
2) You (or some library you are using) changed the matrix of the texture matrix stack, and it is changing the texture coordinates.
3) You are doing something wrong when you load the texture (or call the library to do so).
The first thing I would check is if your video card supports texture non-power of 2 extension. You can check it at runtime, see how to detect if openGL/card supports non power of 2?
What I see first, is that you compute
float w = displayMode.getHeight()/2;
float h = displayMode.getWidth()/2;
switched?
Second, the texture could be flipped because the loader flipped it (when I remember right this happened to me, too especially with BMPs).

Categories