I am trying to draw the shape shown below (in black) in OpenGL (for desktops, not mobile). I'd prefer it in 3D, but 2D would suffice as well. I will be mapping a texture to it as well. I've used triangle fans/strips for other things in the past and I imagine that's what I'd need to use here but I'm just not sure where to start on it. I've drawn the WHITE part before, but never the inverse (the black part). Any thoughts or guidance on what to use (triangle fan, triangle strip, some other odd OpenGL shape I probably didn't know existed, etc...)
Final Solution:
void draw_arch(GLfloat width, GLfloat height, GLint slices)
{
glPushMatrix();
GLfloat offset = 0.5f;
glScalef(width/2,height/(1+offset),1.0f);
glBegin(GL_QUADS);
for( unsigned int i = 0; i < slices; ++i ) {
float curAngle = ( ( i + 0 ) / (float)slices ) * 3.14159;
float nxtAngle = ( ( i + 1 ) / (float)slices ) * 3.14159;
glVertex2f( cos( curAngle ), sin( curAngle ) );
glVertex2f( cos( curAngle ), 1.0f + offset );
glVertex2f( cos( nxtAngle ), 1.0f + offset );
glVertex2f( cos( nxtAngle ), sin( nxtAngle ) );
}
glEnd();
glPopMatrix();
}
I can adjust the "offset" variable to make different looking arches, however in this application, I choose 0.5 to make it look the way I wanted it to!
Generate the top half of a circle and an offset above it and link the two with quads/triangles:
#include <GL/glut.h>
#include <cmath>
void glShape( const float height, unsigned int segs )
{
glBegin( GL_QUADS );
for( unsigned int i = 0; i < segs; ++i )
{
float curAngle = ( ( i + 0 ) / (float)segs ) * 3.14159;
float nxtAngle = ( ( i + 1 ) / (float)segs ) * 3.14159;
glVertex2f( cos( curAngle ), sin( curAngle ) );
glVertex2f( cos( curAngle ), 1 + height );
glVertex2f( cos( nxtAngle ), 1 + height );
glVertex2f( cos( nxtAngle ), sin( nxtAngle ) );
}
glEnd();
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 0, 0 );
glShape( 0.1f, 20 );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
Probably not the minimal number of quads/triangles but it's quick and easy :)
Related
In this post the rectangle moved via mouse. I want to add a triangle and move like rectangle via mouse.
The triangle function like this:
void drawTriangle(float x,float y,float size){
glPushMatrix();
glTranslatef( x, y, 0.0f );
glScalef( size, size, 1.0f );
glBegin( GL_TRIANGLES );
glColor3ub( 255, 255, 255 );
glVertex2f( -1, 1 );
glVertex2f( 1, -1 );
glVertex2f( 1, 1 );
glEnd();
glPopMatrix();
Rectangle and triangle moved together. But I want to move it different. So what is my wrong?
You'll have to maintain an array of Shape objects and test each one for mouse collisions, as well as keep track of which Shape you're dragging:
#include <GL/glut.h>
#include <vector>
using namespace std;
struct Shape
{
float mX, mY;
float mSize;
bool mIsRectangle;
bool PointInside( const float x, const float y ) const
{
return
mX - mSize <= x && x <= mX + mSize
&&
mY - mSize <= y && y <= mY + mSize;
}
};
vector< Shape > objects;
Shape* dragging = NULL;
void mouse( int button, int state, int x, int y )
{
if( GLUT_DOWN == state )
{
dragging = NULL;
for( Shape& obj : objects )
{
if( obj.PointInside( x, y ) )
{
dragging = &obj;
glutPostRedisplay();
break;
}
}
}
else
{
dragging = NULL;
}
}
void motion( int x, int y )
{
if( dragging )
{
dragging->mX = x;
dragging->mY = y;
glutPostRedisplay();
}
}
void drawRect( float x, float y, float size )
{
glPushMatrix();
glTranslatef( x, y, 0.0f );
glScalef( size, size, 1.0f );
glBegin( GL_QUADS );
glColor3ub( 255, 255, 255 );
glVertex2f( -1, -1 );
glVertex2f( 1, -1 );
glVertex2f( 1, 1 );
glVertex2f( -1, 1 );
glEnd();
glPopMatrix();
}
void drawTriangle( float x, float y, float size )
{
glPushMatrix();
glTranslatef( x, y, 0.0f );
glScalef( size, size, 1.0f );
glBegin( GL_TRIANGLES );
glColor3ub( 255, 255, 255 );
glVertex2f( -1, 1 );
glVertex2f( 1, -1 );
glVertex2f( 1, 1 );
glEnd();
glPopMatrix();
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, h, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
for( const Shape& obj : objects )
{
if( obj.mIsRectangle )
drawRect( obj.mX, obj.mY, obj.mSize );
else
drawTriangle( obj.mX, obj.mY, obj.mSize );
}
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMouseFunc( mouse );
glutMotionFunc( motion );
Shape temp;
temp.mSize = 50;
temp.mX = temp.mY = 100;
temp.mIsRectangle = true;
objects.push_back( temp );
temp.mX = temp.mY = 200;
temp.mIsRectangle = false;
objects.push_back( temp );
glutMainLoop();
return 0;
}
I got some problem with OpenGL matrix system.
I made five instances like below. I want to get their individual working.
C_IMGMGR cImgmgr1( 200 , 170 );
C_IMGMGR cImgmgr2( 200 , 170 );
C_IMGMGR cImgmgr3( 200 , 170 );
C_IMGMGR cImgmgr4( 200 , 170 );
C_IMGMGR cImgmgr5( 200 , 170 );
this is main,
int main( int argc, char** argv )
{
int nWinSizeX = 1280;
int nWinSizeY = 768;
glutInit( &argc , argv );
mainWindow = CreateMain( nWinSizeX , nWinSizeY );
SetProjection( nWinSizeX , nWinSizeY );
SetStateSettings();
NowFrameMove();
return 1;
}
and SetProjection()
void SetProjection( int nWinSizeX , int nWinSizeY )
{
cImgmgr1.Init_Viewport( 540 , 599 , nWinSizeX , nWinSizeY );
cImgmgr2.Init_Viewport( 40 , 299 , nWinSizeX , nWinSizeY );
cImgmgr3.Init_Viewport( 540 , 299 , nWinSizeX , nWinSizeY );
cImgmgr4.Init_Viewport( 1040 , 299 , nWinSizeX , nWinSizeY );
cImgmgr5.Init_Viewport( 540 , -1 , nWinSizeX , nWinSizeY );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0 , 1280 , 0.0 , 768 , -10000.0 , 10000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( (200 / 2) , (170 / 2) , 0 );
}
and SetStateSetting()
void SetStateSettings()
{
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_TEXTURE_2D );
cImgmgr1.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
cImgmgr2.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
cImgmgr3.Init_Sprite( "data\\Image\\symbolTemp_Pay\\" );
cImgmgr4.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
cImgmgr5.Init_Sprite( "data\\Image\\symbol01_Pay\\" );
}
here is Update() for glutDisplayFunc() and glutIdleFunc(),
void Update()
{
//////////////////////////////////////////////////////////////////////////
//Process
cImgmgr1.Process( FLIP_VERTICAL );
cImgmgr2.Process( FLIP_HORIZONTAL );
cImgmgr3.Process( NORMAL );
cImgmgr4.Process( NORMAL );
cImgmgr5.Process( NORMAL );
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//Render
glClearColor( 0.0F , 0.0F , 0.0F , 0.0F );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearDepth( 1.0 );
cImgmgr1.Render_Sprite( cImgmgr1.Get_Frame() );
cImgmgr2.Render_Sprite( cImgmgr2.Get_Frame() );
cImgmgr3.Render_Sprite( cImgmgr3.Get_Frame() );
cImgmgr4.Render_Sprite( cImgmgr4.Get_Frame() );
cImgmgr5.Render_Sprite( cImgmgr5.Get_Frame() );
//////////////////////////////////////////////////////////////////////////
glutSwapBuffers();
}
and IMGMGR class,
void C_IMGMGR::Process( E_RENDER_MODE eMode )
{
if( m_nFrame >= m_nFileCount )
m_nFrame = 0;
switch ( eMode )
{
case NORMAL :
break;
case FLIP_VERTICAL :
m_fRotationAngle = 1.0F;
m_fRotationX = 1.0F;
break;
case FLIP_HORIZONTAL :
m_fRotationAngle = 1.0F;
m_fRotationY = 1.0F;
break;
case FAN_LEFT :
m_fRotationAngle = 1.0F;
m_fRotationZ = 1.0F;
break;
case FAN_RIGHT :
m_fRotationAngle = 1.0F;
m_fRotationZ = -1.0F;
break;
case TRANSLATE_VERTICAL :
break;
case TRANSLATE_HORIZONTAL :
break;
}
}
void C_IMGMGR::Render_Sprite( int nFrame )
{
glBindTexture( GL_TEXTURE_2D , m_pLoadedSprite[nFrame] );
glViewport( m_nViewPortX , m_nViewPortY , m_nScreenW , m_nScreenH );
glPushMatrix();
glRotatef( m_fRotationAngle , m_fRotationX , m_fRotationY , m_fRotationZ );
glTranslatef( m_fTranslateX , m_fTranslateY , m_fTranslateZ );
glScalef( m_fScaleX , m_fScaleY , m_fScaleZ );
glBegin( GL_QUADS );
glTexCoord2f( 0.0F , 1.0F); glVertex2f( -(float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 1.0F); glVertex2f( (float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 0.0F); glVertex2f( (float)m_nImgX_half , (float)m_nImgY_half);
glTexCoord2f( 0.0F , 0.0F); glVertex2f( -(float)m_nImgX_half , (float)m_nImgY_half);
glEnd();
glPopMatrix();
}
they rotating together that looks like sharing FLIP_VERTICAL and FLIP_HORIZONTAL.
so, how can I separate their matrix for individual instance working?
There are a couple of issues with the posted code:
OpenGL operates as a state machine. When you make a draw call, it uses the state set at the time of the draw call. Therefore, the transformations need to be specified before the draw call if they should be applied to a draw call.
Related, calls like glRotatef() and glTranslatef() modify the current transformation matrix. Unless you do something about it, each of these calls combines the specified transformation with the previous current transformation. If you simply make a few of these calls for each draw call, all the transformations will be combined.
To fix these two problems, you need to change the order of the calls, and use glPushMatrix() and glPopMatrix() to save/restore the previous transformation when you apply the transformations for a specific draw call. The code will then look like this:
glPushMatrix();
glRotatef( m_fRotationAngle , m_fRotationX , m_fRotationY , m_fRotationZ );
glTranslatef( m_fTranslateX , m_fTranslateY , m_fTranslateZ );
glScalef( m_fScaleX , m_fScaleY , m_fScaleZ );
glBegin( GL_QUADS );
glTexCoord2f( 0.0F , 1.0F); glVertex2f( -(float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 1.0F); glVertex2f( (float)m_nImgX_half , -(float)m_nImgY_half);
glTexCoord2f( 1.0F , 0.0F); glVertex2f( (float)m_nImgX_half , (float)m_nImgY_half);
glTexCoord2f( 0.0F , 0.0F); glVertex2f( -(float)m_nImgX_half , (float)m_nImgY_half);
glEnd();
glPopMatrix();
I hope you are already aware that almost all of the OpenGL calls used in this code are deprecated, and not available anymore in modern versions of OpenGL. If you want to learn current OpenGL, look for tutorials for the OpenGL Core Profile, or for OpenGL ES 2.0 or later.
I am using the following code to implement the "zoom to mouse point" functionality in opengl in c++. Most of the code is from
OpenGL Google maps style 2D camera / zoom to mouse cursor but I had to change some include statements because of changes in the GLM (http://glm.g-truc.net/0.9.5/index.html) codebase.
#include <GL/freeglut.h>
#include <iostream>
using namespace std;
#define GLM_SWIZZLE_XYZW
#define GLM_FORCE_RADIANS
#include "glm/glm.hpp"
#include "glm/detail/setup.hpp"
#include "glm/detail/_swizzle.hpp"
#include "glm/detail/_swizzle_func.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
glm::dvec3 Unproject( const glm::dvec3& win )
{
glm::ivec4 view;
glm::dmat4 proj, model;
glGetDoublev( GL_MODELVIEW_MATRIX, glm::value_ptr( model ) );
glGetDoublev( GL_PROJECTION_MATRIX, glm::value_ptr( proj ) );
glGetIntegerv( GL_VIEWPORT, glm::value_ptr( view ) );
glm::dvec3 world = glm::unProject( win, model, proj, view );
return world;
}
// unprojects the given window point
// and finds the ray intersection with the Z=0 plane
glm::dvec2 PlaneUnproject( const glm::dvec2& win )
{
glm::dvec3 world1 = Unproject( glm::dvec3( win, 0.01 ) );
glm::dvec3 world2 = Unproject( glm::dvec3( win, 0.99 ) );
// u is a value such that:
// 0 = world1.z + u * ( world2.z - world1.z )
double u = -world1.z / ( world2.z - world1.z );
// clamp u to reasonable values
if( u < 0 ) u = 0;
if( u > 1 ) u = 1;
return glm::swizzle< glm::X, glm::Y >( world1 + u * ( world2 - world1 ) );
}
// pixels per unit
const double ppu = 80.0;
glm::dvec2 center( 0 );
double scale = 1.0;
void ApplyCamera()
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH ) / ppu;
const double h = glutGet( GLUT_WINDOW_HEIGHT ) / ppu;
glOrtho( -w/2, w/2, -h/2, h/2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glScaled( scale, scale, 1.0 );
glTranslated( -center[0], -center[1], 0 );
}
glm::dvec2 centerStart( 0 );
int btn = -1;
void mouse( int button, int state, int x, int y )
{
ApplyCamera();
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
btn = button;
if( GLUT_LEFT_BUTTON == btn && GLUT_DOWN == state )
{
centerStart = PlaneUnproject( glm::dvec2( x, y ) );
}
if( GLUT_LEFT_BUTTON == btn && GLUT_UP == state )
{
btn = -1;
}
glutPostRedisplay();
}
void motion( int x, int y )
{
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
if( GLUT_LEFT_BUTTON == btn )
{
ApplyCamera();
glm::dvec2 cur = PlaneUnproject( glm::dvec2( x, y ) );
center += ( centerStart - cur );
}
glutPostRedisplay();
}
void wheel( int wheel, int direction, int x, int y )
{
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
ApplyCamera();
glm::dvec2 beforeZoom = PlaneUnproject( glm::dvec2( x, y ) );
const double scaleFactor = 0.90;
if( direction == -1 ) scale *= scaleFactor;
if( direction == 1 ) scale /= scaleFactor;
ApplyCamera();
glm::dvec2 afterZoom = PlaneUnproject( glm::dvec2( x, y ) );
center += ( beforeZoom - afterZoom );
glutPostRedisplay();
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
ApplyCamera();
glBegin( GL_QUADS );
glVertex2i( -1, -1 );
glVertex2i( 1, -1 );
glVertex2i( 1, 1 );
glVertex2i( -1, 1 );
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "GLUT" );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glutMouseWheelFunc( wheel );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
The errors
error: 'swizzle' is not a member of 'glm'
error: 'X' is not a member of 'glm'
error: 'Y' is not a member of 'glm'
are in line
return glm::swizzle< glm::X, glm::Y >( world1 + u * ( world2 - world1 ) );
I know that glm::swizzle is obviously wrong but glm::detail::_swizzle requires 8 parameters compared to 2 earlier by glm::swizzle. What would be the new parameters be? Also, I don't know how to correct glm::X and glm::Y. Can someone please help?
Actually you don't need swizzling at all in this case since there's a dvec2 constructor that can slice off the z coordinate for you.
Change the last line of PlaneUnproject() from this:
return glm::swizzle< glm::X, glm::Y >( world1 + u * ( world2 - world1 ) );
to this:
return glm::dvec2( world1 + u * ( world2 - world1 ) );
I updated the complete code in my original answer.
Though if you really want to swizzle:
Pare down your #includes:
#define GLM_SWIZZLE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
And change the last line of PlaneUnproject() from this:
return glm::swizzle< glm::X, glm::Y >( world1 + u * ( world2 - world1 ) );
to this:
return ( world1 + u * ( world2 - world1 ) ).xy();
In newer version of glm if you want swizzle operator you need to define GLM_FORCE_SWIZZLE before including glm. GLM_SWIZZLE is deprecated.
#define GLM_FORCE_SWIZZLE
#include "glm/glm.hpp"
Then you can use it like this:
return world1.yx();
And on some compiler you can also do it like this:
return world1.yx;
Warning: Just be aware that there is a huge performance impact if you enable it.
On Visual Studio 2013 I measure around x150 speed difference in runtime matrix multiplication.
With swizzle operations: 450 000 multiplications per second.
Without swizzle operations: 65 000 000 multiplications per second.
I believe it's pretty straightforward. Under the glm namespace there's no member known as 'swizzle'. Please check these two points :-
Just check if there is a nested namespace and refer it accordingly like glm::detail::swizzle
Else please check if the member name is swizzle or _swizzle.
Thanks!
how can drawing small circle in side big circle using algorthim of circle
each circle consist of 9 or 8 point and link each 4point to gather and link this with 4 under and so on
.................................................
........................................................
You need to generate the inner and outer points in one loop.
Give this a shot:
#include <GL/glut.h>
#include <cmath>
void Torus2d( float inner, float outer, unsigned int pts )
{
glBegin( GL_QUAD_STRIP );
for( unsigned int i = 0; i <= pts; ++i )
{
float angle = ( i / (float)pts ) * 3.14159f * 2.0f;
glVertex2f( inner * cos( angle ), inner * sin( angle ) );
glVertex2f( outer * cos( angle ), outer * sin( angle ) );
}
glEnd();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -4 * ar, 4 * ar, -4, 4, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 0, 0 );
Torus2d( 2, 3, 20 );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
Find below a faster (less trigono) solution in Go (can be easily translated to c++) which was inspired by Sieglord's Abode article
func Torus(cx, cy, inner, outer float64, num_segments int) {
theta := 2 * math.Pi / float64(num_segments)
c := math.Cos(theta) //precalculate the sine and cosine
s := math.Sin(theta)
t_in := 0.0
t_out := 0.0
x_in := inner //we start at angle = 0
x_out := outer //we start at angle = 0
y_in := 0.0
y_out := 0.0
gl.Begin(gl.QUAD_STRIP)
for ii := 0; ii <= num_segments; ii++ {
gl.Vertex2f(float32(x_in+cx), float32(y_in+cy)) //output vertex inner
gl.Vertex2f(float32(x_out+cx), float32(y_out+cy)) //output vertex outer
//apply the rotation matrix
t_in = x_in
t_out = x_out
x_in = c*x_in - s*y_in
x_out = c*x_out - s*y_out
y_in = s*t_in + c*y_in
y_out = s*t_out + c*y_out
}
gl.End()
}
I need to draw a Polygon iteratively. for example, I want to draw a Polygon with 8 corners. I need to draw the first line with GL_LINES and then draw the second line with the same length and an angle of 135° between them, the third line has also an angle of 135° to the second line, etc.
I want to make a loop to render it but I don't know how. I have an approach, but it doesn't work properly.
the second point of line n-1 should be the first point of n and so on...
At the end, I need to get a closed Polygon. the last point of the last line should be the first point of the first line.
Use GL_LINE_LOOP, that will connect your last vertex to your first automatically:
#include <GL/glut.h>
#include <cmath>
void glPolygon( unsigned int sides )
{
if( sides < 3 ) return;
const float PI = 3.14159;
const float step = ( 2 * PI ) / static_cast< float >( sides );
glBegin( GL_LINE_LOOP );
for( unsigned int i = 0; i < sides; ++i )
{
glVertex2f( cos( i * step ), sin( i * step ) );
}
glEnd();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double ar = glutGet( GLUT_WINDOW_WIDTH ) / (double)glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 0, 0 );
glPolygon( 8 );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "Polygons" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
If you're dead-set on GL_LINES this works:
void glPolygonLines( unsigned int sides )
{
if( sides < 3 ) return;
const float PI = 3.14159f;
const float step = ( 2 * PI ) / static_cast< float >( sides );
glBegin( GL_LINES );
for( unsigned int i = 0; i < sides; ++i )
{
unsigned int cur = ( i + 0 ) % sides;
unsigned int nxt = ( i + 1 ) % sides;
glVertex2f( cos( cur * step ), sin( cur * step ) );
glVertex2f( cos( nxt * step ), sin( nxt * step ) );
}
glEnd();
}