Sign in to follow this  
rayan14

OpenGL I need help from all of you!

Recommended Posts

Hello my name is Ryan im 14 years old from Turkey, cheers everyone I started ogl programming a few months ago and i am going good but i have a very serious bug that i am stuck in for 2 months! I have a very simple OpenGL project that is not working right. After looking for info on the Internet, it turns out that the glRotatef() function does the transform local to the object, and does not do a global transform. So the order of operation of is important. In the function Cylinder::ProcessMessage(), processing for the MSG_ROTATE_CURRENT_STATE message, there is an if to check if the rotation is ‘positive’ or ‘negative’ it is just dumb luck that doing the rotations in the backward order works for this program, but to add functionality, I need a function for global rotation. There has to be a function to do global rotation!!! Run the scope project, and press the ‘go’ button. The window has two cylinders, but one of them starts with a view straight-on, so you must use the arrow keys on the keyboard to see one of the cylinders. Can you please help me? I have uploaded source code of the project here: www.albayoung.com/kosta/OGL.zip Please help me!!!!! thank you very much for your time. I owe a very big favour to all who help me :)

Share this post


Link to post
Share on other sites
take a breath there kid.
relax,
before trying coding more, since you have all your life ahead of you ( 14 years old ?? ) take some time and study some basic matrix mathematics and then some basic opengl matrix / matrix stack stuff. I guess you are familiar with some opengl concepts but obviously you must master some more before you can say you are stuck because of a bug.
At this point you are, most probably, doing something in a way that you shouldn't.
Pace yourself, study more opengl, before going deep into complex stuff.

you can start from these tutorials here ( http://nehe.gamedev.net )
they are not the best code practices but they have helped a lot of people ( including myself ) to start off.

in the meantime i will take a look of your code ...


Share this post


Link to post
Share on other sites
thank you very much skiritis. you cant imagine how much i appreciate it!

I know about nehe and i read tutorials almost every day:)

i am waiting very excited to see if you can help me so i can go past this bug

thanks alot!

Share this post


Link to post
Share on other sites
If by global rotation you mean rotate every object, there is a simple solution. You rotate your camera in the opposite way. Still that means you have to do your rotation before drawing your objects in your loop.

Try to understand how OpenGL works,
i)you set your viewpoint(Camera)
ii) Set your transformation for your object(translate/rotate)
iii) draw your object
At this point your object is rasterized, turned to pixels, you can no longer change this object for this frame. Only thing which could happen is another object closer to the camera is rasterized and it overwrites the pixels.

The only 2 ways you could rotate every object in the world are as follows
1) rotate your camera in the opposite direction.
2) apply your rotation calculations to every object in the world.

Obviously the former is easier/faster to do.

Share this post


Link to post
Share on other sites
Hello, thanks for the reply

Can you demonstrate me how i can do this in the code that i have provided? My OpenGL knowledge is not very stable and im trying my best to have a sample for the contest entry before 30th... i know im going to take last palce but i just want to do it for my pleasure

thanks alot :)

Share this post


Link to post
Share on other sites
Dont really have much time now, but in general, you could do this. Ive not touched Transfomations in a long time so i might be wrong, but i believe it should work.

float[4] globalRotation;

loop every frame
{
glLoadIdentity();
//do this here if you want objects to rotate around camera
//glRotatefv(globalRotation);
for every object
glPushMatrix();
glTranslatef(ObjectPosition)
//do this here if you want objects to around their own axis
//glRotatefv(globalRotation);
glRotatef(localObjectRotation);
//draw your object here
glPopMatrix();
end for

}

Share this post


Link to post
Share on other sites
Thank you very much!! can someone please also help me with the exact code that i have provided so that i can remove the if?

Thank you very much!

Share this post


Link to post
Share on other sites
First off, give us some time! You've been stuck on this for 2 months now, you say, so surely you could wait more than 2 hours before bumping your post (say, until it is actually push off the front page?).

Secondly, no one really wants to sort through your entire code base and find the function/section that you are talking about. Can you post that code here directly yourself? You can just copy-and-paste it into a post, and put it between [source] and [/source] tags. Try to give us as little as possible; one or two functions should be enough.

Share this post


Link to post
Share on other sites
Thank you for the reply very much. I am sorry for my bad behaviour.

I believe this section of the code needs to be fixed:

bool Cylinder::ProcessMessage(U32 msg, MsgParam param1, MsgParam param2)
{
bool bHandled = false;

switch (msg)
{
case MSG_TRANSLATE_ALONG_LENGTH:
glTranslatef(0.0f, 0.0f, param1.f * m_lfHeight);
bHandled = true;
break;

case MSG_GET_LENGTH:
*(param1.plf) = m_lfHeight;
bHandled = true;
break;

case MSG_ROTATE_CURRENT_STATE:
if (param1.f > 0)
{
glRotatef(m_rotx, param1.f, 0.0f, 0.0f);
glRotatef(m_roty, 0.0f, param1.f, 0.0f);
glRotatef(m_rotz, 0.0f, 0.0f, param1.f);
}
else
{
glRotatef(m_rotz, 0.0f, 0.0f, param1.f);
glRotatef(m_roty, 0.0f, param1.f, 0.0f);
glRotatef(m_rotx, param1.f, 0.0f, 0.0f);
}
bHandled = true;
break;

case MSG_INCR_LENGTH: m_lfHeight++; bHandled = true; break;
case MSG_ACCUM_LENGTH: m_lfHeight += param1.f; bHandled = true; break;
}

if (!bHandled)
bHandled = GLuObj::ProcessMessage(msg, param1, param2);

return bHandled;
}

i want to remove the if statement that checks if rotation is positive

Share this post


Link to post
Share on other sites
Quote:
Original post by skiritis
your archive ( OGL ) is broken


thank you very much Skirtis; it appears that u are the only one who bothered to check the source so far... :) i really appreciate it.

I fixed the problem, please try again :)

Share this post


Link to post
Share on other sites
Here is a simplified (pseudo code) version of my own drawing function in a flight sim that I am writing. Notice how there is a camera to define how all other objects will be rotated/translated.


int DrawGLScene(GLvoid)
{
glViewport(0, 0, xres, yres);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, x_res/y_res, 1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
camera_position[0],
camera_position[1],
camera_position[2],
camera_direction[0],
camera_direction[1],
camera_direction[2],
0, 1, 0);
glLightfv(GL_LIGHT1, GL_POSITION, your_light);
//Draw all objects here.
draw_world();
return TRUE;
}


Share this post


Link to post
Share on other sites
Hey kid [smile] skiritis asked me to help out, so here I am. I can't really understand what your problem is, your explanation is lacking in the question/information respect. However, there are a few things I can point out which may help.

The function ChangeSize, there is no indication as to when that function gets called (apart from the name implying some order), the issue here that it changes the OpenGL state and your rendering function is state sensitive (see below). One option would be to not register ChangeSize with GLUT and manually call it at the beginning of the RenderScene function.

The RenderScene function is state sensitive, that is, the resulting state of OpenGL of the next call to RenderScene is dependent on state just after the current RenderScene call, this is not necessarily a bad thing, but in your case, it looks accidental (because you seem to be tracking absolute rotations using the global variables xRot, yRot and the Visual object's local state variables etc.).

You can make RenderScene self-contained by calling glMatrixMode(GL_MODELVIEW) followed by glLoadIdentity() just before your first call to glPushMatrix in the RenderScene function. After you have done this, try removing the negative check and see if that is the behavior you require.

Hope this helps, I will take another look later tonight.

Share this post


Link to post
Share on other sites
Hello everybody & silvermance


Your suggestion did not work.

Look at www.albayoung.com/kosta/OGL2.zip . If you run it, you will see that as the one cylinder moves around, it almost never intersects the other cylinder. This is the wrong behavior. If you run the project from the original post (without your suggested changes, www.albayoung.com/kosta/OGL.zip ) the two cylinders always intersect. This is the correct behavior.

Let me know if you need more details

Once again, thank you all

Share this post


Link to post
Share on other sites
I would suggest bumping your thread less frequently as others have suggested, it is hurting your ability to get feedback.

I don’t have as new of a version of Visual Studio so I can’t compile your code. But regardless, slow down and explain a little clearer what your issue is.

By global rotation do you mean the ability to rotate an object around a point rather than rotating it about its own axis? Please explain clearly what you need to do (with an example), and I will try and help you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"Scope1" is the first project ( OGL.zip. "Scope2" is the second one (OGL2.zip). The desired behavior of the program is that the two cylinders are always touching - one cylinder stays in the same spot (but grows in length), and one cylinder moves around, but always intersects the other (stationary) cylinder.

1) Run Scope1
2) Press the "Go" button
3) Press the "up arrow" on the keyboard once (now you can see both cylinders)
4) Notice that the two cylinders always intersect

Run the steps above on Scope 2 and you will notice in step 4 that the cylinders don't always intersect.

Hope this helps, Please help me

Share this post


Link to post
Share on other sites
Bumping before reading?

As I said in my prior post I am NOT able to compile your code, and with the description you gave I really don’t know much, but here it goes. I really suggest you read what other people post if they are trying to help.

I am going to assume you just want the second cylinder to rotate around the first one.

Code:

DrawCylinder(); //The middle cylinder, drawn at 0,0,0
glPushMatrix();
glTranslatef(); //Translate the distance out the other one is growing minus the second cylinders radius
glRotatef(); //Rotate the cylinder around the center
DrawCylinder(); //The second cylinder, drawn at 0,0,0 (the rotate and transform will move it)
glPopMatrix(); //Done

If that is not what you need, well it is a shame you didn’t give me more details like I asked.

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  

  • Forum Statistics

    • Total Topics
      627714
    • Total Posts
      2978773
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now