Install OpenGL on Linux Ubuntu and Running it

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn Install Application on Linux
  • You will learn Runing OpenGL on Linux

Requirements

  • Linux Ubuntu
  • Notepad++/Notepadqq/Text Editor
  • C++ Language

Difficulty

  • Basic

Tutorial Contents

Well Hello People, now I wan't to tell you how to install OpenGL on linux ubuntu. But first, you must to know what is OpenGL? OpenGL is a abbreviation from Open Graphic Library, which is one API, that is a library consists of various functions and usually used to draw a 2D or 3D object. Okay guys follow me to install it.

Open your terminal

  • sudo apt-get update

This will update your apt database to the most recent available packages.

  • sudo apt-get install build-essential

This installs the necessary development tools for building source code.

  • sudo apt-get install freeglut3-dev

This installs the development libraries and headers for freeglut.

okay you already to install it now you follow me to running it.

Open your Notepad++/Notepadqq/Text Editor

  • Copy this source code
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
void userdraw(void);
void drawDot(float x, float y)
{
    glBegin(GL_POINTS);
        glVertex2f(x,y);
    glEnd();
}
void drawLine (float x1, float y1, float x2, float y2)
{
    glBegin(GL_LINES);
        glVertex2f(x1,y1);
        glVertex2f(x2,y2);
        glEnd();
}
void setColor(float red, float green, float blue)
{
    glColor3f(red,green,blue);
}
void userdraw(void)
{
    glPointSize(4);
    setColor(1.,1.,1.);
            drawDot(100,40); //point
            
                drawLine(1,1,100,100);//line
                
                    glBegin(GL_POLYGON); //polygon
                    glVertex2f(210,260);
                    glVertex2f(260,260);
                    glVertex2f(260,210);
                    glEnd();
                    
                        glBegin(GL_LINE_STRIP); //polyline
                        glVertex2f(110,160);
                        glVertex2f(110,110);
                        glVertex2f(160,110);
                        glEnd();
}               
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    userdraw();
    glFlush();
}
void main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,150);
    glutCreateWindow("My First Open Graphic Library");
    glClearColor(0.0,0.0,0.0,0.0);
    gluOrtho2D(0.,640.,0.0,480.0);
    glutDisplayFunc(display);
    glutMainLoop();
}

Explanation:

#include <GL/glut.h> // a package from OpenGL.

void userdraw(void); //Here is the place to draw.

void drawDot(float x, float y) //function to draw points based on x and y coordinates. If on therethe data type is float, on glVertex2f(x,y);. if on void drawDot(float x, float y) the data type is intiger like this void drawDot(int x, int y), you must change glVertex2f(x,y); to glVertex2i(x,y);. if on the data type is double you must change glVertex2f(x,y); to glVertex2d(x,y);.

void drawLine (float x1, float y1, float x2, float y2) //function to draw lines on your project.

void setColor(float red, float green, float blue) //color settings for dots or lines .

glPointSize(4); //to settings you dots or lines sizes.

setColor(1.,1.,1.); // to set your color.

drawDot(100,40); //the point will be at the coordinates x 100 and the y coordinates 40.

glClearColor(0.0,0.0,0.0,0.0);//for background color.

  • Save that source with name try.c
  • Open your terminal and run command for compile your program.

    gcc try.c -lGL -lGLU -lglut -lm -o utopian

-lGL -lGLU -lglut -lm to call glut package.
-o for name output your program

  • if complie success, now call your output name like this " ./utopian" and you can see your output.
    image.png

okay guys until here i told you how to Install OpenGL on Linux Ubuntu and Running it. see you on the next tutorial.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Hi, these are the reasons your contribution was rejected

  • Tutorials must be technical instructions that teach non-trivial aspects of an open-source project. First of all you don't really teach anything, except how to install OpenGL, which is too trivial. Nothing in your tutorial teaches the user how they can use OpenGL in situations outside of the specific example you give.
  • The linked repository is for cl-opengl, which is a set of bindings and utilities for accessing the OpenGL,
    GLU and GLUT APIs using CFFI. I'm pretty sure that since OpenGL itself is just a specification and not an implementation, that there isn't a repository for general OpenGL tutorials.
  • The writing style should be professional and I don't think yours is. I also think the formatting of your post (especially where you explain the code) isn't the greatest.

I recommend you improve on the above points for your future tutorials.

You can contact us on Discord.
[utopian-moderator]