This is the code of my first OpenGL project. I drew simple a house, a tree, a car, and a animation character "Sponge Bob"


#include <stdlib.h>
#include <gl/glut.h>
#include <math.h>

int Width = 600, Height = 600;

void Render();
void Reshape(int w, int h);
void Draw_Apple(float a, float b, float c);

void SetupViewTransform()
{
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0.0, 0.0, -10.0);
}

void SetupViewVolume()
{
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-10.0, 10.0, -10.0, 10.0, -100.0, 100.0);
}

int main(int argc, char **argv)
{
 glutInit(&argc, argv);

 glutInitWindowSize(Width, Height);
 glutInitWindowPosition(0, 0);

 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

 glutCreateWindow("A SimpleGL Program");

 glutDisplayFunc(Render);
 glutReshapeFunc(Reshape);

 glutMainLoop();
 return 0;
}

void Render()
{
 glClearColor(1.0f, 0.81f, 0.42f, 1.0f);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 SetupViewTransform();

 SetupViewVolume();

 //house
 glMatrixMode(GL_MODELVIEW);
 glTranslatef(5.0, 2.0, 0.0);

 glBegin(GL_TRIANGLES);
 glColor3f(1.0, 0.6, 0.6);
 glVertex3f(-2.0, 0.0, 0.0);
 glColor3f(1.0, 0.0, 0.0);
 glVertex3f(2.0, 0.0, 0.0);
 glColor3f(1.0, 0.6, 0.6);
 glVertex3f(0.0, 2.0, 0.0);
 glEnd();

 glTranslatef(0.0, -1.0, 0.0);

 glBegin(GL_QUADS);
 glColor3f(1.0, 1.0, 0.9);
 glVertex3f(-1.8, 1.0, 0.0);
 glColor3f(1.0, 1.0, 0.7);
 glVertex3f(-1.8, -2.0, 0.0);
 glVertex3f(1.8, -2.0, 0.0);
 glVertex3f(1.8, 1.0, 0.0);
 glEnd();

 //house window
 glTranslatef(0.8, 0, 0.0);

 glBegin(GL_QUADS);
 glColor3f(0.7, 1.0, 1.0);
 glVertex3f(-0.8, 0.7, 0.0);
 glColor3f(0.4, 1.0, 1.0);
 glVertex3f(-0.8, -0.7, 0.0);
 glColor3f(0.7, 1.0, 1.0);
 glVertex3f(0.8, -0.7, 0.0);
 glColor3f(0.7, 1.0, 1.0);
 glVertex3f(0.8, 0.7, 0.0);
 glEnd();


 //car
 glTranslatef(-5.8, -6.5, 0);

 glBegin(GL_POLYGON);
 glColor3f(0.3, 0.3, 1);
 glVertex3f(0.5, 0, 0);
 for (int i = 90; i <= 180; i++)
 {
  glVertex3f(1.5 * cos(i * 3.1416 / 180), sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 glColor3f(0.3, 0.3, 1);

 glBegin(GL_QUADS);
 glVertex3f(0, 0, 0);
 glVertex3f(4, 0, 0);
 glVertex3f(4, 2, 0);
 glVertex3f(0, 2, 0);
 glEnd();

 glBegin(GL_POLYGON);
 glVertex3f(4, 0, 0);
 for (int i = 0; i <= 90; i++)
 {
  glVertex3f(4 +  1.5 * cos(i *3.1416 / 180), sin(i*3.1416 / 180), 0);
 }

 glEnd();  
 
 //wheel 1
 glBegin(GL_POLYGON);
 glColor3f(0, 0, 0);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(0.6 * cos(i * 3.1416 / 180), 0.6 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 //wheel 2
 glBegin(GL_POLYGON);
 glColor3f(0, 0, 0);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(4 + 0.6 * cos(i * 3.1416 / 180), 0.6 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();


 //window of the car
 glTranslatef(0.3, 1, 0);
 glBegin(GL_QUADS);
 glColor3f(0.9, 0.9, 1);
 glVertex3f(0, 0, 0);
 glVertex3f(0, 0.8, 0);
 glVertex3f(3.4, 0.8, 0);
 glVertex3f(3.4, 0, 0);
 glEnd();

 //tree

 glTranslatef(-1, 2, 0);
 glBegin(GL_QUADS);
 glColor3f(0.65, 0.3, 0.13);
 glVertex3f(0, 0, 0);
 glVertex3f(2, 0, 0);
 glVertex3f(1.5, 6, 0);
 glVertex3f(0.5, 6, 0);
 glEnd();

 glTranslatef(1, 7, -3);
 glBegin(GL_POLYGON);
 glColor3f(0.2, 0.9, 0.15);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(2 * cos(i * 3.1416 / 180), 4 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 // apples

 Draw_Apple(-0.5, 3, 0);

 Draw_Apple(-0.5, -2, 0);

 Draw_Apple(2.1, 1, 0);
 
 Draw_Apple(0.2, -1.2, 0);
 
 Draw_Apple(-1, -1.7, 0);

 Draw_Apple(-1, -1, 0);

 //SpongeBob Body
 glTranslatef(-8, -5, 0);
 glBegin(GL_QUADS);
 glColor3f(1, 1, 0);
 glVertex3f(0, 0, 0);
 glVertex3f(4, 0, 0);
 glVertex3f(4, 5, 0);
 glVertex3f(0, 5, 0);
 glEnd();

 //Left Eye
 glColor3f(1, 1, 1);
 glBegin(GL_POLYGON);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(1.25 + 0.5 * cos(i * 3.1416 / 180), 3.75 + 0.5 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();
 glColor3f(0.357, 0.345, 0.87);
 glBegin(GL_POLYGON);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(1.25 + 0.2 * cos(i * 3.1416 / 180), 3.75 + 0.2 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();
 glColor3f(0, 0, 0);
 glBegin(GL_POLYGON);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(1.25 + 0.1 * cos(i * 3.1416 / 180), 3.75 + 0.1 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 //Right Eye
 glColor3f(1, 1, 1);
 glBegin(GL_POLYGON);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(2.75 + 0.5 * cos(i * 3.1416 / 180), 3.75 + 0.5 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();
 glColor3f(0.357, 0.345, 0.87);
 glBegin(GL_POLYGON);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(2.75 + 0.2 * cos(i * 3.1416 / 180), 3.75 + 0.2 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();
 glColor3f(0, 0, 0);
 glBegin(GL_POLYGON);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(2.75 + 0.1 * cos(i * 3.1416 / 180), 3.75 + 0.1 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 //nose
 glColor3f(0, 0, 0);
 glBegin(GL_LINE_STRIP);
 for (int i = -30; i <= 210; i++)
 {
  glVertex3f(2 + 0.25 * cos(i * 3.1416 / 180), 3 + 0.25 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 //mouth
 glBegin(GL_LINE_STRIP);
 for (int i = 180; i <= 360; i++)
 {
  glVertex3f(2 + 0.8 * cos(i * 3.1416 / 180), 2.5 + 0.4 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 //Pants
 glColor3f(0.65, 0.3, 0.13);
 glBegin(GL_QUADS);
 glVertex3f(0, 0, 0);
 glVertex3f(4, 0, 0);
 glVertex3f(4, 0.75, 0);
 glVertex3f(0, 0.75, 0);
 glEnd();

 glColor3f(0, 0, 0);
 glBegin(GL_QUADS);
 glVertex3f(0.25, 0.5, 0);
 glVertex3f(1.25, 0.5, 0);
 glVertex3f(1.25, 0.65, 0);
 glVertex3f(0.25, 0.65, 0);

 glVertex3f(1.5, 0.5, 0);
 glVertex3f(2.5, 0.5, 0);
 glVertex3f(2.5, 0.65, 0);
 glVertex3f(1.5, 0.65, 0);

 glVertex3f(2.75, 0.5, 0);
 glVertex3f(3.75, 0.5, 0);
 glVertex3f(3.75, 0.65, 0);
 glVertex3f(2.75, 0.65, 0);
 glEnd();

 //Shirt
 glColor3f(1, 1, 1);
 glBegin(GL_QUADS);
 glVertex3f(0, 0.75, 0);
 glVertex3f(4, 0.75, 0);
 glVertex3f(4, 1.5, 0);
 glVertex3f(0, 1.5, 0);
 glEnd();


 //necktie

 glColor3f(1, 0, 0);
 glBegin(GL_TRIANGLES);
 glVertex3f(1.75, 1.5, 0);
 glVertex3f(2, 1.35, 0);
 glVertex3f(2.15, 1.5, 0);
 glEnd();

 glBegin(GL_QUADS);
 glVertex3f(2, 1.35, 0);
 glVertex3f(1.75, 1, 0);
 glVertex3f(2, 0.75, 0);
 glVertex3f(2.15, 1, 0);
 glEnd();


 glutSwapBuffers();
}

void Reshape(int w, int h)
{
 glViewport(0, 0, w, h);
 Width = w;
 Height = h;
}

void Draw_Apple(float a, float b, float c)
{
 glTranslatef(a, b, c);
 glBegin(GL_POLYGON);
 glColor3f(1, 0, 0);
 for (int i = 0; i <= 360; i++)
 {
  glVertex3f(0.5 * cos(i * 3.1416 / 180), 0.5 * sin(i * 3.1416 / 180), 0);
 }
 glEnd();

 glBegin(GL_TRIANGLES);
 glColor3f(1, 1, 1);
 glVertex3f(0.25, 0.20, 1);
 glVertex3f(0.25, 0.35, 1);
 glVertex3f(-0.05, 0.32, 1);
 glEnd();
}





+ Recent posts