Trouble when drawing a 2nd object without texture - java

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.

Related

Stopping glUseProgram()

I'm using an opengl shader on a texture. Once the texture is done shading I want to stop the glUseProgram() function.
Presently the shader is going overtop of everything, including rectf() functions that I need to not be shaded.
I tried glUseProgram(0) but that didn't work.
related question: Java Opengl: Discarding Texture Background with Shaders
Here is the relevant code.
glPushMatrix();
dirPosd = i.torso.getPosition().mul(30);
glTranslatef(dirPosd.x, dirPosd.y, 0);
glRotated(Math.toDegrees(i.torso.getAngle()), 0, 0, 1);
glColor3f(1,1,1);
skel_torso.bind();
sizer = 40;
glUseProgram(shaderProgram);
glBegin(GL_QUADS);
glTexCoord2f(0f, 0f);
glVertex2f( i.torso.getPosition().x - sizer-5, i.torso.getPosition().y - sizer-5); //NW
glTexCoord2f(1, 0);
glVertex2f( i.torso.getPosition().x + sizer-5, i.torso.getPosition().y - sizer-5); //NE
glTexCoord2f(1, 1);
glVertex2f( i.torso.getPosition().x + sizer-5, i.torso.getPosition().y + sizer-5); //SE
glTexCoord2f(0, 1);
glVertex2f( i.torso.getPosition().x - sizer-5, i.torso.getPosition().y + sizer-5); //SW
glEnd();
glPopMatrix();
glUseProgram(0); //Note here
glPushMatrix();
Vec2 shoulderPosL = i.shouldL.getPosition().mul(30);
glTranslatef(shoulderPosL.x, shoulderPosL.y, 0);
glRotated(Math.toDegrees(i.shouldL.getAngle()), 0, 0, 1);
glColor3f(1,1,0);
glRectf(-i.shoulderSize[0] * 30, -i.shoulderSize[1] * 30, i.shoulderSize[0] * 30, i.shoulderSize[1] * 30);
glPopMatrix();`
I've added a glUseProgram(0) statement, but when I added that the shader doesn't work at all.
You have to switch to a different shader, doing what you want for the other stuff rendering. Shaders are not something "wrapped" over something you render. Shaders are what make rendering work. If you disable the shaders, and your OpenGL context is not a compatibility profile no default fallback (to fixed function pipeline behavior) is provided and nothing renders. OTOH you're using the fixed function pipeline, but for that to work you have to parametize it properly (enable texture targets, modulation modes, colors and so on). It's generally easier to simply switch shaders instead of juggle with the FF pipeline state.

LWJGL draw rect over texture

I use the following to draw the texture quads
tex.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();
glLoadIdentity();
with these configurations:
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
however now i want to add a menu to the game by drawing a rect over the screen but when i do so, everything turns red (if i set red as color)
case MENU:
GL11.glColor3f(1.0f, 0.0f, 0.0f);
glRectf(50, 50, 50, 50);
Two things:
Whenever you draw using color instead of textures, make sure to disable GL_TEXTURE_2D by calling glDisable(GL_TEXTURE_2D); before the rendering call (in your case before case MENU:). But don't forget to enable it again as soon as you're finished and ready to draw the texture once again (in your case, call glEnable(GL_TEXTURE_2D); after glRectf()). This might not be the reason for the problem that you're experiencing, but it will become a problem later.
The reason as to why everything turns red, is because whenever you render a texture, the currently bound color affects what color components are rendered from the texture. Every pixel in the texture that you're drawing is composed of 3 or 4 values. Red, Green, Blue and possibly an Alpha channel. Since you bind the color to red, and you do not change it before you draw the texture, OpenGL only draws the red component of each pixel within your texture. Thus, the Red component will be ranging from 0-1 in your texture, and the Green and Blue values will be staying at a constant 0.
To fix this, simply call glColor3f(1, 1, 1); After rendering the red rectangle. This will set the rendering color back to white (default) and therefore OpenGL will successfully draw all 3 color components of your texture pixels.
I hope that I've been clear and that this helps.
Good luck! :)
//SK

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.

why is my texture displaying so strangely with slick util?

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();

Categories