Unable to rotate 2D object properly with opengl - c++

I'm using opengl, glfw and glew. I want to rotate the tip of the spaceship without moving the whole spaceship when users press on the left and right arrow respectively. I've tried using gltransform glrotate, it does rotate but it also move my spaceship along. The output of the movement of spaces
objects objects::rotateSpaceship(GLFWwindow * window, objects & sp)
{
GLfloat spaceship_angle = sp.getAngle();
GLfloat newAngle = sp.getAngle() + 0.5;
sp.setAngle(newAngle);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
GLfloat spaceShipRightLine[] =
{
x, y + 80, 0,
x - 50, y - 50, 0
};
GLfloat spaceShipLeftLine[] = {
x, y + 80, 0,
x + 50, y - 50, 0
};
GLfloat spaceShipMiddleLine[] = {
sp.x - 30 ,sp.y,0,
sp.x + 35,sp.y,0
};
glPushMatrix();
glTranslatef(0.0, 0.01, 0.0); // 3. Translate to the object's position.
glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
glTranslatef(0.0, 0.01, 0.0); // 1. Translate to the origin.
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
glfwTerminate();
return sp;
}

Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades.
Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.
I want to rotate the tip of the spaceship without moving the whole spaceship [...]
First you have to define the pivot, which is the "tip" of the spaceship:
float pivot_x = sp.x;
float pivot_y = sp.y + 80.0f;
To rotate around a pivot you have to define a model matrix, which displaces by the inverted pivot, then rotate (glRotatef) and final transforms back by to the pivot (glTranslate):
glTranslatef(pivot_x, pivot_y, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-pivot_x, -pivot_y, 0);
The final code may look like that:
float pivot_x = x;
float pivot_y = y + 80.0f;
glPushMatrix();
glTranslatef(pivot_x, pivot_y, 0 ); // 3. Translate to the object's position.
glRotatef(sp.getAngle(), 0.0, 0.0, 1.0); // 2. Rotate the object.
glTranslatef(-pivot_x, -pivot_y, 0.0); // 1. Translate to the origin.
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipRightLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipLeftLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, spaceShipMiddleLine);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();

Related

How to fill the deph buffer in OpenGL 1 with a depth texture?

I separate moveable 3d object and non moveable 3d. I can generate 2 textures for non moveables : background color texture and background depth texture . I have no problem. The values are ok for both. I check the value with a gray gradient texture for the depth.
And I would like to write / fill the color buffer and the depth buffer in opengl with those texture but the depth doesn't work (the color buffer is fine on the screen).
My goal is to never recompute the non movable 3d objects when the camera doesn't move.
(rem : The depth buffer bits size is always in 16 bits)
This is my code for generate the depth texture :
U16 m_backgroundDepthBuffer[4096 * 4096]; // it's a member of my class
glReadPixels(0,0, width(), height(), GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, (void*)m_backgroundDepthBuffer);
// Allocate GPU-memory for the depth-texture.
glDeleteTextures(1, &m_backgroundDepthTextureId);
glGenTextures(1, &m_backgroundDepthTextureId);
glBindTexture(GL_TEXTURE_2D, m_backgroundDepthTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16,
width(), height(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, m_backgroundDepthBuffer);
This is my code for filling the color buffer :
void Application::Render()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
// render background
Render::RenderImage(m_backgroundTextureId, 0,0, width(), height());
// for debug
// Render::RenderImage(m_backgroundDepthGrayTextureId, 0,0, width(), height());
// This is how I fill the depth buffer : it's work but slow
glDrawPixels(width(), height(), GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, (GLvoid*) m_backgroundDepthBuffer); //
// doesn't work
// Render::RenderDepth(m_backgroundDepthTextureId, 0,0, width(), height());
}
This is my Render::RenderImage() : it works
void Render::RenderImage(U32 tex, int x, int y, int w, int h, float anchorX, float anchorY)
{
glClear(GL_DEPTH_BUFFER_BIT);
GLboolean depth = glIsEnabled(GL_DEPTH_TEST);
if (depth)
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, s_width, s_height, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3ub(255, 255, 255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
x -= (int)(anchorX * w);
y -= (int)(anchorY * h);
gVertices[0] = VECTOR3F(x, y, 0);
gVertices[1] = VECTOR3F(x + w - 1, y, 0);
gVertices[2] = VECTOR3F(x + w - 1, y + h - 1, 0);
gVertices[3] = VECTOR3F(x, y + h - 1, 0);
gTexCoords[0] = VECTOR2F(0, 1);
gTexCoords[1] = VECTOR2F(1, 1);
gTexCoords[2] = VECTOR2F(1, 0);
gTexCoords[3] = VECTOR2F(0, 0);
gIndexes[0] = 2;
gIndexes[1] = 1;
gIndexes[2] = 0;
gIndexes[3] = 0;
gIndexes[4] = 3;
gIndexes[5] = 2;
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glTexCoordPointer(2, GL_FLOAT, 0, gTexCoords);
glDrawElements(GL_TRIANGLES, 3 * 2, GL_UNSIGNED_SHORT, gIndexes);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
if (depth)
glEnable(GL_DEPTH_TEST);
}
This is my Render::RenderDepth() : it doesn't work / I have a texture with depth = 0.0f instead to use the depth of each texel.
I want only to change the depth buffer and do nothing to the color buffer
Render::RenderDepth() is similar to Render::RenderImage() : I render 2 triangles
void Render::RenderDepth(U32 tex, int x, int y, int w, int h, float anchorX, float anchorY)
{
glClear(GL_DEPTH_BUFFER_BIT);
/*glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_DEPTH_TO_TEXTURE_EXT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_ALWAYS);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_DEPTH_TEXTURE_MODE, GL_ALPHA);
*/
GLboolean depth = glIsEnabled(GL_DEPTH_TEST);
//if (depth)
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, s_width, s_height, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3ub(255, 255, 255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
x -= (int)(anchorX * w);
y -= (int)(anchorY * h);
gVertices[0] = VECTOR3F(x, y, 0.0);
gVertices[1] = VECTOR3F(x + w - 1, y, 0.0);
gVertices[2] = VECTOR3F(x + w - 1, y + h - 1, 0.0);
gVertices[3] = VECTOR3F(x, y + h - 1, 0.0);
gTexCoords[0] = VECTOR2F(0, 1);
gTexCoords[1] = VECTOR2F(1, 1);
gTexCoords[2] = VECTOR2F(1, 0);
gTexCoords[3] = VECTOR2F(0, 0);
gIndexes[0] = 2;
gIndexes[1] = 1;
gIndexes[2] = 0;
gIndexes[3] = 0;
gIndexes[4] = 3;
gIndexes[5] = 2;
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glTexCoordPointer(2, GL_FLOAT, 0, gTexCoords);
glDrawElements(GL_TRIANGLES, 3 * 2, GL_UNSIGNED_SHORT, gIndexes);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}

My image keeps disappearing when I add Opengl transformations

My image disappears if I add Opengl transformations.
void renderFileButton()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glViewport(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 320, 0, 240, -1, 1);
glMatrixMode(GL_MODELVIEW);
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(file_vertices), file_vertices,
GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(fileButtonVertexData), (GLvoid *)
offsetof(fileButtonVertexData, fileButtonPositionCoordinates));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(fileButtonVertexData), (GLvoid *)
offsetof(fileButtonVertexData, fileButtonTextureCoordinates));
loadsprite("files/sprites/options.png");
glScalef(0, 0, 1);
glRotatef(180, 1, 0, 0);
glTranslatef(0, 0, 0);
glPopMatrix();
}
Could someone tell me where I'm in the wrong here?
You've scaled by zero. That makes the object's size zero. No surprise that it disappears.
In the future, you can troubleshoot by adding one transformation at a time, and trying to add a transformation that doesn't do anything (scale by 1, rotate by 0, translate by 0). Then increase the transformation slowly until you get to your intended value.
Also, your glPopMatrix is undoing all your transformations.
Finally, I don't see any actual rendering. Just a lot of state manipulation.
I found the problem after playing with it all last night. The transformation order was the issue.
glTranslatef(100, 100, 0);
glRotatef(180.0f,0.0f,0.0f,1.0f);
glScalef(-1.0f,1.0f,1.0f);
This does the job, that is all.

Opengl weird viewing

As you can see from the images below, I have two triangles side by side (viewed from front). But when I view it from the right, the yellow triangle shows as if it is in front of the green triangle where it should not be visible because it is behind the green one (see right view), why is this happening? Viewing it from the left is fine (see left view). Thanks.
FrontView:
Right View (why is the yellow triangle is still visible?):
Left view (viewing from here is fine):
This is my render method. Even though I don't use glColor4f and do the coloring in a modern way, I still have the same result. I have also set glEnable(GL_DEPTH); and
glDepthFunc(GL_LESS);
void display(){
glClear(GL_COLOR_BUFFER_BIT);
control(0.2, 0.2, mi);
view = updateCam();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
model = glm::rotate(glm::mat4(1.0), 45, glm::vec3(0, 1, 0));
glColor4f(0, 1, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 3);
resetMatrix();
model = glm::translate(glm::mat4(1.0), glm::vec3(5, 0, 0));
model = glm::rotate(model, 45, glm::vec3(0, 1, 0));
glColor4f(1, 1, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
resetMatrix();
SDL_GL_SwapBuffers();
}
You're only clearing your color buffer. The depth buffer still contains the depth values from previous frames, so your Z-test doesn't work as intended.
Clear them both:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Rotate Texture2d using rotation matrix

I want to rotate 2d texture in cocos2d-x(opengl es) as i searched i should use rotation matrix
as this :
(x = cos(deg) * x - sin(deg) * y y = sin(deg) * x + cos(deg) * y)
but when i want to implement this formula i fail may code is like(i want original x and y to be same ) :
i updated my code to this but still not working!
GLfloat coordinates[] = {
0.0f, text->getMaxS(),
text->getMaxS(),text->getMaxT(),
0.0f, 0.0f,
text->getMaxS(),0.0f };
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
GLfloat vertices[] = { rect.origin.x, rect.origin.y, 1.0f,
rect.origin.x + rect.size.width, rect.origin.y, 1.0f,
rect.origin.x, rect.origin.y + rect.size.height, 1.0f,
rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, 1.0f };
glMultMatrixf(vertices);
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glColor4f( 0, 0, 250, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glRotatef(-a, 0.0f, 0.0f, -1.0f);
glPopMatrix();
If you're using OpenGL in two dimensions, you can still use the various transformation functions. For glRotate(), you have to pass it an axis of (0,0,1):
glRotate(angle,0,0,1);
You seem to be confused about a few aspects of OpenGL. At the minute you have this:
glMultMatrixf(vertices);
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glColor4f( 0, 0, 250, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glRotatef(-a, 0.0f, 0.0f, -1.0f);
glPopMatrix();
That would ostensibly:
// reinterpret your vertices as a matrix; multiply the
// current matrix by the one formed from your vertices
glMultMatrixf(vertices);
// rotate by a degrees, then translate to p1
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
// bind the relevant texture, supply the vertices
// as vertices this time, supply texture coordinates
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// set a colour of (0, 0, 1, 1) — probably you
// wanted glColor4ub to set a colour of (0, 0, 250/255, 1)?
glColor4f( 0, 0, 250, 1);
// draw some geometry
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// perform the same rotation as before a second time;
// giving either a and -1, or -a and 1 would have been
// the opposite rotation
glRotatef(-a, 0.0f, 0.0f, -1.0f);
// remove the current matrix from the stack
glPopMatrix();
What you probably want is:
// push the current matrix, so that the following
// transformations can be undone with a pop
glPushMatrix();
// rotate by a degrees, then translate to p1
glTranslatef(p1.x, p1.y, 0.0f);
glRotatef(a,0,0,1);
// bind the relevant texture, supply the vertices
// as vertices this time, supply texture coordinates
glBindTexture(GL_TEXTURE_2D, text->getName());
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// set a colour of (0, 0, 250/255, 1)
glColor4ub( 0, 0, 250, 1);
// draw some geometry
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// remove the current matrix from the stack
glPopMatrix();
The current transform matrices are applied to all geometry. You don't flag up which geometry they apply to and which they don't. Pushes and pops should be paired, and are the way you affect transformations from being affected by prior transformations when that's what you want. So you don't manually perform the inverse rotation, and you need to push before you do all the stuff that changes the matrix. I've also switched you to glColor4ub for the reason given above — you appear to be using unsigned bytes to push colours in but OpenGL uses the range from 0.0 to 1.0 to represent a colour. glColor4ub will automatically map from the former to the latter.

Rotate Triangle In OpenGL

I'm trying to rotate a triangle around it's center point. I'm aware that OpenGL rotates about the origin so I need to translate the middle point to the origin, then rotate, and translate back. I've commented out this last line to ensure that it at least rotates about its center at the origin. It does not. It appears to be rotating about its old origin despite the translation... Note that ccc4 and ccp generate floats. Here's my code:
ccColor4B colors[] = {
ccc4(255, 0, 0, 255),
ccc4(0, 255, 0, 255),
ccc4(0, 0, 255, 255)
};
CGPoint vertices[] = {
ccp(0,0),
ccp(50,100),
ccp(100,0),
};
CGPoint middle[] = {ccp(50,50)};
CGPoint origin[] = {ccp(0,0)};
// Rotate the triangle
glPushMatrix();
glTranslatef(-50, -50, 0);
glRotatef(45, 0, 0, 1.0);
// glTranslatef(50, 50, 0);
// Draw the triangle
glLineWidth(2);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glColor4ub(0, 0, 255, 255);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
// Revert rotation, we only want triangle to rotate
glPopMatrix();
// Draw the points
glDisableClientState(GL_COLOR_ARRAY);
glPointSize(5);
glColor4ub(255, 255, 255, 255);
glVertexPointer(2, GL_FLOAT, 0, middle);
glDrawArrays(GL_POINTS, 0, 1);
glPointSize(5);
glColor4ub(0, 255, 0, 255);
glVertexPointer(2, GL_FLOAT, 0, origin);
glDrawArrays(GL_POINTS, 0, 1);
glEnableClientState(GL_COLOR_ARRAY);
// End points
Here's the output:
You need to think of transforms as applying in reverse relative to the order in which you call them.
Actually, it's easier to think in terms of transforming the local coordinate system (LCS), not the object, which allows you to mentally apply transforms in the order they're called. To rotate about the center, translate the LCS to the center, rotate, then translate it back out again:
glTranslatef(50, 50, 0);
glRotatef(45, 0, 0, 1);
glTranslatef(-50, -50, 0);