Sodapdf Converted 1
Sodapdf Converted 1
Sodapdf Converted 1
h>
#include <GL/glut.h>
#include <stdio.h>
int x1, y1, x2, y2;
void myInit()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
incx = 1;
if (x2 < x1)
incx = -1;
incy = 1;
if (y2 < y1)
incy = -1;
x = x1;
y = y1;
void myDisplay()
{
draw_line(x1, x2, y1, y2);
glFlush();
}
void drawSquare()
{
glColor3f(0.0, 0.0, 0.0); // Set square color to black
glBegin(GL_QUADS);
glVertex2f(squareX, squareY);
glVertex2f(squareX + squareSize, squareY);
glVertex2f(squareX + squareSize, squareY + squareSize);
glVertex2f(squareX, squareY + squareSize);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glLoadIdentity(); // Load the identity matrix
drawSquare();
void init()
{
glEnable(GL_DEPTH_TEST); // Enable depth testing for 3D rendering
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glLoadIdentity(); // Load the identity matrix
glTranslatef(0.0f, 0.0f, -5.0f); // Translate the cube to a distance of -5 units along z-axis
glRotatef(angleX, 1.0f, 0.0f, 0.0f); // Rotate the cube around x-axis
glRotatef(angleY, 0.0f, 1.0f, 0.0f); // Rotate the cube around y-axis
glScalef(scale, scale, scale); // Scale the cube
// Draw Cube
glBegin(GL_QUADS); // Begin drawing the cube using quads
// Front face
glColor3f(1.0f, 0.0f, 0.0f); // Red color
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
// Back face
glColor3f(0.0f, 1.0f, 0.0f); // Green color
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Top face
glColor3f(0.0f, 0.0f, 1.0f); // Blue color
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Bottom face
glColor3f(1.0f, 1.0f, 0.0f); // Yellow color
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glColor3f(1.0f, 0.0f, 1.0f); // Purple color
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Left face
glColor3f(0.0f, 1.0f, 1.0f); // Cyan color
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd(); // End of drawing cube
================================================================================
4) #include<windows.h>
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Apply transformations
glTranslatef(translateX, translateY, 0.0f); // Translation
glRotatef(angle, 0.0f, 0.0f, 1.0f); // Rotation
glScalef(scaleX, scaleY, 1.0f); // Scaling
glFlush();
}
================================================================================
5) #include<windows.h>
#include <GL/glut.h>
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -6.0);
glRotatef(angle, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
// Front face
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
// Back face
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
// Top face
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
// Bottom face
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
// Right face
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
// Left face
glColor3f(0.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glEnd();
glutSwapBuffers();
}
void timer(int)
{
angle += 2.0;
glutPostRedisplay();
glutTimerFunc(1000/60, timer, 0);
}
==============================================================================
6) #include<windows.h>
#include <GL/glut.h>
float trianglePosX = -0.5f; // Initial position of the triangle
float triangleSpeed = 0.005f; // Speed of the triangle
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Draw the triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); // Red color
glVertex2f(trianglePosX, 0.0f);
glVertex2f(trianglePosX + 0.1f, 0.2f);
glVertex2f(trianglePosX + 0.2f, 0.0f);
glEnd();
glutSwapBuffers();
}
void update(int value)
{
// Update the position of the triangle
trianglePosX += triangleSpeed;
// If the triangle goes beyond the right edge of the window, reset its position
if (trianglePosX > 1.1f)
trianglePosX = -0.5f;
// Redisplay the scene
glutPostRedisplay();
// Call update() again after 16 milliseconds
glutTimerFunc(16, update, 0);
}
void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background color
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("vtucode | OpenGL Animation");
init();
glutDisplayFunc(display);
glutTimerFunc(16, update, 0); // Call update() after 16 milliseconds
glutMainLoop();
return 0;
}