Hello all,
thank you for your patience and your replies. I can see that my posts are very unclear. I am a little frustrated at the code and the pressure to get things working was amounting. I very apologize to the community and I hope that you all can forgive.
If I may, please let me try again on a clean slate.
I am attempting to create a 2D CAD editor with some very basic functions to draw shapes take measurements, etc. I will not be using shaders since everything is literally going to be black and white. I know that the older versions of opengl have a number of shortcomings but since I will not be using the majority of the featurs, I figured I could get aaway with using an older version. I was also hoping that the older version would be "simplier". I am also looking for a very wide range of device support so I went with GL 2.1 (I heard that 1.1 is a very bad idea so 2.1 seemed to suffice). This CAD editor will be used in a finite Element simulator (Just to give you some context of where I am going). Thankfully, I am not going to be getting into 3D at this time and I am sticking to only 2D.
Below is the infinite grid space. I hope that someone will find this beneficial later on. Although, I am still very new to openGL programming and this was found on a basis of guess and check. So I do have some questions on why something work.
void geometryEditorCanvas::drawGrid()
{
//Reset to modelview matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPushMatrix();
glTranslatef(canvasWidth / 2.0f - (float)cameraX, canvasHeight / 2.0f - (float)cameraY, 0.0f);
for(double i = 0; i < factor; i++)
{
for(double j = 0; j < factor; j++)
{
glBegin(GL_POINTS);
glColor3d(0.0d, 0.0d, 0.0d);
glVertex2d((j * coordinateFactorWidth - canvasWidth / 2.0d ), (i * coordinateFactorHeight - canvasHeight / 2.0d));
glEnd();
}
}
}
In the function, I am selecting the model view matrix, then I am popping and pushing a matrix to the stack. I am kinda of borrowing code from an example and when they were drawing, they were poping and pushing the matrix as such. The author did not go into great detail but what is the purpose of doing this? (Also, if you see any issues with my terminology or my interpretation of anything, please point it out and correct me so that I can make sure that my understanding is correct)
Continuing on, I set the view matrix to the center of the screen (I think that what this is happening is that I am actually setting the eniter model to the center of the screen). Now we go into the whole double for loop. My idea is that one loop controls the marking for the x-axis and the other controls the markings for the y-axis. CoordinateFactorWidth is a number which describes the number of pixels between grid markings and factor is a number controlling the number of grid markings that the user will see. The glColor3d(0.0d, 0.0d, 0.0d) is crucial because this will set the points to be black (yes, this compiles and this is legit. At least, it compiles on my linux machine).
CameraX and CameraY is a variable which describes the number of pixels that the model is shifted by. The code for this is found below:
void geometryEditorCanvas::onKeyDown(wxKeyEvent &event)
{
if(event.GetKeyCode() == LETTER_W || event.GetKeyCode() == LETTER_w)
{
cameraY -= 16.0f;
}
else if(event.GetKeyCode() == LETTER_S || event.GetKeyCode() == LETTER_s)
{
cameraY += 16.0f;
}
else if(event.GetKeyCode() == LETTER_A || event.GetKeyCode() == LETTER_a)
{
cameraX -= 16.0f;
}
else if(event.GetKeyCode() == LETTER_d || event.GetKeyCode() == LETTER_D)
{
cameraX += 16.0f;
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glLoadIdentity();
glTranslated(cameraX, cameraY, 0.0f);
glPushMatrix();
this->Refresh();// This will force the canvas to experience a redraw event
}
And for those curious, yes, this is my own code that I handwritten. Some parts, I saw they were doing in the tutorial which I emulated but, I do not have much understanding why they did as such.
Above, I have an initialize routine which is setting the projection by glOrtho.
Overall, I am wondering, using openGL 2.1, is there a better method of doing this from what i have? What improvements do you see that I can implement? (improvements could be it is more efficient or the code could be structured better, etc.