Drawing a line along z axis in opengl - opengl

I am trying to draw a line along the point 0.5,-0.5,0.0 to 0.5,-0.5,-0.5 using GL_LINES in the z direction .
Intialization of the window :
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB);
Setup in the display function.
glClearColor(1.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
However, the line is not displayed on the screen. Please help as to how to display a line going along the z direction.

You should probably share the piece of code where you actually attempt to draw the line using GL_LINES. Without it I must assume that you don't know how to do it properly. The correct way to draw the line after the setup is:
glBegin(GL_LINES);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
Have you tried it that way? Also, if you use double buffering, don't forget to swap buffers after rendering, using glutSwapBuffers() when using glut or SwapBuffers(hdc) when not using it.
Edit:
Additionally you need to setup your camera correctly and move it slightly to actually see the line that you draw (it is possible that it's outside of the view area)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,0.1,100); //Example values but should work in your case
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
This piece of code should setup your projection correctly. Now OpenGL by default looks in the negative direction of Z axis so if you want to see your line you need to move tha camera towards the positive end of Z axis using the code (in fact the code moves the whole world, not just your camera, but it doesn't matter):
glTranslate(0.0f,0.0f,-1.0f);
Use this before glBegin and you should be good to go.

Related

OpenGL transformation

I want to ask a question about transformation.
glPushMatrix();
glTranslatef(0.0, -10, 0.0);
glScalef(5000.0, 10.0, 5000.0);
glPushMatrix();
glColor3f(0,0,0);
glutSolidCube(1);
glColor3f(0.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
glPopMatrix();
The above code is the one I wrote to create a cube. In this case, I have understood the centre of the cube will be on y = -5. Did I understand correctly?
One of the great things about computer graphics is that you can always run the program and see what happens. And then swap the order of the translate and scale calls and run it again. You'll figure it out.
In this case, you may have difficulties because there's no reference to compare the cube to. I often use a little drawAxes() function, and recommend it to students, that just draws three lines from 0,0,0 to each of X=1, Y=1, Z=1. (Helps to draw the X line in red, Y in green, Z in blue.) Draw the axes as the very first thing you do so you've got a reference point, draw them again anytime you're not sure where a series of transformations will end up.

Drawing a thick line with legacy OpenGL (immediate mode) in C++

i wanted to create a line that is thick using OpenGL library in c++ but it is not working.
i tried this code:
glBegin(GL_LINES);
glLineWidth(3);
glVertex2f(-0.7f, -1.0f);
glVertex2f(-0.7f, 1.0f);
glEnd();
is there something wrong here?
Note that rendering with glBegin/glEnd sequences and also glLineWidth is deprecated. See OpenGL Line Width for a solution using "modern" OpenGL.
It is not allowed to call glLineWidth with in a glBegin/glEnd sequence. Set the line width before:
glLineWidth(3);
glBegin(GL_LINES);
glVertex2f(-0.7f, -1.0f);
glVertex2f(-0.7f, 1.0f);
glEnd();
Once drawing of primitives was started by glBegin it is only allowed to specify vertex coordinates (glVertex) and change attributes (e.g. glColor, glTexCoord, etc.), till the drawn is ended (glEnd).
All other instruction will be ignored and cause a GL_INVALID_OPERATION error, which can be get by glGetError.

Drawing a primitive ( GL_QUADS ) on top of a 2D texture - no quad rendered, texture colour changed

I am trying to draw a 2D scene with a texture as background and then ( as the program flows and does computations ) draw different primitives on the "canvas". As a test case I wanted to draw a blue quad on the background image.
I have looked at several resources and SO questions to try get the information I need to accomplish the task ( e.g. this tutorial for first primitive rendering, SOIL "example" for texture loading ).
My understanding was that the texture will be drawn on Z=0, and quad as well. Quad would thus "cover" a portion of texture - be drawn on it, which is what I want. Instead the result of my display function is my initial texture in black/blue colour, and not my texture ( in original colour ) with a blue quad drawn on it. This is the display function code :
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
// background render
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 1024.0, 512.0, 0.0, 0.0, 1.f); // window size is 1024x512
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1024.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1024.0,512.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,512.0);
glEnd(); // here I get the texture properly displayed in window
glDisable(GL_TEXTURE_2D);
// foreground render
glLoadIdentity();
gluPerspective (60, (GLfloat)winWidth / (GLfloat)winHeight, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glColor3f(0.0, 0.0, 1.0);
glBegin (GL_QUADS);
glVertex2d(400.0,100.0);
glVertex2d(400.0,500.0);
glVertex2d(700.0,100.0);
glVertex2d(700.0,500.0);
glEnd(); // now instead of a rendered blue quad I get my texture coloured in blue
glutSwapBuffers(); }
I have already tried with many modifications, but since I am just beginning with OpenGL and don't yet understand a lot of it, my attempts failed. For example, I tried with pushing and popping matrices before and after drawing the quad, clearing the depth buffer, changing parameters in gluPerspective etc.
How do I have to modify my code so it will render the quad properly on top of the background texture image of my 2D scene ? Being a beginner, extra explanations of the modifications ( as well as mistakes in the present code ) and principles in general will be greatly appreciated.
EDIT - after answer by Reto Koradi :
I have tried to follow the instructions, and the modified code now looks like :
// foreground render
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glOrtho(0.0f, 1024.0, 512.0, 0.0, 0.0, 1.f);
glColor3f(0.0, 0.0, 1.0);
glBegin (GL_QUADS); // same from here on
Now I can see the blue "quad", but it is not displayed properly, it looks something like this .
Beside that, the whole scene is flashing really quickly.
What do I have to change in my code so that quad will get displayed properly and screen won't be flashing ?
You are setting up a perspective transformation before rendering the blue quad:
glLoadIdentity();
gluPerspective (60, (GLfloat)winWidth / (GLfloat)winHeight, 1.0, 100.0);
The way gluPerspective() is defined, it sets up a transformation that looks from the origin down the negative z-axis, with the near and far values specifying the distance range that will be visible. With this transformation, z-values from -1.0 to -100.0 will be visible. Which does not include your quad at z = 0.0.
If you want to draw your quad in 2D coordinate space, the easiest solution is to not use gluPerspective() at all. Just use a glOrtho() type transformation like you did for your initial drawing.
If you want perspective, you will need a GL_MODELVIEW transformation as well. You can start with a translation in the negative z-direction, within a range of 1.0 to 100.0. You may have to adjust your coordinates for the different coordinate system as well, or use additional transformations that also translate in xy-direction, and possibly scale.
The code also has the coordinates in the wrong order for drawing the blue quad. You either have to change the draw call to GL_TRIANGLE_STRIP (recommended because it at least gets you one step closer to using features that are not deprecated), or swap the order of the last two vertices:
glBegin (GL_QUADS);
glVertex2d(400.0,100.0);
glVertex2d(400.0,500.0);
glVertex2d(700.0,500.0);
glVertex2d(700.0,100.0);
glEnd(GL_QUADS);

C++ OpenGL Empty Cube with Visible Edges

I am trying to create a cube. I want the cube itself to be clear (black since the background is black), but I'd like the 12 lines to be thin and white. Is the only way to do this to create lines and lay them on top of the edges? Or is there a different way to approach it?
The reason being I have to create balls bouncing around inside the box.
Maybe I should just do glBegin(GL_LINES) and not even worry about surfaces to collide against since I can just create that mathematically?
I am just creating my sides like this:
glBegin(GL_POLYGON);
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
You can just draw the 'wireframe' cube. You will see the edges but no faces. Set the fill mode to wire and render lines instead of polygons.
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // this tells it to only render lines
glBegin(GL_LINES);
// endpoints of 1 line/edge
glVertex3f( ...
glVertex3f( ...
// endpoints of second line/edge
glVertex3f(
glVertex3f(
// on up thru all 12 lines/edges
glEnd();
Now, this isn't the most efficient. You could use a line strip perhaps, or just draw 6 quads. But since this is "day one", this might be an easy start.
Eventually you'll want to not used fixed-functionality at all - it's deprecated. But this will give you an environment in which to get comfortable with matrices and lighting, etc. When you have serious gemoetry to render, you'll put it in buffers and send it off to the GPU in big chunks, letting your GLSL shaders process the data on the graphics card.
Welcome to graphics!
Maybe I should just do glBegin(GL_LINES) and not even worry about
surfaces to collide against since I can just create that
mathematically?
Correct. You already know the bounds of your cube.
Do some lines, and bounce your balls.
You could set the polygon mode (glPolygonMode, read here) to GL_LINE to achieve the same thing.
Maybe I should just do glBegin(GL_LINES) and not even worry about surfaces to collide against since I can just create that mathematically?
OpenGL isn't going to help you with collisions of any sort.
As a somewhat off topic note, consider using a more modern approach. Immediate mode drawing is effectively deprecated, even if you aren't using the newer OpenGL versions.
This is a decent place to start

View the inside of a cylinder

If I draw a gluCylinder with a gluDisk on top. Without culling enabled, I get the desired cylinder with lid effect. However, if I enable culling, the disk (aka lid) disappears. Why is that? This is the main question. In addition, with culling enabled the back faces of the cylinder are also not drawn. I get why this is happening but I would still like to see the inside of the cylinder drawn. The code is:
glPushMatrix()
quadratic = gluNewQuadric()
gluQuadricNormals(quadratic, GLU_SMOOTH)
gluQuadricTexture(quadratic, GL_TRUE)
glRotatef(90, 1, 0, 0)
glTranslate(0, 0, -3*sz)
gluCylinder(quadratic, 0.75*sz, 0.75*sz, 3.0*sz, 32, 32)
gluDisk(quadratic, 0.0, 0.75*sz, 32, 32)
glPopMatrix()
Your disk is facing in the wrong direction (wrong winding). Therefore, it is culled. You can try to reverse its orientation using gluQuadricOrientation, this should do the trick. For more information, refer to the OpenGL spec for gluDisk and glCullFace.
A disk is just a plane without any thickness. So one side is front and the other is back and with culling enabled one of those gets culled away. You are probably just seeing the culled away side. If this is not the side you want to see, just rotate the disk around. Nothing fancier to it. So just wrap it into a:
glPushMatrix();
glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
gluDisk(quadratic, 0.0, 0.75*sz, 32, 32);
glPopMatrix();
Or, like kroneml suggests, change its triangles' orientations. Decide for yourself which one is more conceptually correct in your situation.