• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# OpenGL OpenGL Rotational Problem

## Recommended Posts

OK, I have a simple piece of trial code, as below.


const float *Data = new float;

Data = Shape->getShapeData();

glColor3f(0.50f,0.125f,0.5f);

glRotatef( m_Rot, 1.0f, 0.0f, 0.0f );

glVertex3f(Data[0],Data[1],0.0f);
glVertex3f(Data[2],Data[3],0.0f);
glVertex3f(Data[4],Data[5],0.0f);
glVertex3f(Data[6],Data[7],0.0f);
glEnd();

glPopMatrix();

SwapBuffers(this->hDC);


All I want to do is to Draw my shape (it''s a long rectangle), and rotate it via a value (m_Rot). m_Rot is set elsewhere on a key press, and after debugging, I know that this is incrementing 90 degrees each key press. I am getting a strange behaviour in my rotation, in that sometimes the shape never moves, and sometimes it disappears, and sometimes it rotates through 90 or 180 degrees. I suspect it is the coordinates in my glRotate() function. As I am drawing at 100,100,2 I assumed that if I rotated from 0,0,2 I would rotate my shape 90 in the plane it is in, from the origin. Changing the coords to 100,100,2 should rotate it around one corner of the object. Am I correct in my assumptions for the rotation coordinates, and if so, why am I getting unexpected behaviour. Tia, BP.

##### Share on other sites
Looks like you''re rotating in the YZ plane instead of the XY plane. Try changing the arguments to the rotate function to 0,0,1.

How appropriate. You fight like a cow.

##### Share on other sites
Thats solved some of my problem, but I still can''t see my rotated shape all the time.

Any more ideas ?

Thanks so far,

Bp

##### Share on other sites
glTranslatef(100.0f,100.0f,2.0f);

WTF? In plain english, this would be "Move the object behind me and way the f*ck up and to the right." Why are you translating to this location?

const float *Data = new float;
Data = Shape->getShapeData();

I don''t even want to know what you were trying to do.

glVertex3f(Data[0],Data[1],0.0f);
glVertex3f(Data[2],Data[3],0.0f);
glVertex3f(Data[4],Data[5],0.0f);
glVertex3f(Data[6],Data[7],0.0f);

Do you know what buffer over-run is?

glPopMatrix();
Are you calling glPushMatrix() before this?

SwapBuffers(this->hDC);
Can''t you just say "SwapBuffers(hDC);"?

How long have you been programming?

##### Share on other sites
Jonski.
1) My Coordinate system is converted to my screen res (1024 * 768), and the simple shape appears fine, so I guess whilst you may look over your shoulder, I can see the shape. I only posted what was relevant, sorry if you are not able to understand that.

2)Data->shape.getData() - maybe irrelevant, but my shape returns a pointer to an array of floats.

3)No Buffer overrun, as this is me playing, I know exactly how big my shape is.

4) Yes, Sorry, it looks like my cut & paste went awry.

5) Yes I could. I started off with a program I found on the net, which was a mix of a class and global var''s, and in the process of making a class I found an identical variable was giving me a problem. It doesn''t really matter whether I user this-> or just teh function. I am so sorry for not cleaning up my personal code to a production standard before releasing it. I am trying to learn, If I wanted to be shot down in flames I am sure I could post a better question than this one. If you can''t help someone, why the f* are you posting here ?

I know that my code is not too efficient, or maybe even the best methodm but I am playing around, trying to learn something. Try not to criticise something you don''t understand. If you can''t say something nice, then at least keep it shut.

##### Share on other sites
quote:
Original post by Bagpuss
2)Data->shape.getData() - maybe irrelevant, but my shape returns a pointer to an array of floats.

which was quite obvious from the context.. i think he just wanted to demonstrate that the smartass business doesnt work if you''re lacking in the smart department.

anyway: gltranslate probably doesnt do what you want. either you call it before you rotate, then the object will still rotate around its local origin (its position) or you rotate before you translate, but then the translation will be along its local axes and it wont appear where you might expect it. example: rotate 90 deg to the left and translate 100 along (0,0,1 -pretending inverted z-). it will now appear at (world coords) (-100,0,0)

##### Share on other sites
quote:
Original post by Trienco
i think he just wanted to demonstrate that the smartass business doesnt work if you''re lacking in the smart department.

I deserved that. I shouldn''t have been condescending. Anyways...

In order for
glTranslatef(100.0f,100.0f,2.0f);
to allow you to show anything you would first need either:
a) a z-inverted (aka left-handed coordinate system) projection matrix
b) pre-translated geometry
c) a custom vertex program
d) you object is large enough to poke through near clipping plane
Otherwise, the z-axis points out of your screen. Just for debugging purposes, see if you can see your object by translating (0, 0, -z), where ''z'' is any value between your near and far clipping planes.

const float *Data = new float;
Data = Shape->getShapeData();

I''m pretty sure this leaks 4 bytes. If it''s called in a loop, then it will leak 4 bytes every iteration. I don''t think you also don''t need the ''const'' keyword, either. I think you should simply say:
float *Data = Shape->getShapeData();

I also want to re-iterate what Trienco said, but in different words about translate/rotation order.
Think about a basketball. If you translate then rotate, it''s like spinning the ball on you finger over the point where you translated.
If you rotate and then translate, it''s like holding the ball out where you translated and then turning your whole body in circles.

##### Share on other sites
OK, thanks for the replys guys,
and sorry if I was a bit touchy. Been a bad weekend for me :-/

I will try your suggestions, and if none of them work then no doubt I will be back !

Thanks again,

Bp.

##### Share on other sites
hope it will help.. the basketball explanation is pretty good and definitely better than me babbling about local and global axes (i couldnt explain things like that if my life depended on it.. at least not in a way somebody can understand if he''s not already knowing about it ,-) )

if you still have strange behaviour when rotating try and rotate and very small steps so you can watch where it goes.

• ### Forum Statistics

• Total Topics
627735
• Total Posts
2978848
• ### Similar Content

• 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!

• 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

• 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?

• 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.

• 10
• 10
• 21
• 14
• 12