Sign in to follow this  
Jevi

OpenGL Quick help needed with 2d rotation of vertex buffer object OpenGL

Recommended Posts

Jevi    131
I have a Ship object made up of 4 lines ( looks like the asteroids game ship ). I am rendering it with the vertex buffer object method. It gets drawn to the screen fine but when i go to rotate the ship the whole ship rotates around the (x,y) coordinate and not about the (x,y) coordinate, if that makes sense.

CODE SNIPPETS - full code at: https://github.com/Jevi/Asteroids/tree/master/src/org/cstutorials


This is how i render the ship object ( which is compromised of 4 lines loaded into a FloatBuffer ( im using LWJGL btw ) that FloatBuffer is loaded in to memory and I draw from that allocated memory. The random GL_POINT is to see where (x,y) currently is.

[CODE]
@Override
public void render(Graphics g)
{
glLoadIdentity();
glPushMatrix();
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
g.rotate(x, y, angle);
g.drawLines();
glPopMatrix();
}
[/CODE]

This is my drawLines() implementation, the variables seen inside are handled by a gameObjectManager which is not in these snippets ( no need )

[CODE]
public void drawLines()
{
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, currentVertexHandle);
glVertexPointer(numCoordinates, GL_FLOAT, 0, 0L);
glDrawArrays(GL_LINES, 0, numPairVertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
}
[/CODE]

Finally here is my rotatation implementation, it use to work fine when I used immediate mode rendering but now that I have VBOs it does not work the same

[CODE]
public void rotate(float x, float y, float angle)
{
glTranslatef(x, y, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-x, -y, 0);
}
[/CODE]

Share this post


Link to post
Share on other sites
Jevi    131
[quote name='Solias' timestamp='1351209348' post='4993967']
Why are you doing the -x, -y translation? I assume the verts in the VBO are in object space already?
[/quote]

Because this is the method I used when I was using intermediate mode rendering. If I remove -x, -y translation the rotation error becomes even greater ( or less accurate ).

This is what I do to create the lines and store them to memory

[CODE]
Line[] lines = new Line[] { new Line(x, y - height / 4, x + width / 3, y + height / 4),
new Line(x + width / 3, y + height / 4, x, y), new Line(x, y, x - width / 3, y + height / 4),
new Line(x - width / 3, y + height / 4, x, y - height / 4) };
vertexHandle = glGenBuffers();
vertexBuffer = BufferUtils.createFloatBuffer(lines.length * Line.numIndividualVertices);
numCoordinates = Line.numCoordinates;
numPairVertices = lines.length * numCoordinates;
for (Line line : lines)
{
vertexBuffer.put(new float[] { line.getX1(), line.getY1(), line.getX2(), line.getY2() });
}
vertexBuffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, vertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
[/CODE]

Share this post


Link to post
Share on other sites
Jevi    131
[quote name='NumberXaero' timestamp='1351210725' post='4993972']
What happens if you try

glRotatef(angle, 0, 0, 1);
glTranslatef(x, y, 0);
[/quote]

That doesn't work either... I feel like if someone could see what was happening they could figure it out so he is a video of what is happening.. notice the dot witch is the x,y coordinate and then notice the ship

https://www.dropbox.com/s/52mq14gj8b80mb4/Untitled.mp4

Share this post


Link to post
Share on other sites
Kaptein    2224
to do rotations, you have to translate the object to its rotational origin, called a PIVOT position, then rotate it there, and translate it back!
if your object is a box created at (0,0) and all the way to (1,1), you have to translate it to (-0.5, -0.5) to rotate it around itself
however, if your box was created from (-0.5, -0.5) to (0.5, 0.5) you can rotate it immediately, because its rotational origin is now (0, 0) from the start, if you wanted to rotate it around itself!

3ds max (or any other 3d CAD) is a great tool to visualize how this works

to do the same thing with your model, you need to know its dimensions, and then its half-sizes, then translate it backwards so that the object is positioned with (0, 0) at the center of the object, then rotate it there (if that's what you wanted to do)

from looking at the ship creation code from the link.. its hard for me to understand how exactly it is created, except to say i never would be doing it that way myself :S
i'm sure theres some logic to it though! Edited by Kaptein

Share this post


Link to post
Share on other sites
Jevi    131
[quote name='Kaptein' timestamp='1351214814' post='4993997']
to do rotations, you have to translate the object to its rotational origin, called a PIVOT position, then rotate it there, and translate it back!
if your object is a box created at (0,0) and all the way to (1,1), you have to translate it to (-0.5, -0.5) to rotate it around itself
however, if your box was created from (-0.5, -0.5) to (0.5, 0.5) you can rotate it immediately, because its rotational origin is now (0, 0) from the start, if you wanted to rotate it around itself!

3ds max (or any other 3d CAD) is a great tool to visualize how this works

to do the same thing with your model, you need to know its dimensions, and then its half-sizes, then translate it backwards so that the object is positioned with (0, 0) at the center of the object, then rotate it there (if that's what you wanted to do)

from looking at the ship creation code from the link.. its hard for me to understand how exactly it is created, except to say i never would be doing it that way myself :S
i'm sure theres some logic to it though!
[/quote]

Ok so now the rotation works correctly. I translate back to the starting x,y rotate it there but how do i move the ship to where the current x,y are?

And yes my code may be a little confusing but its hard to stay true to object oriented programming when all the OGL tutorials I have found don't seem to code with that convention... [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

This is my modified code

[CODE]
@Override
public void render(Graphics g)
{
glLoadIdentity();
glPushMatrix();
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glTranslatef(startx, starty, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-startx, -starty, 0);
g.drawLines();
glPopMatrix();
}
[/CODE] Edited by Jevi

Share this post


Link to post
Share on other sites
Jevi    131
[quote name='Kaptein' timestamp='1351216682' post='4994004']
change glTranslatef(-startx, -starty, 0);
to:
glTranslatef(new_x - startx, new_y - starty, 0);
[/quote]

This does not work

[CODE]
@Override
public void render(Graphics g)
{
glLoadIdentity();
glPushMatrix();
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glTranslatef(startx, starty, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(x - startx, y - starty, 0);
g.drawLines();
glPopMatrix();
}
[/CODE]

Share this post


Link to post
Share on other sites
Jevi    131
I got it I forgot the all this is done slightly backwards so here is the working code if anyone wants to know

So I translate to the startx, starty then i rotate around it, I translate to -startx, -starty ( all that is done is g.rotate(..) ) then I translate to the current x,y coordinates of the object. As you can see in the following code those steps are implemented backwards in the code but are interpreted in the correct order by OGL. a little confusing but its not that bad [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]. Thanks for all the help guys!!


[CODE]
@Override
public void render(Graphics g)
{
glLoadIdentity();
glPushMatrix();
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glTranslatef(x - startx, y - starty, 0);
g.rotate(startx, starty, angle);
g.drawLines();
glPopMatrix();
}
[/CODE] Edited by Jevi

Share this post


Link to post
Share on other sites
Kaptein    2224
that's good to hear
sorry i got it backwards, yes the order of operations is indeed backwards [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
this is how matrices work in general
once you get a bit further into all this, and you are writing your own little matrix library (it's an important step to take)
then you will see why ;) not to mention the possibilities Edited by Kaptein

Share this post


Link to post
Share on other sites
Jevi    131
[quote name='Kaptein' timestamp='1351217940' post='4994012']
that's good to hear
sorry i got it backwards, yes the order of operations is indeed backwards [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
this is how matrices work in general
once you get a bit further into all this, and you are writing your own little matrix library (it's an important step to take)
then you will see why ;) not to mention the possibilities
[/quote]

thanks again! getting about 1000 fps more with VBOs as compared to intermediate rendering even though its just a couple of lines on the screen I can already see how this is going to be crucial later on [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now