CG Prog
CG Prog
#include<glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init()
{
glClearColor(0.7, 0.5, 0.0, 0.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Change Background Color");
init();
glutDisplayFunc(display);
glutMainLoop();
}
2. Points Example
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(20.0);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(10,10);
glVertex2i(10,290);
glVertex2i(200,150);
glVertex2i(390,290);
glVertex2i(390,10);
glEnd();
glFlush();
}
void init() {
glClearColor(0.0,0.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 399.0, 0.0, 299.0);
}
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(2.0);
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Line Types: Normal Line, Line Strip, Line Loop");
init();
glutDisplayFunc(display);
glutMainLoop();
}
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
#include <glut.h>
#include <math.h>
float x = x1;
float y = y1;
glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2i(round(x), round(y));
x += xInc;
y += yInc;
}
glEnd();
glFlush();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
dda(100, 150, 250, 300);
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(2.0);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#include <glut.h>
#include<cmath>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
bresenhamLine(100, 150, 250, 300);
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(2.0);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
init();
glutDisplayFunc(display);
glutMainLoop();
}
7. Quadrilateral
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0, 0.5, 0.5);
glVertex2i(100, 100);
glVertex2i(300, 100);
glVertex2i(250, 300);
glVertex2i(50, 300);
glEnd();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}
8. Mesh/Grid example
#include<glut.h>
void drawGrid()
{
glBegin(GL_LINES);
for (int i = 0; i <= 20; i++)
{
glVertex2i(i, 0);
glVertex2i(i, 20);
glVertex2i(0, i);
glVertex2i(20, i);
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawGrid();
glFlush();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glLineWidth(2.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 10.0, 0.0, 10.0);
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mesh/Grid Example");
glutDisplayFunc(display);
init();
glutMainLoop();
}
9. Display text
#include <glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
output(10, 300, "B.N.M Institute of Technology");
glFlush();
}
void myinit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
gluOrtho2D(0.0, 499.0, 0.0, 499.0);
}
#include <stdio.h>
#include <stdlib.h>
#include <glut.h>
point v[4] = { {0.0, 0.0, 10.0}, {0.0, 10.0, -10.0}, {-10.0, -10.0, -10.0}, {10.0,
-10.0, -10.0} };
int n;
void tetrahedron(int m) {
glColor3f(1.0, 0.0, 0.0);
divide_triangle(v[0], v[1], v[2], m);
glColor3f(0.0, 1.0, 0.0);
divide_triangle(v[3], v[2], v[1], m);
glColor3f(0.0, 0.0, 1.0);
divide_triangle(v[0], v[3], v[1], m);
glColor3f(0.0, 0.0, 0.0);
divide_triangle(v[0], v[2], v[3], m);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("3D Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
#include <glut.h>
#include <math.h>
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y);
for (i = 0; i <= triangleAmount; i++) {
glVertex2f(
x + (radius * cos(i * twicePi / triangleAmount)),
y + (radius * sin(i * twicePi / triangleAmount))
);
}
glEnd();
}
void drawSunRays(float x, float y, float radius, int numRays) {
glBegin(GL_LINES);
for (int i = 0; i < numRays; i++) {
float angle = (2.0f * 3.14159f / numRays) * i;
float x1 = x + radius * cos(angle);
float y1 = y + radius * sin(angle);
glVertex2f(x, y); // Center of the sun
glVertex2f(x1, y1); // End of the ray
}
glEnd();
}
void drawCrow(float x, float y) {
glColor3f(0.0f, 0.0f, 0.0f); // Dark color for crow
glLineWidth(2.0);
glBegin(GL_LINES);
glEnd();
}
// Boat mast
glColor3f(0.0f, 0.0f, 0.0f); // Black mast
glBegin(GL_LINES);
glVertex2f(x, y);
glVertex2f(x, y + 0.25f);
glEnd();
}
// Sky background
glColor3f(0.53f, 0.81f, 0.92f); // Light blue
glBegin(GL_QUADS);
glVertex2f(-1.0f, -0.2f);
glVertex2f(1.0f, -0.2f);
glVertex2f(1.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();
// Ground
glColor3f(0.0f, 0.5f, 0.0f); // Green
glBegin(GL_QUADS);
glVertex2f(-1.0f, -0.2f);
glVertex2f(1.0f, -0.2f);
glVertex2f(1.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glEnd();
// Water
glColor3f(0.0f, 0.5f, 1.0f); // Blue water
glBegin(GL_QUADS);
glVertex2f(-1.0f, -0.6f);
glVertex2f(1.0f, -0.6f);
glVertex2f(1.0f, -1.0f);
glVertex2f(-1.0f, -1.0f);
glEnd();
// Sun
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
drawSunRays(0.15f, 0.7f, 0.15f, 12); // 12 rays
drawCircle(0.15f, 0.7f, 0.1f);
// Mountains
glColor3f(0.6f, 0.3f, 0.1f); // Brown mountains
glBegin(GL_TRIANGLES);
glVertex2f(-1.0f, -0.2f);
glVertex2f(-0.8f, 0.3f);
glVertex2f(-0.6f, -0.2f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.2f);
glVertex2f(-0.3f, 0.35f);
glVertex2f(-0.1f, -0.2f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(0.0f, -0.2f);
glVertex2f(0.2f, 0.25f);
glVertex2f(0.4f, -0.2f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(0.5f, -0.2f);
glVertex2f(0.7f, 0.3f);
glVertex2f(0.9f, -0.2f);
glEnd();
// Draw houses
drawHouse(-0.7f, -0.2f, 0.15f, 0.8f, 0.0f, 0.0f); // Red house
drawHouse(-0.2f, -0.2f, 0.15f, 0.0f, 0.8f, 0.0f); // Green house
drawHouse(0.3f, -0.2f, 0.15f, 0.0f, 0.0f, 0.8f); // Blue house
drawHouse(0.8f, -0.2f, 0.15f, 1.0f, 0.5f, 0.f); // Yellow house
// Draw trees
drawTree(-0.9f, -0.2f, 0.15f);
drawTree(-0.4f, -0.2f, 0.15f);
drawTree(0.1f, -0.2f, 0.15f);
drawTree(0.6f, -0.2f, 0.15f);
// Refresh
glFlush();
}
// Initialize OpenGL
void init() {
glClearColor(0.53f, 0.81f, 0.92f, 1.0f); // Sky blue background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}