why is my texture displaying so strangely with slick util? - java

I'm trying to use slick util to display a logo in the centre of the screen.
texture.bind();
glBegin(GL11.GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + texture.getTextureWidth(), y);
glTexCoord2f(1, 1);
glVertex2f(x + texture.getTextureWidth(), y + texture.getTextureHeight());
glTexCoord2f(0, 1);
glVertex2f(x, y + texture.getTextureHeight());
glEnd();
But when it renders the texture isn't centred, and a random line is drawn on the right. I have no idea why. Thanks.

FIXED: Make your image a power of 2. I did this, and it now works :D
I can vouch for this. I use LWJGL to render everything in my game I am making, and I use it do render backgrounds, which is okay, but when I render things less than the full size of the display, it shows random lines and the image is slanted. Here is a screenshot picture; http://imageshack.us/f/39/problema.png/ <<<<--- See the split line about a third of the way across, and the line on the left hand side.
And here is my code;
ImageLoader.STAT_BAR.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0); //TOP LEFT
GL11.glVertex2f(0,0);
GL11.glTexCoord2f(1,0); //TOP RIGHT
GL11.glVertex2f(ImageLoader.STAT_BAR.getTextureWidth(),0);
GL11.glTexCoord2f(1,1); //BOTTOM RIGHT
GL11.glVertex2f(ImageLoader.STAT_BAR.getTextureWidth(), ImageLoader.STAT_BAR.getTextureHeight());
GL11.glTexCoord2f(0,1); //BOTTOM LEFT
GL11.glVertex2f(0, ImageLoader.STAT_BAR.getTextureHeight());
GL11.glEnd();

Related

2D overlay over 3D scene not working

I am attempting to create a 2D overlay over a 3D scene! I have tried all the solutions I can find on GameDev and StackOverflow, however they have not seemed to work!
My current code:
static void ready3D()
{
glViewport(0, 0, Display.getWidth(),Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45, (float) Display.getWidth()/Display.getHeight(), 0.1f, 5000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
}
static void ready2D()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluOrtho2D(0.0f, Display.getWidth(), Display.getHeight(), 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.375f, 0.375f, 0.0f);
glDisable(GL_DEPTH_TEST);
}
And,
glPushMatrix();
//Overlay start - This is in my render method BTW.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ready2D();
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex2f(0, 0); // bottom-left
glVertex2f(0, 1); // top-left
glVertex2f(1, 1); // top-right
glVertex2f(1, 0); // bottom-right
glEnd();
glPopMatrix();
ready3D();
However the 2D box thing that I am trying to draw does not draw! Obviously I eventually hope to have complicated objects/icons on an overlay, but first things first.
The 3D world still draws totally fine.
Is anyone able to tell me what I am doing wrong?
The order of the vertices in the 2D quad look a bit suspicious to me, if I'm reading that rightly it's going: bottom-left, top-right, bottom-right, top-left, which results in a sort of cross rather than a quad.
Try this:
glVertex2f(0, 0); // bottom-left
glVertex2f(0, 1); // top-left
glVertex2f(1, 1); // top-right
glVertex2f(1, 0); // bottom-right
Note the clockwise ordering which I believe OpenGL is expecting by default for front-facing quads.

opengl rotation texture off center

I wrote some code to get a plain 2d box to face the mouse. It rotated just fine around its center, and everything was working great, but when i put a texture on the box, it did not rotate around the center anymore.
The code:
float imgWidth = texture.getImageWidth()*scale;
float imgHeight = texture.getImageHeight()*scale;
glLoadIdentity();
texture.bind();
glTranslatef(x, y, 0);
glRotated(rotation - 90, 0, 0, 360);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-imgWidth, -imgHeight);
glTexCoord2f(1, 0);
glVertex2f(imgWidth, -imgHeight);
glTexCoord2f(1,1);
glVertex2f(imgWidth, imgHeight);
glTexCoord2f(0, 1);
glVertex2f(-imgWidth, imgHeight);
glEnd();
The answer is simple but with a complicated background that has to be understood.
OpenGL always rotate something not around its center but takes as center the point (0;0).
This can be an issue because if you translate your object somewhere and then your rotate it, it will rotate not on its center but around the (0;0) point (origin) creating a big rotation, I will say as a planet around the Sun.
Also OpenGL works with matrix, that with very barbaric simplification means that the operation are executed bottom to top.
// store the current model matrix
GL11.glPushMatrix();
// bind to the appropriate texture for this image
this.texture.bind();
// translate to the right location and prepare to draw
GL11.glColor3f(1, 1, 1);
GL11.glTranslated(x + (this.texture.getImageWidth() / 2), y + (this.texture.getImageHeight() / 2), 0);
GL11.glRotated(this.angle, 0, 0, 1);
GL11.glTranslated(-this.texture.getImageWidth() / 2, -this.texture.getImageHeight() / 2, 0);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, this.texture.getHeight());
GL11.glVertex2f(0, this.texture.getImageHeight());
GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight());
GL11.glVertex2f(this.texture.getImageWidth(), this.texture.getImageHeight());
GL11.glTexCoord2f(this.texture.getWidth(), 0);
GL11.glVertex2f(this.texture.getImageWidth(), 0);
}
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
This means that first I am moving the texture to have its center positioned at (0;0) that means translating it backward half the dimensions.
Then I rotate it, and this is the crucial point, because you are using a strange way to rotate it, maybe the problem it's here, take a look to the javadoc:
SPECIFICATION
void glRotated( GLdouble angle,<br>
GLdouble x,<br>
GLdouble y,<br>
GLdouble z )<br>
void glRotatef( GLfloat angle,<br>
GLfloat x,<br>
GLfloat y,<br>
GLfloat z )<br>
PARAMETERS<br>
angle Specifies the angle of rotation, in degrees.
x, y, z<br>
Specify the x, y, and z coordinates of a vector, respectively.
DESCRIPTION<br>
glRotate produces a rotation of angle degrees around the<br>
vector (x,y,z).
First of all all x,y,z values should be between 0 and 1, if you want to rotate a 2d image then you should use the z axis, so the third param will be 1 that mean that you are rotating your image around the unit vector z.
The angle should be in degree and can be either positive or negative.
Try to change your code according to the documentation and you will have your problem solved.
Also with your quad you are drawing a 2x scaled quad, you are starting from -imageWidth to +imageWidth that means 2 times the width...

Text field using OpenGL

So, I'm new to OpenGL and trying to create a 2D Multiplayer game, I know how to do all of the networking, although the graphics portion is honestly kicking my buttox.
I have tried looking at NiftyGUI aswell as TWL as they have been recommended a-lot, although I can't seem to get a grasp of them as there aren't very many tutorials and no videos to help explain what's going on, not to mention the OpenGL Documentary page is just horribly laid out.
I've attemped drawing a black rectangle, which I was going to go ahead and outline in white somehow and making that a temporary textbox, by drawing white font onto it, although I don't even know how to draw font. These are just a few of the things I'm strugling with that I can't find and I'm aware that I'm going to have to use some libraries, so I'll name the ones I have implemented currently.
LWJGL
Slick2D
I don't have any others currently, besides for TWL but I can't figure out how to use it for the life of me.
Here's the code that I made myself while trying to get a small black rectangle going
void drawTextBox(int fromLeft, int fromTop, int width, int height) {
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(fromLeft, fromTop); // Upper Left
glTexCoord2f(1, 0);
glVertex2i(fromLeft - width, fromTop); // Uppright
glTexCoord2f(1, 1);
glVertex2i(fromLeft - width, fromTop + height); // Bottom right
glTexCoord2f(0, 1);
glVertex2i(fromLeft, fromTop + height); // bottom left
glEnd();
}
This is working correctly, all but one part... It's drawing the last texture that I have loaded, even though I'm not binding it anywhere in the program, because I've made sure of it. Then it's scaling it to fit into the dimensions of the text-box.
Could someone help me resolve this error and direct me to where I can learn how to set the opacity of quads as-well as draw some text?
Try binding the texture you want right before calling the function. Without knowing how you've "made sure" that the other texture is not being bound, that's all I can say. Also, for changing opacity simply use one of GL11.glColor4_ functions; the last parameter is the alpha (opacity) of the color.
void drawTextBox(int fromLeft, int fromTop, int width, int height) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0f, 1.0f, 1.0f, <B>0.5f</B>);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(fromLeft, fromTop); // Upper Left
glTexCoord2f(1, 0);
glVertex2i(fromLeft - width, fromTop); // Uppright
glTexCoord2f(1, 1);
glVertex2i(fromLeft - width, fromTop + height); // Bottom right
glTexCoord2f(0, 1);
glVertex2i(fromLeft, fromTop + height); // bottom left
glEnd();
glDisable(GL_BLEND);
}
This block of code allows you to draw a white rectangle with %50 opacity.
Take a look at the BOLDED part (0.5f). This float number defines the opacity of your rectangle. The bigger this number is (0.0f~1.0f), the more the opacity of the rectangle is.

Trouble when drawing a 2nd object without texture

Im having trouble geting a 2nd box to render.
So fare I have 1 object drawn with a texture and it seems to work fine but I have some code attached to it, so if I hold down the left mouse button ontop of it a 2nd box apears to its right.
Instead the 2nd box fails to apear and the textured box disaperes altogether.
I think im missing something with opengl, like something i have to enable or disable before or after drawing 1 thing.
heres my initilization code for opengl:
private static void glInit(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glClearColor(0, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
}
heres the code that draws the objects:
public void render(){
if(myImage != null){
myImage.bind();
glBegin(GL_QUADS);
{
// upper left
glTexCoord2f(0, 0);
glVertex2f(x, y);
// upper right
glTexCoord2f(1, 0);
glVertex2f(x + size, y);
// bottom right
glTexCoord2f(1, 1);
glVertex2f(x + size, y + size);
// bottom left
glTexCoord2f(0, 1);
glVertex2f(x, y + size);
}
glEnd();
}
if(drawChoiceBox){
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
{
glVertex2f(x + size, y - size);
glVertex2f(x + size*2, y - size);
glVertex2f(x + size*2, y + size*2);
glVertex2f(x + size, y + size*2);
}
glEnd();
}
}
I have tested the logic for geting the 2nd box up and its works so i guess it must be me missing somthing with the way opengl draws things.
Any idea what im missing? that is ALL the opengl code in my project right now.
It doesn't look like you're disabling texturing for the 2nd object, so it's being drawn with texture.
Meanwhile your first box doesn't have glColor() specified, so after drawing the 2nd box, the first will be red. This could make it disappear, if modulating by red produces black, and modulation is enabled.
So you should probably explicitly enable/disable texturing when drawing your quads, as well as specifying all the vertex attributes which are changed, such as colour.

lwjgl colors all items on screen

I have a several quads which are successfully being displayed on the screen, however when I try to set the colour of one of them it sets the color of all of them.
GL11.glColor3f(red,green,blue);
xh = getXsize() / 2;
yh = getYsize() / 2;
GL11.glPushMatrix();
GL11.glTranslated(x, y, 0);
GL11.glTranslatef(10.0f, 10.5f, -0.0f);
GL11.glRotated(rotate, 0.0f, 0.0f, -1.0f);
GL11.glTranslatef(-10.0f, -10.5f, 0.0f);
GL11.glTranslated(-x, -y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(x - xh, y - yh);
GL11.glVertex2d(x - xh, y + yh);
GL11.glVertex2d(x + xh, y + yh);
GL11.glVertex2d(x + xh, y - yh);
GL11.glEnd();
GL11.glPopMatrix();
As far as I see, only one quad is drawn in the code you have posted. Anyways, when you call GL11.glColor3f(red,green,blue) it will be applied to all following quads you draw after calling it. Basically, if you want different color quads, you need to call the function with different values, every time before drawing new quad.
You should give a look on the following to this resource (it's not JWJGL, but OpenGL) in the section called : Specifying a Color and a Shading Model, which explains bit better how the color is being applied to the simple shapes.

Categories