CS0045 Module 3

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/58

encourage image

There's no tags or description

Looks like no tags are added yet.

59 Terms

1
New cards

These functions are essential for rendering bitmap or stroke-based text in OpenGL, particularly when using the GLUT toolkit

Text & Raster Positioning Function

2
New cards

They operate within the rasterization stage of the graphics pipeline, where pixel-based operations are performed

Text & Raster Positioning Function

3
New cards

Purpose: Sets the current raster position in 2D space

Text & Raster Positioning Function

4
New cards

Parameters: x, y: Floating-point coordinates in object space

Text & Raster Positioning Function

5
New cards

Usage Context:

  • Defines where bitmap text or pixel images will be drawn

  • Transformed by the current model view and projection matrices


Text & Raster Positioning Function

6
New cards

Sets the raster position in 2D space for pixel-based operations such as bitmap text or image rendering

glRasterPos2f()

7
New cards

Coordinates are specified in the current model view and projection context

glRasterPos2f()

8
New cards

Renders a string of bitmap characters at the current raster position using a specified GLUT bitmap font

glutBitmapString()

9
New cards

Commonly used for simple on-screen text in legacy OpenGL

glutBitmapString()

10
New cards

drawBitmapString() is an extension of freeglut, therefore you need to add this to your code

#include <GL/glut.h>
#include <GL/freeglut_ext.h>


11
New cards
<pre><code class="language-cpp">#include &lt;GL/glut.h&gt;
#include &lt;GL/freeglut_ext.h&gt;

using namespace std;

void textSample(){
    glClear(GL_COLOR_BUFFER_BIT);
    
    const unsigned char str[100] = {"FEU-TECH"};
    glColor3f(1.0f, 1.0f, 1.0f);
    glRasterPos2f(0.0, 0.0);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, str);

    glFlush();
}

int main(int argc, char** argv){
    glutInit(&amp;argc, argv);
    glutCreateWindow("Line Strip");
    glutDisplayFunc(textSample);

    glutMainLoop();

return 0;
}</code></pre><p></p>
#include <GL/glut.h>
#include <GL/freeglut_ext.h>

using namespace std;

void textSample(){
    glClear(GL_COLOR_BUFFER_BIT);
    
    const unsigned char str[100] = {"FEU-TECH"};
    glColor3f(1.0f, 1.0f, 1.0f);
    glRasterPos2f(0.0, 0.0);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, str);

    glFlush();
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutCreateWindow("Line Strip");
    glutDisplayFunc(textSample);

    glutMainLoop();

return 0;
}


Drawing Text (*Note: Set the color before the position)

12
New cards
glColor3f(0.0f, 0.0f, 1.0f);


Blue

13
New cards
glColor3f(1.0f, 0.0f, 0.0f);


Red

14
New cards
glColor3f(0.0f, 1.0f, 0.0f);


Green

15
New cards
glColor3f(1.0f, 1.0f, 1.0f);


White

16
New cards
glColor3f(1.0f, 1.0f, 0.0f);


Yellow

17
New cards
glColor3f(1.0f, 0.0f, 1.0f)


Magenta

18
New cards
GLUT_STROKE_ROMAN
GLUT_STROKE_MONO_ROMAN
GLUT_BITMAP_9_BY_15
GLUT_BITMAP_8_BY_13
GLUT_BITMAP_TIMES_ROMAN_10
GLUT_BITMAP_TIMES_ROMAN_24
GLUT_BITMAP_HELVETICA_10
GLUT_BITMAP_HELVETICA_12
GLUT_BITMAP_HELVETICA_18


Available GLUT Font Constants

19
New cards

A GLUT stroke font rendered using line segments, allowing scalable vector-based text. Suitable for zoomable or transformable text in 3D scenes

GLUT_STROKE_ROMAN

20
New cards

A fixed-size bitmap font in Helvetica style with 12-point height. Used for rendering small, readable text in 2D overlays or HUDs

GLUT_BITMAP_HELVETICA_12

21
New cards

Refer to the foundational steps required to create and prepare a graphical window for rendering OpenGL content

Window Initialization & Configuration

22
New cards

This process is typically handled using the GLUT (OpenGL Utility Toolkit), which abstracts platform-specific windowing operations

Window Initialization & Configuration

23
New cards

Key Components:

glutInit()
glutInitWindowSize(int width, int height)
glutInitWindowPosition(int x, int y)
glutCreateWindow(const char *title)


Window Initialization & Configuration

24
New cards

Should only be called once in a GLUT program

glutInit()


25
New cards

Used to initialize the GLUT library

glutInit()


26
New cards

Sets the window size and position respectively

glutInitWindowSize()
glutInitWindowPosition()


27
New cards

Usage:

glutInitWindowSize(int width, int height);
glutInitWindowPosition(int x, int y);


width

Width in pixels

height

Height in pixels

x Window X location in pixels
y Window Y location in pixels


28
New cards

Creates a top-level window

glutCreateWindow()


29
New cards

Usage:

int glutCreateWindow(char *name);
name //ASCII character string for use as window name


glutCreateWindow()


30
New cards

Requests that current window be made full screen

glutFullScreen()


31
New cards

Usage:

void glutFullScreen(void);


glutFullScreen()


32
New cards

Marks the current window as needing to be redisplayed

glutPostRedisplay()


33
New cards

Most of the time used in callbacks

glutPostRedisplay()


34
New cards

Usage:

void glutPostRedisplay(void);


glutPostRedisplay()


35
New cards
36
New cards

Enable interactive graphics applications by capturing and responding to user-generated events such as keyboard presses, mouse movements, and clicks

Input Handling Functions

37
New cards

In GLUT, these functions are registered as callback handlers, allowing the application to define custom behavior for each input type

Input Handling Functions

38
New cards

Sets the display callback for the current window

glutDisplayFunc()


39
New cards

Usage:

void glutDisplayFunc(void (*func) (void));
func //The new display callback function


glutDisplayFunc()


40
New cards

Sets the keyboard callback for the current window

glutKeyboardFunc()


41
New cards

Usage:

void glutKeyboardFunc(void (*func) (unsigned char key, int x, int y));


glutKeyboardFunc()


42
New cards
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;GL/glut.h&gt;
#include &lt;GL/freeglut_ext.h&gt;

using namespace std;

void defaultDisplay();
void keyboardMonitor(unsigned char key, int x, int y);

float px, py;

int main(int argc, char** argv){
    glutInit(&amp;argc, argv);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(200,50);

    glutCreateWindow("My First OpenGL");
    //glutFullScreen();
    glutDisplayFunc(defaultDisplay);
    glutKeyboardFunc(keyboardMonitor);

    glutMainLoop();

return 0;
}

void keyboardMonitor(unsigned char key, int x, int y){
    cout &lt;&lt; key &lt;&lt; endl;
    switch(key) {
        case 'a':
            px -= .02f;
            break;
        case 'd':
            px += .02f;
            break;

        default:
        px=0.0f;
    }
    glutPostRedisplay();
}

void defaultDisplay(){
    glClear(GL_COLOR_BUFFER_BIT);

    const unsigned char str[100] = "Tony Tony Chopper";
    glColor3f(1.0f,0.75f,0.8f);
    glRasterPos2f(px,py);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, str);


    glFlush();
}</code></pre><p></p>
#include <iostream>
#include <GL/glut.h>
#include <GL/freeglut_ext.h>

using namespace std;

void defaultDisplay();
void keyboardMonitor(unsigned char key, int x, int y);

float px, py;

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(200,50);

    glutCreateWindow("My First OpenGL");
    //glutFullScreen();
    glutDisplayFunc(defaultDisplay);
    glutKeyboardFunc(keyboardMonitor);

    glutMainLoop();

return 0;
}

void keyboardMonitor(unsigned char key, int x, int y){
    cout << key << endl;
    switch(key) {
        case 'a':
            px -= .02f;
            break;
        case 'd':
            px += .02f;
            break;

        default:
        px=0.0f;
    }
    glutPostRedisplay();
}

void defaultDisplay(){
    glClear(GL_COLOR_BUFFER_BIT);

    const unsigned char str[100] = "Tony Tony Chopper";
    glColor3f(1.0f,0.75f,0.8f);
    glRasterPos2f(px,py);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, str);


    glFlush();
}


glutKeyboardFunc() Sample Code

<p>glutKeyboardFunc() Sample Code</p>
43
New cards

Sets the mouse callback for the current window

glutMouseFunc()


44
New cards

Usage:

void glutMouseFunc(void (*func) (int button, int state, int x, int y));
func //The new mouse callback funtion


glutMouseFunc()


45
New cards

When a user presses and releases mouse buttons in the window, each press and each release generates a mouse callback

glutMouseFunc()


46
New cards

The button parameter is one of GLUT LEFT BUTTON, GLUT MIDDLE BUTTON, or GLUT RIGHT BUTTON

glutMouseFunc()


47
New cards

The state parameter is either GLUT UP or GLUT DOWN

glutMouseFunc()


48
New cards
<pre><code class="language-cpp">void mouseMonitor(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON) {
        if ( state == GLUT_DOWN)
            cout &lt;&lt; "Left button was clicked..." &lt;&lt; endl;
        else
            cout &lt;&lt; "Left button was released..." &lt;&lt; endl;
    } else if(button == GLUT_RIGHT_BUTTON){
        cout &lt;&lt; "Right button was clicked..." &lt;&lt; endl;
    } else {
        cout &lt;&lt; "Middle button was clicked..." &lt;&lt; endl;
    }
    cout &lt;&lt; x &lt;&lt; " " &lt;&lt; y &lt;&lt; endl;
    //cout &lt;&lt; button &lt;&lt; endl;
}</code></pre><p></p>
void mouseMonitor(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON) {
        if ( state == GLUT_DOWN)
            cout << "Left button was clicked..." << endl;
        else
            cout << "Left button was released..." << endl;
    } else if(button == GLUT_RIGHT_BUTTON){
        cout << "Right button was clicked..." << endl;
    } else {
        cout << "Middle button was clicked..." << endl;
    }
    cout << x << " " << y << endl;
    //cout << button << endl;
}


glutMouseFunc()


49
New cards

The passive motion callback for a window is called when the mouse moves within the window while no mouse buttons are pressed

glutMotionFunc(), glutPassiveMotionFunc()


50
New cards

Usage:

void glutMotionFunc(void (*func) (int x, int y)); 
void glutPassiveMotionFunc(void (*func) (int x, int y)); 
func //The new motion or passive motion callback function


glutMotionFunc(), glutPassiveMotionFunc()


51
New cards

Sets the mouse enter/leave callback for the current window

glutEntryFunc()


52
New cards

Usage:

void glutEntryFunc(void (*func) (int state));
func //The new entry callback funtion


glutEntryFunc()


53
New cards

The state callback parameter is either GLUT LEFT or GLUT ENTERED depending on if the mouse pointer has last left or entered the window

glutEntryFunc()


54
New cards

Sets the global idle callback

glutIdleFunc()


55
New cards

Sets the global idle callback to be func so a GLUT program can perform background processing tasks or continuous animation when window system events are not being received

glutIdleFunc()


56
New cards

Usage:

void glutIdleFunc(void (*func) (void));


glutIdleFunc()


57
New cards

Registers a timer callback to be triggered in a specified number of milliseconds

glutTimerFunc()


58
New cards
void glutTimerFunc(unsigned int msecs, void (*func) (int value), value);
msecs //Number of milliseconds to pass before calling the callback
func // The timer callback funtion
value // Integer value to pass to the timer callback


glutTimerFunc()


59
New cards
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;GL\glut.h&gt;
#include &lt;GL\freeglut_ext.h&gt;

using namespace std;

void defaultDisplay();
void keyboardMonitor(unsigned char key, int x, int y);
void mouseMonitor(int button, int state, int x, int y);
void detectMotion(int x, int y);
void detectPassiveMotion(int x, int y);
void mouseEntryDetector(int state);
void animateText();
void animateString(int value);

float px, py;

int main(int argc, char **argv){
    glutInit(&amp;argc, argv);
    glutInitWindowSize(1024,768);
    glutInitWindowPosition(200,50);

    glutCreateWindow("My First OpenGL");
    //glutFullScreen();
    glutDisplayFunc(defaultDisplay);
    glutKeyboardFunc(keyboardMonitor);
    glutMouseFunc(mouseMonitor);
    glutMotionFunc(detectMotion);
    glutPassiveMotionFunc(detectPassiveMotion);
    glutEntryFunc(mouseEntryDetector);
    //glutIdleFunc(animateText);
    glutMainLoop();

    return 0;
}

void mouseEntryDetector(int state) {
    if (state == GLUT_LEFT){
        cout &lt;&lt; "Mouse has left the application " &lt;&lt;endl;
    } else{
        cout &lt;&lt; "Mouse has re-entered the application " &lt;&lt; endl;
    }
}
void animateString(int value) {
    if (value==1) {
        px += 0.02;
    } else{
        px -= 0.02;
    }
    glutPostRedisplay();
}

void animateText(){
    Sleep(1000);
    px += .02f;
    glutPostRedisplay();
}
    void detectMotion(int x, int y){
        cout &lt;&lt; x &lt;&lt; " " &lt;&lt; y &lt;&lt; endl;
}
    void detectPassiveMotion(int x, int y){
        cout &lt;&lt; x &lt;&lt; "P " &lt;&lt; y &lt;&lt; endl;
}

void mouseMonitor(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON) {
        if ( state == GLUT_DOWN)
            cout &lt;&lt; "Left button was clicked..." &lt;&lt; endl;
        else
            cout &lt;&lt; "Left button was released..." &lt;&lt; endl;
    } else if(button == GLUT_RIGHT_BUTTON) {
        cout &lt;&lt; "Right button was clicked..." &lt;&lt; endl;
    } else{
        cout &lt;&lt; "Middle button was clicked..." &lt;&lt; endl;
    }
        cout &lt;&lt; x &lt;&lt; " " &lt;&lt; y &lt;&lt; endl;
        //cout &lt;&lt; button &lt;&lt; endl;
}

void keyboardMonitor(unsigned char key, int x, int y){
    cout &lt;&lt; key &lt;&lt; endl;
    switch(key){
        case 'a':
            px -= .02f;
            break;
        case 'd':
            px += .02f;
            break;

        default:
            px=0.0f;
    }
    glutPostRedisplay();
}

void defaultDisplay(){
    glClear(GL_COLOR_BUFFER_BIT);

    const unsigned char str[100] = "FEU-TECH";
    glColor3f(0.0f,0.0f,1.0f);
    glRasterPos2f(px,py);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, str);

    glFlush();

    glutTimerFunc(1000,animateString,2);
}</code></pre><p></p>
#include <iostream>
#include <windows.h>
#include <GL\glut.h>
#include <GL\freeglut_ext.h>

using namespace std;

void defaultDisplay();
void keyboardMonitor(unsigned char key, int x, int y);
void mouseMonitor(int button, int state, int x, int y);
void detectMotion(int x, int y);
void detectPassiveMotion(int x, int y);
void mouseEntryDetector(int state);
void animateText();
void animateString(int value);

float px, py;

int main(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitWindowSize(1024,768);
    glutInitWindowPosition(200,50);

    glutCreateWindow("My First OpenGL");
    //glutFullScreen();
    glutDisplayFunc(defaultDisplay);
    glutKeyboardFunc(keyboardMonitor);
    glutMouseFunc(mouseMonitor);
    glutMotionFunc(detectMotion);
    glutPassiveMotionFunc(detectPassiveMotion);
    glutEntryFunc(mouseEntryDetector);
    //glutIdleFunc(animateText);
    glutMainLoop();

    return 0;
}

void mouseEntryDetector(int state) {
    if (state == GLUT_LEFT){
        cout << "Mouse has left the application " <<endl;
    } else{
        cout << "Mouse has re-entered the application " << endl;
    }
}
void animateString(int value) {
    if (value==1) {
        px += 0.02;
    } else{
        px -= 0.02;
    }
    glutPostRedisplay();
}

void animateText(){
    Sleep(1000);
    px += .02f;
    glutPostRedisplay();
}
    void detectMotion(int x, int y){
        cout << x << " " << y << endl;
}
    void detectPassiveMotion(int x, int y){
        cout << x << "P " << y << endl;
}

void mouseMonitor(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON) {
        if ( state == GLUT_DOWN)
            cout << "Left button was clicked..." << endl;
        else
            cout << "Left button was released..." << endl;
    } else if(button == GLUT_RIGHT_BUTTON) {
        cout << "Right button was clicked..." << endl;
    } else{
        cout << "Middle button was clicked..." << endl;
    }
        cout << x << " " << y << endl;
        //cout << button << endl;
}

void keyboardMonitor(unsigned char key, int x, int y){
    cout << key << endl;
    switch(key){
        case 'a':
            px -= .02f;
            break;
        case 'd':
            px += .02f;
            break;

        default:
            px=0.0f;
    }
    glutPostRedisplay();
}

void defaultDisplay(){
    glClear(GL_COLOR_BUFFER_BIT);

    const unsigned char str[100] = "FEU-TECH";
    glColor3f(0.0f,0.0f,1.0f);
    glRasterPos2f(px,py);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, str);

    glFlush();

    glutTimerFunc(1000,animateString,2);
}


Sample Program

<p>Sample Program</p>