I am creating a program which draws an object, which needs to "point" toward the mouse. To do so I want to draw the image rotated.
I have this code for drawing the object:
glBindTexture(GL_TEXTURE_2D,img.getTextureID());
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(x-r,y-r);
glTexCoord2f(1,0);
glVertex2f(x+r,y-r);
glTexCoord2f(1,1);
glVertex2f(x+r,y+r);
glTexCoord2f(0,1);
glVertex2f(x-r,y+r);
glEnd();
Pretty basic. But I was having a hard time finding out how to rotate the texture, or draw the texture rotated.
To elaborate on #genpfault's answer:
If you want your texture to rotate around its center, you need to draw it so its center is at (0,0). Try something like this:
glPushMatrix();
glTranslatef(x,y); // move to the proper position
glRotatef( angle, 0, 0, 1 ); // now rotate
glBindTexture(GL_TEXTURE_2D,img.getTextureID());
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-r,-r);
glTexCoord2f(1,0);
glVertex2f(+r,-r);
glTexCoord2f(1,1);
glVertex2f(+r,+r);
glTexCoord2f(0,1);
glVertex2f(-r,+r);
glEnd();
glPopMatrix(); // pop off the rotation and transformation
Note that glRotatef's angle is in degrees, not radians.
Rotating the texture:
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glRotatef( angle, 0, 0, 1 );
glMatrixMode( GL_MODELVIEW );
drawTexturedQuad();
Rotating the quad:
glPushMatrix();
glRotatef( angle, 0, 0, 1 );
drawTexturedQuad();
glPopMatrix();
Related
my issue is with opengl in java, I have it with the lwjgl. I am trying to load textures from a .png file and then draw them on to a screen. The problem I am facing is that when the texture coordinates are selected with glTexCoord2f they are off the texture in some cases. This manifests its self by leaving large black areas past the edge of the drawn texture. The only solution that I have found online is to add GL_CLAMP_TO_EDGE to the GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T parameters. Below is a picture of the textures on a green background with the black boarders. And then I have the code related to my use of opengl. Lastly if it matters I am using Eclipse on Linux. Thank you for any productive answers.
Example
textures on green background
Code: Initializing opengl
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, hight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Code: Drawing The Textures
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
background.bind();
glBegin(GL_QUADS); //Draw Background
glTexCoord2f(0, 0);
glVertex2i(25, 25);//Upper Left
glTexCoord2f(1, 0);
glVertex2i(900, 25);//Upper Right
glTexCoord2f(1, 1f);
glVertex2i(900, 600);//Lower Right
glTexCoord2f(0, 1f);
glVertex2i(25, 600);//Lower Left
glEnd();
statusBarWraper.bind();
glBegin(GL_QUADS); //Drawing the wrapper
glTexCoord2f(0, 0);
glVertex2i(500, 650);//Upper Left
glTexCoord2f(1, 0);
glVertex2i(1000, 650);//Upper Right
glTexCoord2f(1, 1);
glVertex2i(1000, 700);//Lower Right
glTexCoord2f(0, 1);
glVertex2i(500, 700);//Lower Left
glEnd();
statusBar.bind();
glBegin(GL_QUADS); //Drawing progress Bar
glTexCoord2f(0, 0);
glVertex2i(100, 650);//Upper Left
glTexCoord2f(1, 0);
glVertex2i(400, 650);//Upper Right
glTexCoord2f(1, 1);
glVertex2i(400, 675);//Lower Right
glTexCoord2f(0, 1);
glVertex2i(100, 675);//Lower Left
glEnd();
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
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.
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...
My Image is 1280x740 aswell as my game Canvas.
here is my code
// Initialize code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
// end initializiation
glClear(GL_COLOR_BUFFER_BIT);
getResources().backgroundImage.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0); // Upper left
glVertex2f(1280,0);
glTexCoord2f(1,0); // Upper Right
glVertex2f(0,0);
glTexCoord2f(1,1); // Bottom Right
glVertex2f(0,740);
glTexCoord2f(0,1); // Bottom Left
glVertex2f(1280,740);
glEnd();
Here is the problem I'm getting, while trying to make this a full-screen texture... the image is actually being scaled down?
Image showing
http://i48.tinypic.com/inrbeu.png
Vertex position is transformed to viewport coordinates by either fixed function pipeline or a vertex shader. Without specifying those, the outcome is probably not what you want.
You need to either setup a projection·modelview or a shader matching your needs.