← Back to C++ Course | Chapter 18: Graphics | Lesson 6 of 7

C++ OpenGL का परिचय

OpenGL computer की graphics chip इस्तेमाल करके 2D और 3D graphics draw करने का एक tool है, एक बहुत तेज़ painter की तरह। आप shapes describe करते हैं, और यह उन्हें draw करता है।
Syntax
cpp
#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?

cpp
#include <GL/glut.h>

int main(int argc, char** argv) {
	glutInit(&argc, argv); // low-level GPU drawing commands via OpenGL
	return 0;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. GLUT window बनाने से पहले glutInit call करना भूल जाना, जो कई systems पर crash करता है।
  2. glFlush() या glutSwapBuffers() भूल जाना, इसलिए drawn shapes कभी appear नहीं होते।
  3. default -1 से 1 range के बाहर coordinates इस्तेमाल करना और सोचना कि shapes क्यों cut off हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.