C++ OpenGL का परिचय
In this page:
#include <GL/glut.h>
glutInit(&argc, argv);
glutCreateWindow("title");
glutDisplayFunc(display_function);
glutMainLoop();
OpenGL क्या है?
OpenGL सीधे GPU को low-level drawing commands issue करके 2D और 3D graphics render करने के लिए एक cross-platform, cross-language API है।
Ubuntu पर आप इसके against compile करने के लिए freeglut3-dev और mesa-common-dev install करेंगे; Windows पर, GLEW और freeGLUT ज़रूरी bindings और window-management glue देते हैं।
उदाहरण: What is OpenGL?
#include <GL/glut.h>
int main(int argc, char** argv) {
glutInit(&argc, argv); // low-level GPU drawing commands via OpenGL
return 0;
}
Login to try C/C++/Java/PHP code in the editor
GLUT से एक OpenGL Window Create करना
GLUT (OpenGL Utility Toolkit) इसलिए exist करता है क्योंकि OpenGL खुद के पास एक window बनाने या operating system के event loop handle करने का कोई concept नहीं -- GLUT उस gap को भरता है, आपको बस कुछ lines setup code से एक window खोलने और rendering और input के लिए callback functions register करने देते हुए।
उदाहरण: Creating an OpenGL Window with GLUT
#include <GL/glut.h>
void display() {}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("OpenGL Window");
glutDisplayFunc(display); // register a rendering callback
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Triangles Draw करना
glBegin(GL_TRIANGLES) और glEnd() vertex calls का एक set bracket करते हैं जिन्हें OpenGL triangles में group करता है, वह fundamental building block जिसमें लगभग सारी 3D rendering break down होती है।
इनके बीच दिए coordinates normalized device space में हैं, -1.0 से 1.0 तक range करते हुए चाहे window का actual pixel size कुछ भी हो।
उदाहरण: Drawing Triangles
#include <GL/glut.h>
void drawTriangle() {
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glEnd();
}
int main() {
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Viewing Volume और Projection
एक projection transform आपकी scene के abstract coordinate space को 2D viewport पर map करता है जो actually display होता है।
Orthographic projection (flat 2D views और UI के लिए इस्तेमाल होता है) distance चाहे जो भी हो parallel lines और object size preserve करता है, perspective projection के उलट जो realistic 3D depth के लिए इस्तेमाल होता है।
उदाहरण: Viewing Volume and Projection
#include <GL/glut.h>
void setupOrtho() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 800.0, 600.0, 0.0); // orthographic: no depth distortion
}
int main() {
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Buffers और Colors Clear करना
glClearColor() set करता है कि हर नया frame draw होने से पहले screen buffer कौन-सा color reset करे; glClear(GL_COLOR_BUFFER_BIT) फिर actually वह reset apply करता है।
यह step skip करना पिछले frame के pixels पीछे छोड़ देता है, old drawing को नए frame में smear करते हुए।
उदाहरण: Clearing Buffers and Colors
#include <GL/glut.h>
void render() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); // reset the buffer before drawing
}
int main() {
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- GLUT window बनाने से पहले
glutInitcall करना भूल जाना, जो कई systems पर crash करता है। glFlush()याglutSwapBuffers()भूल जाना, इसलिए drawn shapes कभी appear नहीं होते।- default -1 से 1 range के बाहर coordinates इस्तेमाल करना और सोचना कि shapes क्यों cut off हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: