# OpenGL I need help from all of you!

This topic is 4100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites

please! this bug is driving me crazy

##### 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 on other sites
thank you very much skiritis. you cant imagine how much i appreciate it!

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

thanks alot!

##### 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,
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 on other sites

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 on other sites
How can i change the code so i can make it that i dont need to check if the rotation is negative?

##### 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
{
//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);
glPopMatrix();
end for

}

##### 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 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 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 on other sites
your archive ( OGL ) is broken

##### Share on other sites
Quote:
 Original post by skiritisyour 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 on other sites
I think the solution to the real problem is Cylert (pemoline), Dexedrine (dextroamphetamine), or Ritalin (methylphenidate).

..

any help guys?

##### 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 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 on other sites
Hello everybody & silvermance

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 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 on other sites
im sorry but nobody wants to help me and the date is approaching :(

##### Share on other sites
"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.

##### Share on other sites

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.

• 9
• 10
• 12
• 10
• 10
• ### Similar Content

• Good Evening,
I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
I am really stucked right now because of the fundamental question:
Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on.
In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
Should I treat those debug objects as entities/components?
For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
Regards,
LifeArtist
• By QQemka
Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
Let's go:
Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
There were several more but i forgot/solved them at time of writing
• By RenanRR
Hi All,
I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
#version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);
So, some doubts:
- Why use it like that?
- Is it okay to manipulate the camera that way?
-in this way, are not the vertex's positions that changes instead of the camera?
- I need to pass MVP to all shaders of object in my scenes ?

What it seems, is that the camera stands still and the scenery that changes...
it's right?

Thank you

• Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

int rgbValue = int(textureSample.w);//4 bytes of data packed as color
// algorithm might not be correct and endianness might need switching.
vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
extractedData /= 255.0f;

• While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
Anyone has any idea .. what should I do?