Hi,
I’m a newbie at coding in openGL. Can someone please take a look at this code and tell me what I’m doing wrong? I’ve been playing with it and moving things around. So far, it’s only the triangle shape that is showing when I compile it. The line, circle and arc are not showing. Thanks.
#include <Windows.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
GLsizei winWidth = 400, winHeight = 300;
const float PI = 3.14159265f;
const float Radius = 4;
const float numPoints = 24;
float t, dt; /* angle /
int n = 30; / # of segments */
int i;
float x = 2;
float y = 2;
float r = 2;
float t0 = 30;
float sweep = 40;
void drawCircle( float Radius, int numPoints )
{
glBegin( GL_LINE_STRIP );
for( int i=0; i<numPoints; i++ )
{
float Angle = i * (2.0*PI/numPoints);
float X = cos( Angle )*Radius;
float Y = sin( Angle )*Radius;
glVertex2f( X, Y );
}
glEnd();
}
void drawTriangle()
{
glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 1, 0);
glEnd();
}
void drawArc(float x, float y, float r, float t0, float sweep)
{
t = t0 * PI/180.0; /* radians */
dt = sweep * PI/(180*n); /* increment */
glBegin(GL_LINE_STRIP);
glColor3f(0.1, 0.2, 0.3);
for(i=0; i<=n; i++, t += dt)
glVertex2f(x + r*cos(t), y + r*sin(t));
glEnd();
}
void DrawLine( )
{
glBegin(GL_LINES);
glVertex2i( 0, 0);
glVertex2i( 10, 10);
glEnd();
}
void drawFace()
{
drawArc(x, y, r, t0, sweep);
drawCircle( Radius, numPoints );
}
void init(void)
{
glClearColor (0.0, 0.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, winWidth, 0.0, winHeight);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2i(50, 50);
glVertex2i(100, 50);
glVertex2i(75, 100);
glEnd();
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex2i( 0, 0);
glVertex2i( 10, 10);
glEnd();
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_LINE_STRIP);
glColor3f(0.1, 0.2, 0.3);
for(i=0; i<=n; i++, t += dt)
glVertex2f(x + r*cos(t), y + r*sin(t));
glEnd();
glBegin( GL_LINE_STRIP );
for( int i=0; i<numPoints; i++ )
{
float Angle = i * (2.0*PI/numPoints);
float X = cos( Angle )*Radius;
float Y = sin( Angle )*Radius;
glVertex2f( X, Y );
}
glEnd();
glFlush(); /* Single buffered, so needs a flush. */
}
int main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (350, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow (“Facial Expression Program”);
init();
glutDisplayFunc (display);
glutMainLoop ( );
}