You are on page 1of 3

Royal University of Phnom Penh CG: Lab 2

Faculty of Engineering Lecture: Kor Sokchea

Lab 2: Drawing axis and Sine Wave


In lab 1, you are introduced about OpenGL and know how OpenGL works. You have drawn a
simple primitive such as triangle and circle, providing the color for those objects. In this lab, we
will draw a simple axis of Cartesian coordinate system with two axes that is perpendicular each
other. In order to construct the axis, we just draw one horizontal line and vertical line and placed
them perpendicular. How can we draw the head of the axis (arrow sign) and x-axis and y-axis
labels? In this example, we will just output the arrow sign character to create the head of the axis
To handle fonts in OpenGL, GLUT provide a function call glutBitmapCharacter to renders a
bitmap character.
Syntax:

void glutBitmapCharacter(void *font, int character)

font: Bitmap font to use.


character : Character to render (not confined to 8 bits)

Font types:
GLUT_BITMAP_8_BY_13

GLUT_BITMAP_9_BY_15

GLUT_BITMAP_TIMES_ROMAN_10

GLUT_BITMAP_TIMES_ROMAN_24

GLUT_BITMAP_HELVETICA_10

GLUT_BITMAP_HELVETICA_12

GLUT_BITMAP_HELVETICA_12

To specify the position of the character you want to render on the screen

//change at the end to f or s or d to specify using float, short, or double respectively


void glRasterPos2i(GLint x, GLint y);
void glRasterPos3i(GLint x, GLint y, GLint z);
Royal University of Phnom Penh CG: Lab 2
Faculty of Engineering Lecture: Kor Sokchea

OpenGL Example
/*********************************************************

Author: Kor Sokchea


Faculty of Engineering - Royal University of Phnom Penh

**********************************************************/
#include <GL/glut.h>
#include <stdio.h>

void renderString ( int pos1 , int pos2 , const char *str)


{
int strLength, i;
glRasterPos2i(pos1 , pos2);
strLength = (int) strlen(str) ;
for (i =0; i<strLength ; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
}
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);

//drawing > for y-axis


outputChar(171, 298, "-");

//drawing > for x-axis


outputChar(300, 172, ">");

glBegin(GL_LINES);
// draw y-axis
glVertex2i(175, 50);
glVertex2i(175, 300);
// draw x-axis
glVertex2i(50, 175);
glVertex2i(300, 175);
glEnd();

glFlush();
}
Royal University of Phnom Penh CG: Lab 2
Faculty of Engineering Lecture: Kor Sokchea

void initialization()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glLineWidth(2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 350.0, 0, 350.0);
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello CG");
initialization();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Bronze Challenge: Draw a unit on the axis and display number

Silver Challenge: Plot sin wave with the following formula

( + )
: Amplitude
: Frequency
: Phase

Gold Challenge: Automatically scale the unit in order to fit the data

Deadline: 26th, February, 2017

You might also like