reverse lines in OpenGl
-
Hi, i am new to OpenGL and therefore need your help. When i draw a simple line using MFC it works fine... my pixels are ret correctly.. when i am trying to redraw that automatically using OpenGL they are being drawn in reverse...so instead of let say up to bottom... it is drawing it bottom to top. please let me know what changes need to be done withing Vertex...i tried it all.. i don't see how to fix this.
int dx = (x2 - x1); int dy = (y2 - y1); int temp; float k; glBegin(GL_LINES); // Set start pixel ::SetPixel(m_hDC, x1, y1, color); glVertex2f(x1,y1); // X-dominant line if (abs(dx) > abs(dy)) { // Ex-change line end points if (dx < 0) { temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 = temp; } k = (float)dy / (float)dx; // Set middle pixels int xs; float yt = (float)y1 + k; float distance; for (xs=x1+1; xs<x2; xs++) { distance = (float)(yt - (int)(yt)); ::SetPixel(m_hDC, xs, (int)yt, color); glVertex2f(xs,(int)yt); ::SetPixel(m_hDC, xs, (int)yt+1, color); glVertex2f(xs,(int)yt+1); yt += k; } } // Y-dominant line else { // Ex-change line end points if (dy < 0) { temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 = temp; } k = (float)dx / (float)dy; // Set middle pixels int ys; float xt = (float)x1 + k; float distance; for (ys=y1+1; ys<y2; ys++) { distance = (float)(xt - (int)(xt)); ::SetPixel(m_hDC, (int)xt, ys, color); glVertex2f((int)xt,ys); ::SetPixel(m_hDC, (int)xt+1, ys, color); glVertex2f((int)xt+1,ys); xt += k; } } // Set end pixel ::SetPixel(m_hDC, x2, y2, color); glVertex2f(x2,y2); glEnd();
-
Hi, i am new to OpenGL and therefore need your help. When i draw a simple line using MFC it works fine... my pixels are ret correctly.. when i am trying to redraw that automatically using OpenGL they are being drawn in reverse...so instead of let say up to bottom... it is drawing it bottom to top. please let me know what changes need to be done withing Vertex...i tried it all.. i don't see how to fix this.
int dx = (x2 - x1); int dy = (y2 - y1); int temp; float k; glBegin(GL_LINES); // Set start pixel ::SetPixel(m_hDC, x1, y1, color); glVertex2f(x1,y1); // X-dominant line if (abs(dx) > abs(dy)) { // Ex-change line end points if (dx < 0) { temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 = temp; } k = (float)dy / (float)dx; // Set middle pixels int xs; float yt = (float)y1 + k; float distance; for (xs=x1+1; xs<x2; xs++) { distance = (float)(yt - (int)(yt)); ::SetPixel(m_hDC, xs, (int)yt, color); glVertex2f(xs,(int)yt); ::SetPixel(m_hDC, xs, (int)yt+1, color); glVertex2f(xs,(int)yt+1); yt += k; } } // Y-dominant line else { // Ex-change line end points if (dy < 0) { temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 = temp; } k = (float)dx / (float)dy; // Set middle pixels int ys; float xt = (float)x1 + k; float distance; for (ys=y1+1; ys<y2; ys++) { distance = (float)(xt - (int)(xt)); ::SetPixel(m_hDC, (int)xt, ys, color); glVertex2f((int)xt,ys); ::SetPixel(m_hDC, (int)xt+1, ys, color); glVertex2f((int)xt+1,ys); xt += k; } } // Set end pixel ::SetPixel(m_hDC, x2, y2, color); glVertex2f(x2,y2); glEnd();
It depends on the settings your using for your OpenGL renderer.
Don't try it, just do it! ;-)
-
It depends on the settings your using for your OpenGL renderer.
Don't try it, just do it! ;-)
[Message Deleted]
-
[Message Deleted]
Member 3375334 wrote:
gluOrtho2D(0.0, width, 10.0, height);
The gluOrtho2D function has the following arguments:
void gluOrtho2D ( GLdouble left , GLdouble right , GLdouble bottom , GLdouble top );
So, first question, why do you pass 10 for the bottom instead of 0 ? Anyway, if you want to use the same coordinate system as with the MFC (top=0 and y value increase when you go down), then you have to invert the top and bottom:gluOrtho2D(0.0, width, height, 0.0);
Another point: why are you using SetPixel in an OpenGL environment ? If you want to draw things with OpenGL, then it is best to use only OpenGL primitives to do so.Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Member 3375334 wrote:
gluOrtho2D(0.0, width, 10.0, height);
The gluOrtho2D function has the following arguments:
void gluOrtho2D ( GLdouble left , GLdouble right , GLdouble bottom , GLdouble top );
So, first question, why do you pass 10 for the bottom instead of 0 ? Anyway, if you want to use the same coordinate system as with the MFC (top=0 and y value increase when you go down), then you have to invert the top and bottom:gluOrtho2D(0.0, width, height, 0.0);
Another point: why are you using SetPixel in an OpenGL environment ? If you want to draw things with OpenGL, then it is best to use only OpenGL primitives to do so.Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++the reason why i am using SetPixel is because i need to draw line in MFC and at the same time make it draw in OpenGL... i am sorry i didn't mention that earlier. that 10 was a mistake i meant for it to be 0 yes. another question i wanted to ask actually was that when i add OpenGL to MFC and roll my mouse over the screen i get entirely black screen and do not see on the left side my MFC line anymore. So what i am trying to do is to draw MFC line on the left and project that line the same exact way on the right. what am i doing wrong for that? because if i am trying to change a window color wihich by default is black to white my window flickers. when i close the window i see briefly my MFC line....but it's like behind the OpenGL layer... what's wrong?
-
Member 3375334 wrote:
gluOrtho2D(0.0, width, 10.0, height);
The gluOrtho2D function has the following arguments:
void gluOrtho2D ( GLdouble left , GLdouble right , GLdouble bottom , GLdouble top );
So, first question, why do you pass 10 for the bottom instead of 0 ? Anyway, if you want to use the same coordinate system as with the MFC (top=0 and y value increase when you go down), then you have to invert the top and bottom:gluOrtho2D(0.0, width, height, 0.0);
Another point: why are you using SetPixel in an OpenGL environment ? If you want to draw things with OpenGL, then it is best to use only OpenGL primitives to do so.Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++[Message Deleted]
-
[Message Deleted]
Member 3375334 wrote:
old lines are not being erased like in MFC one. I thought by doing PushMatrix will do the trick...
PushMatrix doesn't change anything of what is already drawn on the screen. In fact what you are doing is drawing things on a buffer that was not cleared. Because of that, your previous lines are still visible: it's a bit like drawing things on a paper, if you don't erase what's already on the paper, it will still be visible. MFC and OpenGL work completely differently on that aspect (and on many others). PushMatrix simply pushes the current model/view matrix one place down the stack so that you can 'save' it for later. This model/view matrix is simply a matrix that will be applied to points in your model in order to position them correctly. Also, your design is a bit ugly. You should draw things in your mouse handler. Instead you should store your data and put all the drawing code in the OnPaint handler. For your other questions, I don't really understand what you are trying to do: it seems that you are drawing both from MFC and from OpenGL on the same window. In that sense, chances are that you will only be able to see the results from one of the two. Why do you need to have MFC and OpenGL drawing on the same window ?
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Member 3375334 wrote:
old lines are not being erased like in MFC one. I thought by doing PushMatrix will do the trick...
PushMatrix doesn't change anything of what is already drawn on the screen. In fact what you are doing is drawing things on a buffer that was not cleared. Because of that, your previous lines are still visible: it's a bit like drawing things on a paper, if you don't erase what's already on the paper, it will still be visible. MFC and OpenGL work completely differently on that aspect (and on many others). PushMatrix simply pushes the current model/view matrix one place down the stack so that you can 'save' it for later. This model/view matrix is simply a matrix that will be applied to points in your model in order to position them correctly. Also, your design is a bit ugly. You should draw things in your mouse handler. Instead you should store your data and put all the drawing code in the OnPaint handler. For your other questions, I don't really understand what you are trying to do: it seems that you are drawing both from MFC and from OpenGL on the same window. In that sense, chances are that you will only be able to see the results from one of the two. Why do you need to have MFC and OpenGL drawing on the same window ?
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++Ok the fact that my code is so ugly is because i am brand new to all this so please don't be judgmental :) the reason why i am doing it in mousemove..is because that's what i need to do.. i need to draw on mouse move not paint. when i move my mouse i should be drawing lines. So that's right. The reason why i need to see MFC is because that's what i need to do based on the requirements. where should i do clear?
-
Ok the fact that my code is so ugly is because i am brand new to all this so please don't be judgmental :) the reason why i am doing it in mousemove..is because that's what i need to do.. i need to draw on mouse move not paint. when i move my mouse i should be drawing lines. So that's right. The reason why i need to see MFC is because that's what i need to do based on the requirements. where should i do clear?
Member 3375334 wrote:
Ok the fact that my code is so ugly is because i am brand new to all this so please don't be judgmental
I don't judge you, I was just saying that what you did is not the best design.
Member 3375334 wrote:
the reason why i am doing it in mousemove..is because that's what i need to do.. i need to draw on mouse move not paint.
Of course, but storing your information (the coordinates of each lines for instance) in the mouse handler and forcing a repaint of the view will have the same effect. Besides, all drawing code from the MFC has to be put in the OnPaint handler, otherwise if your window is hidden for a while (even partially), all the MFC drawing will be lost because windows doesn't store any information about what to draw but instead issues a WM_PAINT message and lets the window repaint itself. As you didn't provide any drawing code there, everything is lost.
Member 3375334 wrote:
The reason why i need to see MFC is because that's what i need to do based on the requirements.
And what are your exact requirements here ? Because I don't see any reason why you would need to use MFC AND OpenGL on the same window. How can you expect to see one drawing through the other one ? I'm not an expert in mixing MFC and OpenGL drawings together, but I think this is not possible (not sure about it, but it sounds really strange anyway). Think of it this way: imagine that OpenGL and MFC are painting things on a buffer (like a real life painting). Now, each of them puts its buffer on the screen ("they hang their paintings on the wall" :) ). So, you can't see both of the paintings at the same time... Besides, another strange thing is that OpenGL and MFC have different levels of 'abstraction': with MFC, you draw things on your screen and everything is 'pixel based'. With OpenGL, you are one level higher: your model have a space which is converted to screen coordinates by OpenGL. In your case, it happens that your units in the space correspond to pixels because you configured OpenGL this way. Suppose now that you want to add features like zoom, which is easy to do in OpenGL, then the MFC drawing won't follow.
Cédric Moonen Software developer
Charting control [v1.5]