Draw this shape - c++

glBegin(GL_POLYGON);
// color of the shape
glColor3f(1.0, 1.0, 1.0);
// draw shape
glVertex2f(50, 200);
glVertex2f(50, 170);
glVertex2f(80, 200);
glVertex2f(80, 240);
glVertex2f(50, 240);
glEnd();
I am struggling to draw the shape below. My shape just needs to be rotated.
Preview:
How the shapes should look like:

The primitive type GL_POLYGON concatenates the vertex coordinates to a convex polygon. The last point is concatenated to the first point. For a shape with 4 corner points, 4 vertex coordinates are sufficient:
+ (80, 240)
/ |
Y / |
^ / |
| (50, 200) + |
| | |
| | |
| | |
| (50, 170) +-----+ (80, 170)
|
+-------------> X
glBegin(GL_POLYGON);
glVertex2f(50, 170);
glVertex2f(80, 170);
glVertex2f(80, 240);
glVertex2f(50, 200);
glEnd();

Related

OpenVG draw circle

My hardware have two Layers, I draw the background at the layer0
I draw a circle like this:
(a color circle, other area is transparent.) at the layer1
My code is :
vgPaintPattern(maskPaint, maskImage);
vgSetPaint( maskPaint, VG_FILL_PATH );
vgSeti(VG_MATRIX_MODE, VG_MATRIX_FILL_PAINT_TO_USER);
vgLoadIdentity();
vgTranslate (-HALO_W / 2, -HALO_W / 2);
vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
vgLoadIdentity();
vgTranslate (pointer_x, floater_y);
vgSeti ( VG_FILL_RULE, VG_NON_ZERO );
path = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1.0f, 0.0f, 0, 0,
VG_PATH_CAPABILITY_ALL);
vguArc( path, 0.0f, 0.0f, HALO_W, HALO_W, startAngle, angleExtent, VGU_ARC_PIE );
vgRemovePathCapabilities(path, VG_PATH_CAPABILITY_APPEND_FROM | VG_PATH_CAPABILITY_APPEND_TO |
VG_PATH_CAPABILITY_MODIFY |
VG_PATH_CAPABILITY_TRANSFORM_FROM |
VG_PATH_CAPABILITY_TRANSFORM_TO |
VG_PATH_CAPABILITY_INTERPOLATE_FROM |
VG_PATH_CAPABILITY_INTERPOLATE_TO);
vgDrawPath( path, VG_FILL_PATH );
vgDestroyPath( path );
vgSetPaint (VG_INVALID_HANDLE, VG_FILL_PATH);
vgPaintPattern(maskPaint, VG_INVALID_HANDLE);
but the result is this:
the center part is black not transparent. How did I do?

OpenGL glOrtho set up match the window size

I am a beginner in OpenGl and I am struggling a bit with setting up the glOrtho camera to match the window size so that I can draw a line using the window's coordinates. For example, if I want to draw a line from coordinates 0,10 (x,y) to 600,10. I managed to draw the line (which will be a "Separator" from the viewport and a toolbar with buttons) in my current set up but it was by "try end error" approach and the coordinates that I needed to put don't make any sense to me. When I tried to draw a line using the above-mentioned coordinates, the line simply did not show up. What I need to change in the glOrtho set up in order to work with these (1000x600) screen size and draw my vertices and not these:
glVertex3f(-2.0, 11.0, 0.0);
glVertex3f(20.0, 11.0, 0.0);
Note, my current window size is 1000x600 (width/height)
This is the line (on the top that crosses the whole screen):
This is my OGWindow class that handles all of the drawing:
void OGWindow::MyReSizeGLScene(int fwidth, int fheight)
{
// Store window size in class variables so it can be accessed in myDrawGLScene() if necessary
wWidth = fwidth;
wHeight = fheight;
// Calculate aspect ration of the OpenGL window
aspect_ratio = (float) fwidth / fheight;
// Set camera so it can see a square area of space running from 0 to 10
// in both X and Y directions, plus a bit of space around it.
Ymin = -1;
Ymax = 12;
Xmin = -1;
// Choose Xmax so that the aspect ration of the projection
// = the aspect ratio of the viewport
Xmax = (aspect_ratio * (Ymax -Ymin)) + Xmin;
glMatrixMode(GL_PROJECTION); // Select The Projection Stack
glLoadIdentity();
glOrtho(Xmin, Xmax, Ymin, Ymax, -1.0, 1.0);
glViewport(0, 0, wWidth, wHeight); // Viewport fills the window
}
void OGWindow::myDrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the drawing area
OGWindow::myDrawModel();
drawToolbar();
glutSwapBuffers(); // Needed if we're running an animation
glFlush();
}
void OGWindow::myDrawModel(GLvoid)
{
switch ( squareColour ) {
case RED:
glColor3f(1.0, 0.0, 0.0);
break;
case BLUE:
glColor3f(0.0, 0.0, 1.0);
break;
}
glBegin( GL_QUADS );
glVertex3f( squareX, squareY, 0.0 ); // Coordinates of bottom-left corner of square
glVertex3f( squareX + squareWidth, squareY, 0.0 );
glVertex3f( squareX + squareWidth, squareY + squareHeight, 0.0 );
glVertex3f( squareX, squareY + squareHeight, 0.0 );
glEnd();
}
// Convert from screen coords returned by mouse
// to world coordinates.
// Return result in worldX, worldY
void OGWindow::screen2World(int screenX, int screenY, double & worldX, double & worldY)
{
// Dimensions of rectangle viewed by camera projection
double projWidth = Xmax -Xmin;
double projHeight = Ymax - Ymin;
// Screen coords with origin at bottom left
int screenLeft = screenX;
int screenUp = wHeight - screenY;
worldX = Xmin + screenLeft * projWidth / wWidth ;
worldY = Ymin + screenUp * projHeight / wHeight ;
}
//Method to draw the toolbar separator line
void OGWindow::drawToolbar(GLvoid) {
//draw toolbar line separator
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3f(-2.0, 11.0, 0.0);
glVertex3f(20.0, 11.0, 0.0);
glEnd();
//draw create button
glPushMatrix();
glTranslatef(2.0, 10.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();
}
This is my main class where I am ivoking the methods from OGWindow:
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize( 1000, 600 );
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Demo");
glEnable(GL_DEPTH_TEST); // enable the depth buffer test
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glutPassiveMotionFunc(mousePassiveMotion);
glutIdleFunc(Idle);
theWindow.initGL();
glutMainLoop();
}
Check out the documentation of glOrtho function. As you see, there are 6 parameters: left, right, bottom, top, near, far. You made mistake by setting window width to top instead of bottom parameter. Here's proper use of function:
glOrtho (0, 1000, 600, 0, -1.0, 1.0)
So, first your ortho settings. If you want your camera to match the screen dimensions, glOrtho has to use the same dimensions.
// This will anchor the camera to the center of the screen
// Camera will be centered on (0,0)
glOrtho( -screenWidth/2.f, screenWidth/2.f, -screenHeight/2.f, screenHeight/2.f, -1, 1 );
// This will anchor the camera to the lower left corner of the screen
// Camera will be centered on (screenWidth/2, screenHeight/2)
glOrtho( 0, screenWidth, 0, screenHeight, -1, 1 );
Try both and see the difference. Although if you are making some sort of editor, where your camera doesn't move, you may be looking for the second ortho setup.
Second, you only ever use (apparently) the GL_PROJECTION matrix mode. You must use this mode to set the camera projection and GL_MODELVIEW to apply transforms to the camera or the objects.
So when you call resize and don't change the matrix mode back to GL_MODELVIEW, you'll be applying translations to the projection matrix.
If you did forget to initialize the modelview matrix it may contain garbage values and yield unexpected results.

Texture not displaying properly - probably coordinates are wrong OpenGL, C++

I'll try to explain my problem with images. So this is a test texture I'm using for my OpenGL application:
As you can see, there is a 2-pixels wide border around the image with different colors for me to be able to see if coordinates are properly set in my application.
I'm using a 9-cell pattern so I'm drawing 9 quads with specific texture coordinates. At first sight everything works fine, but there is a small problem with displaying a texture:
In the picture I marked where is first quad, and where is the second one. As you can see, first one is displayed correctly, but second one smoothly goes from first quad's colors to it's own, but it should start with pure green and pink. So I'm guessing it's a problem with texture coordinates.
This is how they are set:
// Bottom left quad [1st quad]
glBegin(GL_QUADS);
// Bottom left
glTexCoord2f(0.0f, 1.0);
glVertex2i(pos.x, pos.y + height);
// Top left
glTexCoord2f(0.0f, (GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x, pos.y + height - m_borderWidth);
// Top right
glTexCoord2f(maxTexCoordBorderX, (GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x + m_borderWidth, pos.y + height - m_borderWidth);
// Bottom right
glTexCoord2f(maxTexCoordBorderX, 1.0);
glVertex2i(pos.x + m_borderWidth, pos.y + height);
glEnd();
// Bottom middle quad [2nd quad]
glBegin(GL_QUADS);
// Bottom left
glTexCoord2f(maxTexCoordBorderX, 1.0);
glVertex2i(pos.x + m_borderWidth, pos.y + height);
// Top left
glTexCoord2f(maxTexCoordBorderX, (GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x + m_borderWidth, pos.y + height - m_borderWidth);
// Top right
glTexCoord2f((GLfloat)1.0 - maxTexCoordBorderX, (GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x + width - m_borderWidth, pos.y + height - m_borderWidth);
// Bottom right
glTexCoord2f((GLfloat)1.0 - maxTexCoordBorderX, 1.0);
glVertex2i(pos.x + width - m_borderWidth, pos.y + height);
glEnd();
You can see that I'm using maxTexCoordBorderX variable which is calculated based on border and image size. Image width is 32 and border width is 2.
maxTexCoordBorderX = 2 / 32 = 0.0625
Could anyone please help with with finding out where the problem is?
Most likely culprit is that you are not sampling on the texel centers. For example, if you have a 32x32 pixel texture, the texel-centers are offset by 1/64.
Here's a rough diagram of a 4x4 texture. The squares are the texels (or pixels) of the image.
_________________1,1
| | | | |
| | | | |
|___|___|___|___|_0.75
| | | | |
| | | | |
|___|___|___|___|_0.5
| | | | |
| | | | |
|___|___|___|___|_0.25
| | | | |
| X | | | |
|___|___|___|___|
0,0 | 0.5 | 1
0.25 0.75
x = (0.125, 0.125)
If you sample on one of the lines, you will get a value exactly between two texels, which will (if you have texture sampling set to linear blend) give you an average value. If you want to sample the exact texel value, you need to specify a u,v in the center of the texel.
You've running into a fencepost problem. I answered the solution to your very problem here
https://stackoverflow.com/a/5879551/524368

OpenGL: Trying to create a solid cylinder using triangle_fan

I am trying to create a solid cylinder using triangle fan.
What I've done so far is this:
float base = 0.5;
float height = 20;
float radius = 2.0f;
glBegin(GL_TRIANGLE_FAN);
for(float j=0; j<=height; j+=0.1)
{
glVertex3f(0,j,0);
for(int i=0; i<360; i++)
{
glVertex3f(radius*sin((float)i),j, radius*cos((float)i));
}
}
glEnd();
glPopMatrix();
The problem appears in these 3 screenshots:
As you see in all 3 screenshots there appears to be some space and not a solid cylinder.
Is that ok?
First thing you should pay attention to (Edit: I slightly misread your code. You are doing fine with the triangle fan) is that a triangle fan works like this:
glVertex: Center point
for each outer point p
glVertex: p
For example:
p2__
/| ---___p1
/ | /
/ | /
p3/ | /
|\ | /
| \ | /
| \_O Center
| __---
p4
Second thing is that a cylinder consists of three parts:
__
/ \
\__/ <---- circle on top (facing up)
| |
| |
| |
| | <---- tube in the middle
| |
| |
| |
\__/ <---- circle on the bottom (facing down)
So the algorithm you need is:
/* top triangle */
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, height, 0); /* center */
for (i = 0; i <= 2 * PI; i += resolution)
glVertex3f(radius * cos(i), height, radius * sin(i));
glEnd();
/* bottom triangle: note: for is in reverse order */
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, 0); /* center */
for (i = 2 * PI; i >= 0; i -= resolution)
glVertex3f(radius * cos(i), 0, radius * sin(i));
/* close the loop back to 0 degrees */
glVertex3f(radius, height, 0);
glEnd();
/* middle tube */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= 2 * PI; i += resolution)
{
glVertex3f(radius * cos(i), 0, radius * sin(i));
glVertex3f(radius * cos(i), height, radius * sin(i));
}
/* close the loop back to zero degrees */
glVertex3f(radius, 0, 0);
glVertex3f(radius, height, 0);
glEnd();
The way you were trying to do is first, not correct, since you are not actually making a cylinder, but stacking many circles, and second is inefficient since you are filling in space that is mostly invisible (the inside of the cylinder).
for(int i=0; i<360; i++)
{
glVertex3f
(
radius*sin((float)i),
j,
radius*cos((float)i)
);
}
The C standard library sin() and cos() functions take radians, not degrees.
Try converting (float)i to radians before you pass it to sin()/cos().

why does not my opengl program work?

I was writing a program to draw a square in my XY plane and make it rotate 360 degree, but it is not working.
void setupRC()
{
glClearColor(0,0,1,1);
glColor3f(1,0,0);
}
void timerfunc(int value)
{
glutPostRedisplay();
glutTimerFunc(33, timerfunc ,1);
}
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static GLfloat rot = 0.0f,x =0.0f , y=10.0f , z=0.0f;
rot++;
x = 10 * cos(rot);
y = 10 * sin(rot);
glPushMatrix();
glRotatef(rot,0.0,1.0,0.0);
glBegin(GL_POLYGON);
glVertex3i(10,-10,0);
glVertex3i(10,10,0);
glVertex3i(-10,10,0);
glVertex3i(-10,-10,0);
glVertex3i(10,-10,0);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void ChangeSize(GLint w, GLint h)
{
if(h==0)
h = 1;
GLfloat aspectratio = (GLfloat)w/(GLfloat)h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, aspectratio, -1.0, 400.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc , char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutCreateWindow("chelsea");
glutTimerFunc(33, timerfunc , 1);
setupRC();
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutMainLoop();
return 0;
}
I get no output, just a blank screen.
gluPerspective(60.0f, aspectratio, -1.0, 400.0);
Looks wrong, the near clipping plane needs to be a positive number
You should really use glRotatef for rotating, because your code is wrong if you want a plane rotating around XY (x and y will be 0 for some values of rot and you'll get to see nothing at all).
And you should perhaps give your polygons a color using the glColor commands.
As JB said, your gluPerspective call seems wrong, too. See this link for a reference.