dir.by  
  Search  
Programming, development, testing
C++
OpenGL
Create a new OpenGL application on C++ | Library GLUT, Visual Studio
  Looked at 7263 times       Comments 3  
 Last Comment: (9 February 2025 0:57) Толковый гайд! read...       Write a comment...
 Create a new OpenGL application on C++ | Library GLUT, Visual Studio 
last updated: 20 December 2024
Download an example drawing a line on OpenGL
ProjectConsoleC++.zip ...
Size: 80 kilobytes
Step 1. Open Visual Studio
If you do not have Visual Studio installed you need install Visual Studio...
Open Visual Studio 2022
or
Open Visual Studio 2019
Step 2. Let's create a new C++ console application
Choose: Console App
The following project was created:
Let's run the program:
Let's see the result:
Step 3. Download the GLUT library as zip
GLUT is a library.
GLUT creates a window in which OpenGL graphics are drawn;
GLUT The library receives events from the keyboard and mouse

Download the GLUT library:
Option 1. Download zip directly
Option 2. Download zip from the official website opengl.com
glutdlls37beta.zip ...
Size: 130 kilobytes
In Google Chrome open www.opengl.org/resources/libraries/glut/glut_downloads.php

Click on glutdlls37beta.zip

 
Inside glutdlls37beta.zip are the following files:
Step 4. Add the GLUT files to the C++ project
My C++ console project is located in the folder:
D:\ProjectConsoleC++

Copy the GULT files inside the C++ console project:
glut32.lib
D:\ProjectConsoleC++\lib\glut32.lib
Note! I didn't have a folder lib and I created it myself
 
glut.h
D:\ProjectConsoleC++\include\glut.h
Note! I didn't have a folder include and I created it myself
 
glut32.dll
D:\ProjectConsoleC++\Debug\glut32.dll
On a note! It is also necessary
Add lib\glut32.lib inside Visual Studio like this:
Properties → Linker → Input → Additional Dependences → lib\glut32.lib
Step 5. Let's add C++ code to draw the line
  File ProjectConsoleC++.cpp
#include "include/glut.h"
#include <iostream>

void MyDrawFunction();

int main(int argc, char** argv)
{
     glutInit(&argc, argv);
     glutInitWindowSize(640, 480);
     glutInitWindowPosition(10, 10);
     glutCreateWindow("My OpenGL application");
    
     glClearColor(1.0, 1.0, 1.0, 0.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluOrtho2D(0.0, 640.0, 0.0, 480.0);

     glutDisplayFunc(MyDrawFunction);

     glutMainLoop();
     return 0;
}

void MyDrawFunction()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0, 0.0, 0.0);
     glPointSize(2.0);

     // green color
     glColor3ub(0, 255, 0);

     // draw line (by pixel)
     for (int i = 100; i < 300; i++)
     {
          glBegin(GL_POINTS);
          glVertex2i(i, i); // x=i, y=i
          glEnd();
     }

     glFlush();
}
Step 6. Let's run the project and see the drawing of the line OpenGL
 
Let's see the result:
More OpenGL application
 
← Previous topic
Pass a function as a parameter to a function (callback) | C++
 
Next topic →
Creating a OpenGL app with 3D pyramid drawing and a motion camera on C++ | Visual Studio | Library glut for using OpenGL
 
Your feedback ... 2 Comments
guest
19 January 2025 12:07
Не работает вообще начиная с Шаг 4. Добавим файлы GLUT в C++ проект, так как архиве тупо нет h файла!.
admin (20 January 2025 10:20) Вы правы, добавил glut.h в glutdlls37beta.zip ... answer
guest
9 February 2025 0:57
Толковый гайд!
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
dynamic_cast in C++ (converting a pointer to a different type and checking validity in runtime)
std::map<Key, Value> is a set of keys and values in C++. An important feature of std::map is to quickly find a value by key
Pass a function as a parameter to a function (callback) | C++
OpenGL
Create a new OpenGL application on C++ | Library GLUT, Visual Studio
Creating a OpenGL app with 3D pyramid drawing and a motion camera on C++ | Visual Studio | Library glut for using OpenGL
Create a new OpenGL ES2 app on C++ in Windows | Visual Studio, Desktop application

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика