can't draw my rectangle where I want on the screen - c++

I need to draw case/ spare which are Rectangle.
but the problem is that glTranslated()doesn't work. When I call it my rectangle isn't draw anymore.
my rectangle: glRectf(-0.032f, 0.032f, 0.032f, -0.032f);
(I don't understand values but working)
full code:
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(254,128,1);
//glTranslated(50, 20, 0); when I remove the comment, my rectangle doesn't appear.
glRectf(-0.032f, 0.032f, 0.032f, -0.032f);
glFlush();
SDL_GL_SwapBuffers();
}

glRectf(x1,y1,x2,y2) means draw a rectangle from point (-0.032,0.032) to the diagonally opposite corner at (0.032,-0.032).
glTranslated(50,20,0) is applying a vector to move it. Those are pretty big numbers since you are saying you can see the rectangle (square) of size 0.064 wide. It may be drawing out of your viewport so you can't see it.
Try some small numbers and also glTranslated requires doubles (so I would cast them too).
Read more here...
http://www.cprogramming.com/tutorial/opengl_first_opengl_program.html

Related

OpenGL antialiasing with glAccum and glRasterPos

My task : make weighted antialiasing algorithm with accumulation buffer in OpenGL. In other words, i have pixels array, which i have to shift by one pixel in all directions with multipliers.
My problem : I don`t really understand what my code does.
while(!glfwWindowShouldClose(window)) {
glReadBuffer(GL_FRONT);
glDrawBuffer(GL_FRONT);
glDrawPixels(800, 800, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glAccum(GL_LOAD, 1.0);
glRasterPos2d(1.0, 0.0);
glAccum(GL_ACCUM, 2.0);
glRasterPos2d(0.0, 1.0);
glAccum(GL_ACCUM, 2.0);
glAccum(GL_RETURN, 1.0);
glfwPollEvents();
glfwSwapBuffers(window);
}
As i understand, glRasterPos changes "cursor" position, so with changing it
my picture should shift to one pixel to right and to bottom. But i don't see any antialiasing results, just blinking of my shape(pixels array contains only white pixels). I understand that i should do GL_ACCUM to all nine directions. And i should use exactly glRasterPos for this.
What don't i understand with glAccum and glRasterPos?
Calling glRasterPos doesn't move anything, it just sets current position - treat it as assigning values to internal posx and posy variables. You should draw your image again (call glDrawPixels) in order to see the effect of this call.

Why doesn't OpenGL display the requested drawings correctly

Greetings I'm new to OpenGL and Glut. I made a function that displays the 4 dots that make the corners of a square without uniting the dots. The problem is when I run the project, the function doesn't display anything.
I have a similar function that works with lines and displays some lines in the corners of the window which works perfectly. Please tell me what I'm doing wrong and how to fix it. Here are the two functions:
Using Lines. Works perfectly
void Display2() {
glColor3f(1,0.1,0.1);
glBegin(GL_LINES);
glVertex2f(1.0,1.0);
glVertex2f(0.9,0.9);
glVertex2f(0.8,0.8);
glVertex2f(0.7,0.7);
glVertex2f(0.6,0.6);
glVertex2f(0.5,0.5);
glVertex2f(-0.5,-0.5);
glVertex2f(-1.0,-1.0);
glEnd();
}
The one with dots. Does not display anything.
void Display3() {
glColor3f(1,0.1,0.1);
glBegin(GL_POINTS);
glVertex2f(100, 100);
glVertex2f(200, 100);
glVertex2f(100, 200);
glVertex2f(200, 200);
glEnd();
}
The default visible area in OpenGL ranges from -1 to 1 on each axis (the so-called normalized device coordinates).
In your line example, all the values are in this range and thus are visible. In the point example, the coordinates are simply outside of the screen. You can either change the coordinates to fit in the [-1,1] interval, or add a projection matrix that handles that for you.

LWJGL 3D picking

So I have been trying to understand the concept of 3D picking but as I can't find any video guides nor any concrete guides that actually speak English, it is proving to be very difficult. If anyone is well experienced with 3D picking in LWJGL, could you give me an example with line by line explanation of what everything means. I should mention that all I am trying to do it shoot the ray out of the center of the screen (not where the mouse is) and have it detect just a normal cube (rendered in 6 QUADS).
Though I am not an expert with 3D picking, I have done it before, so I will try to explain.
You mentioned that you want to shoot a ray, rather than go by mouse position; as long as this ray is parallel to the screen, this method will still work, just the same as it will for a random screen coordinate. If not, and you actually wish to shoot a ray out, angled in some direction, things get a little more complicated, but I will not go in to it (yet).
Now how about some code?
Object* picking3D(int screenX, int screenY){
//Disable any lighting or textures
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE);
//Render Scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
orientateCamera();
for(int i = 0; i < objectListSize; i++){
GLubyte blue = i%256;
GLubyte green = min((int)((float)i/256), 255);
GLubyte red = min((int)((float)i/256/256), 255);
glColor3ub(red, green, blue);
orientateObject(i);
renderObject(i);
}
//Get the pixel
GLubyte pixelColors[3];
glReadPixels(screenX, screenY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixelColors);
//Calculate index
int index = pixelsColors[0]*256*256 + pixelsColors[1]*256 + pixelColors[2];
//Return the object
return getObject(index);
}
Code Notes:
screenX is the x location of the pixel, and screenY is the y location of the pixel (in screen coordinates)
orientateCamera() simply calls any glTranslate, glRotate, glMultMatrix, etc. needed to position (and rotate) the camera in your scene
orientateObject(i) does the same as orientateCamera, except for object 'i' in your scene
when I 'calculate the index', I am really just undoing the math I performed during the rendering to get the index back
The idea behind this method is that each object will be rendered exactly how the user sees it, except that all of a model is a solid colour. Then, you check the colour of the pixel for the screen coordinate requested, and which ever model the colour is indexed to: that's your object!
I do recommend, however, adding a check for the background color (or your glClearColor), just in case you don't actually hit any objects.
Please ask for further explanation if necessary.

Opengl: Viewport, clipping, matrixtmode confusion

I've been studying Computer Graphics and I'm very confused about the role of the viewport, gluortho and when to use GL_MatrixMode and GL_Projection.
Here is a sample code I wrote that confuses me.
void init()
{
glClearColor(1.0,1.0,1.0,1.0);//Background Color of Viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-200,200,-200,200,-50,50);
glMatrixMode(GL_MODELVIEW);
}
void wheel()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0.2,0.2);
glLoadIdentity();
glViewport(0,0,200,200);
glutSolidCube(100);
glFlush();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize(400,400);
glutInitWindowPosition(400,400);//Position from the top left corner
glutCreateWindow("Car");
init();
glutDisplayFunc(wheel);//Shape to draw
glutMainLoop();
}
When I change the Cube's size to 200 it disappears, why? Is that because it's larger than the z clipping?
When I remove glMatrixMode(GL_MODELVIEW) the cube disappears why?
If I don't flush at the end of the display function the cube disappears as well,why?
When I make the viewport smaller the object get smaller does that mean the object coordinates are relative to the viewport and not the world coordinates?
When you change the cubes size to 200, its faces extend beyond the near and far clipping planes, which you've set in your glOrtho call to -50 and 50. Technically you'd then be viewing the inside of the cube, but the far side of the cube is also outside of the far clipping plane, so you can't see its backface.
Removing the call to set the matrix mode to GL_MODELVIEW means your glLoadIdentity call operates on the fixed functionality projection matrix (I'm pretty sure), and so the cube is directly translated into Normalized Device Coordinates, and it once again extends beyond all the clipping planes.
Finally, glViewport defines the size of the buffer you should be rendering to, and therefore usually matches your screen size. Making it smaller effectively makes your screen size smaller, but does not change the actual GLUT window size. In mathematical terms, it changes the way fragments are projected from normalized device coordinates into screen coordinates.

OpenGL: perspective view centered not in the middle of the view?

I have a main scene centered on the center of the viewport
in addition to that I want another small object to be displayed on the corner of the viewport.
The trouble is that when I draw the small object, it is transformed by the main projection transformation and appears slanted. I want the small object to have its own vanishing point centered in its center.
Is this possible which just transformations?
You want your main scene to be projected in one way and your corner object to be projected in another way. This directly leads you to the solution:
void render() {
glMatrixMode(GL_PROJECTION);
setUpMainProjection();
glMatrixMode(GL_MODELVIEW);
drawMainObject();
glMatrixMode(GL_PROJECTION);
setUpCornerProjection();
glMatrixMode(GL_MODELVIEW);
drawCornerObject();
}
Perhaps you're wondering how to implement setUpCornerProjection. It would look something like this:
// let's say r is a rect, which, in eye space, contains the corner object and is
// centered on it
glFrustum(r.left, r.right, r.bottom, r.top, nearVal, farVal);
// let's say p is the rect in screen-space where you want to
// place the corner object
glViewport(p.x, p.y, p.width, p.height);
And then in setUpMainProjection() you'd need to also call glFrustum and glViewport.