Sign in to follow this  
Jengu

OpenGL Rotation makes stuff disappear?

Recommended Posts

Jengu    122
I'm new to OpenGL and I'm trying to figure out how to rotate one object without rotating the whole scene. From looking at MSDN I figured I should be doing something like this:
glPushMatrix();
glRotatef(20f, 0.0f, 1.0f, 0.0f);

glBegin(GL_LINES);
    //Draw my object
glEnd();

glPopMatrix();
Then that way only what I draw in the glBegin block would be rotated. But when I do this my object doesn't render at all! It's just a square with a textured on it if that makes a difference. What am I doing wrong?

Share this post


Link to post
Share on other sites
ciroknight    328
glBegin(GL_LINES); ?

If you want to draw poly's, you might want to use "GL_QUADS" or something other than lines and points. Also, if you're drawing in 3D, remember to use normals. I'm pretty new to OpenGL myself, but that's all I can tell that you might be mistaken on.

[edit] also, why are you using matricies? I'm not sure of the rest of the code you've got going on there, but I'd suggest looking at any of the tutorials, especially the NeHe tutorials on the site; they're pretty good for the simple stuff we're learning.

Share this post


Link to post
Share on other sites
Jengu    122
Quote:
Original post by ciroknight
glBegin(GL_LINES); ?

If you want to draw poly's, you might want to use "GL_QUADS" or something other than lines and points. Also, if you're drawing in 3D, remember to use normals. I'm pretty new to OpenGL myself, but that's all I can tell that you might be mistaken on.


Except, I'm actually drawing lines :)

Quote:
also, why are you using matricies? I'm not sure of the rest of the code you've got going on there, but I'd suggest looking at any of the tutorials, especially the NeHe tutorials on the site; they're pretty good for the simple stuff we're learning.


Um, it's OpenGL, how can you avoid using matrices? ;)

If you mean the push/pop stuff, that's what MSDN made it sound like I should do if I only want to rotate one object.

NeHe's tutorials as far as I can tell don't really address my question. He gets rotation working by reloading the identity and moving to a new point. Since all my drawing takes place from the same position, I'm not sure that makes sense for me. Meh, I'll fiddle with the code tomorrow, resetting the identity may fix it.

Share this post


Link to post
Share on other sites
dimebolt    440
Quote:
Original post by Jengu
If you mean the push/pop stuff, that's what MSDN made it sound like I should do if I only want to rotate one object.


You seem to use push and pop correctly. They make sure that the rotation is not applied to anything drawn after the pop statement.

Quote:
Original post by Jengu
It's just a square with a textured on it if that makes a difference.

Except, I'm actually drawing lines :)

These two statements are a bit confusing. Do you really want only four lines with texture on them? Are you using a 1D texture?

Anyway, the code you have given is not enough to answer your question. Are you sure that the missing quad is caused by the rotation (i.e. is the quad visible if you comment the glRotate(...) line?)?

Tom

Share this post


Link to post
Share on other sites
RuneSword2    122
if you draw a poligon where every x or every z are in the same plane. when rotate it about y axis it would disssapear the solution is to use volumetric poligons where you must draw the poligon and add volume to it like (cones,spheres....) or rotate the camera on the same angle as the object.

All depends what you really want.

Share this post


Link to post
Share on other sites
Aeluned    196
Are you applying that transformation to the ModelView matrix?
perhaps the current matrix on the stack is the projection from your setup code?

before that glPushMatrix() try a call to glMatrixMode(GL_MODELVIEW).

a rotation of 20 degrees shouldn't cause the object to vanish (if backface culling is enabled) due to culling (assuming that you draw the polygon with 0 rotation to begin with - i.e. it's screen aligned).

as a poster above pointed out, your use of lines is weird.

try a GL_TRIANGLES and pass 3 vertices.

a larger code snippet would help.

Share this post


Link to post
Share on other sites
Kalidor    1087
Quote:
Original post by Jengu

glPushMatrix();
glRotatef(20f, 0.0f, 1.0f, 0.0f);

glBegin(GL_LINES);
//Draw my object
glEnd();

glPopMatrix();
Are you drawing your object around the origin? All rotations occur about the current origin, so if you're not rendering the object some distance away from the origin it could easily be rotating off the screen.

If you haven't already, I suggest implementing some simple camera controls so you can look around.

Share this post


Link to post
Share on other sites
Jengu    122
Doh, forgot about the texture. See, my app does actually use GL_LINES at one point. But in this case the object that's disappearing is actually a GL_QUAD. Managed to confuse myself. Sorry :P

Commenting out the glRotate line does make the object reappear.

I used glOrtho2D so that my windowing systems coordinates and my OpenGL coordinates match up because I'm doing a lot of 2D drawing. So the origin (0,0) is the top left of the screen. The object I'm trying to rotate is at the center of the screen so it's at some positive x and positive y value. I think this is what is somehow screwing it up.

Putting glMatrixMode(GL_MODELVIEW) before the push doesn't do the trick either. :/

Here's how I setup the gl view:


void PageCanvas::OnSize(wxSizeEvent& event)
{
// Now resetup OpenGL coordinates
int width, height;
GetClientSize(&width, &height);

if(GetContext())
{
SetCurrent();
glViewport(0, 0, (GLint)width, (GLint)height);
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0, (GLdouble) width, (GLdouble) height, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

Share this post


Link to post
Share on other sites
Morpheus011    309
well if none of these other answers get it working for you then the most likely problem is that you are drawing in a clockwise order and are culling backfaces, meaning you are culling what are you drawing. if none of this other stuff works, that may be your problem.

Share this post


Link to post
Share on other sites
Aeluned    196
It seems you've mixed up your parameters to gluOrtho2D.

the third parameter is the bottom (which should be 0) and the forth parameter is the height.

This is probably causing your polygons to be drawn as if there's a scaling of -1 along the y axis - effectively reversing your winding.

if you have GL_CULL_FACE enabled then you're culling your polygons.

explicitly disable face culling with a glDisable(GL_CULL_FACE) and fix your gluOrtho call.

Share this post


Link to post
Share on other sites
Kalidor    1087
Quote:
Original post by Jengu
glRotatef(20f, 0.0f, 1.0f, 0.0f);
Rotating around the y-axis will probably move the object outside the frustum (gluOrtho2D sets the near and far clip plane distances to -1 and 1 respectively) unless its x coordinate is close to 0. Even if it is close to 0, a large enough rotation angle will still make it disappear.

With that said, since it sounds like you are doing 2D stuff, you probably want to be rotating around the z-axis instead. And remember, to rotate around the center of the object you have to translate the origin to the object's center, rotate, than translate back. Otherwise it will rotate around the origin, which is the bottom-left or top-left of your screen, depending on how you set up your orthographic projection.

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 Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • 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
  • Popular Now