0% found this document useful (0 votes)
6 views24 pages

Afica Codigo

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Afica Codigo

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

5.

#include <GL/glut.h>

void bresenhamLine(int x1, int y1, int x2, int y2) {

glBegin(GL_POINTS);

glColor3f(0.0, 1.0, 0.0); // Cor verde

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int slope_one = 2 * dy;

int slope_two = 2 * (dy - dx);

int p = 2 * dy - dx;

int x, y;

if (x1 > x2) {

x = x2;

y = y2;

x2 = x1;

} else {

x = x1;
y = y1;

glVertex2i(x, y);

while (x < x2) {

x++;

if (p < 0) {

p += slope_one;

} else {

y++;

p += slope_two;

glVertex2i(x, y);

glEnd();

glFlush();

void display() {
glClear(GL_COLOR_BUFFER_BIT);

bresenhamLine(40, 200, 200, 10);

glFlush();

void init() {

glClearColor(0.0, 0.0, 0.0, 0.0);

gluOrtho2D(0, 400, 0, 400);

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 400);

glutInitWindowPosition(100, 100);

glutCreateWindow("Bresenham Line Algorithm");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

}
6.

#include <GL/glut.h>

#include <cmath>

void drawCirclePoints(int xc, int yc, int x, int y) {

glVertex2i(xc + x, yc + y);

glVertex2i(xc - x, yc + y);

glVertex2i(xc + x, yc - y);

glVertex2i(xc - x, yc - y);

glVertex2i(xc + y, yc + x);

glVertex2i(xc - y, yc + x);

glVertex2i(xc + y, yc - x);

glVertex2i(xc - y, yc - x);

void bresenhamCircle(int xc, int yc, int r) {

glBegin(GL_POINTS);

glColor3f(0.0, 0.0, 1.0); // Cor azul

int x = 0;

int y = r;

int p = 3 - 2 * r;
drawCirclePoints(xc, yc, x, y);

while (y >= x) {

x++;

if (p > 0) {

y--;

p = p + 4 * (x - y) + 10;

} else {

p = p + 4 * x + 6;

drawCirclePoints(xc, yc, x, y);

glEnd();

glFlush();

void display() {

glClear(GL_COLOR_BUFFER_BIT);

bresenhamCircle(128, 128, 50);

glFlush();
}

void init() {

glClearColor(0.0, 0.0, 0.0, 0.0);

gluOrtho2D(0, 256, 0, 256);

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(256, 256);

glutInitWindowPosition(100, 100);

glutCreateWindow("Bresenham Circle Algorithm");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

7.

#include <GL/glut.h>
void drawTriangle() {

glBegin(GL_TRIANGLES);

glColor3f(1.0, 0.0, 0.0); // Cor vermelha

// Vértices nos cantos inferiores da janela

glVertex2i(0, 0);

glVertex2i(400, 0);

// Centro da borda superior

glVertex2i(200, 400);

glEnd();

glFlush();

void display() {

glClear(GL_COLOR_BUFFER_BIT);

drawTriangle();

glFlush();

void init() {

glClearColor(1.0, 1.0, 1.0, 0.0); // Cor de fundo branca


gluOrtho2D(0, 400, 0, 400);

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 400);

glutInitWindowPosition(100, 100);

glutCreateWindow("Triangle Example");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

8.
#include <GL/glut.h>

void drawHouse() {

glBegin(GL_QUADS);

glColor3f(0.0, 0.0, 1.0); // Cor azul para as paredes

// Paredes da casa

glVertex2i(50, 50);

glVertex2i(50, 200);

glVertex2i(200, 200);

glVertex2i(200, 50);

glEnd();

glBegin(GL_TRIANGLES);

glColor3f(1.0, 0.0, 0.0); // Cor vermelha para o telhado

// Telhado da casa

glVertex2i(50, 200);

glVertex2i(125, 300);

glVertex2i(200, 200);

glEnd();
glBegin(GL_QUADS);

glColor3f(1.0, 1.0, 1.0); // Cor branca para a porta

// Porta da casa

glVertex2i(100, 50);

glVertex2i(100, 150);

glVertex2i(150, 150);

glVertex2i(150, 50);

glEnd();

// Linha azul do canto inferior direito para o canto inferior esquerdo da porta

glBegin(GL_LINES);

glColor3f(0.0, 0.0, 1.0); // Cor azul para a linha

glVertex2i(150, 50);

glVertex2i(100, 50);

glEnd();

// Definindo as dimensões da janela

int windowWidth = 25;


int windowHeight = 30;

// Calculando as coordenadas da janela

int windowLeftX = 150 + 8; // X da esquerda, afastado 8 cm para a direita

int windowRightX = windowLeftX + windowWidth; // X da direita

int windowBottomY = 50 + (200 - 50 - windowHeight) / 2; // Y inferior, centralizado


verticalmente

int windowTopY = windowBottomY + windowHeight; // Y superior

glBegin(GL_QUADS);

glColor3f(1.0, 1.0, 1.0); // Cor branca para a janela

// Janela da casa

glVertex2i(windowLeftX, windowBottomY);

glVertex2i(windowLeftX, windowTopY);

glVertex2i(windowRightX, windowTopY);

glVertex2i(windowRightX, windowBottomY);

glEnd();

glBegin(GL_LINES);

glColor3f(0.0, 0.0, 1.0); // Cor azul para a cruz na janela


// Cruz na janela

int crossCenterX = (windowLeftX + windowRightX) / 2;

int crossCenterY = (windowBottomY + windowTopY) / 2;

glVertex2i(crossCenterX, windowBottomY);

glVertex2i(crossCenterX, windowTopY);

glVertex2i(windowLeftX, crossCenterY);

glVertex2i(windowRightX, crossCenterY);

glEnd();

void display() {

glClear(GL_COLOR_BUFFER_BIT);

drawHouse();

glFlush();

void init() {

glClearColor(1.0, 1.0, 1.0, 0.0); // Cor de fundo branca

gluOrtho2D(0, 250, 0, 350);

}
int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(250, 350);

glutInitWindowPosition(100, 100);

glutCreateWindow("House Example");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

}
9.

#include <GL/glut.h>

#include <iostream>

// Variáveis globais para as dimensões da casa

float houseWidth = 150.0;

float houseHeight = 150.0;

float roofHeight = 100.0;

float doorWidth = 50.0;

float doorHeight = 100.0;

float windowWidth = 25.0;

float windowHeight = 30.0;

int windowLeftX = 0;

int windowRightX = 0;

int windowBottomY = 0;

int windowTopY = 0;

void updateWindowSize() {

// Recalcula as coordenadas da janela com base nas dimensões da casa


windowRightX = 50 + houseWidth - 8;

windowLeftX = windowRightX - windowWidth;

windowBottomY = 50 + (50 + houseHeight - 50 - windowHeight) / 2;

windowTopY = windowBottomY + windowHeight;

void updateDoorSize() {

// Redimensiona a porta proporcionalmente às novas dimensões da casa

doorWidth = houseWidth / 3; // Ajuste conforme necessário

doorHeight = houseHeight / 2; // Ajuste conforme necessário

void updateWindowSizeDynamic() {

// Redimensiona dinamicamente a janela proporcionalmente às novas dimensões da


casa

float windowWidthPercentage = 0.1; // Ajuste conforme necessário

float windowHeightPercentage = 0.15; // Ajuste conforme necessário

windowWidth = houseWidth * windowWidthPercentage;

windowHeight = houseHeight * windowHeightPercentage;

void drawHouse() {

// Calculando as coordenadas da janela


updateWindowSize();

// Redimensionando a porta

updateDoorSize();

// Redimensionando dinamicamente a janela

updateWindowSizeDynamic();

glBegin(GL_QUADS);

glColor3f(0.0, 0.0, 1.0); // Cor azul para as paredes

// Paredes da casa

glVertex2i(50, 50);

glVertex2i(50, 50 + houseHeight);

glVertex2i(50 + houseWidth, 50 + houseHeight);

glVertex2i(50 + houseWidth, 50);

glEnd();

glBegin(GL_TRIANGLES);

glColor3f(1.0, 0.0, 0.0); // Cor vermelha para o telhado

// Telhado da casa
glVertex2i(50, 50 + houseHeight);

glVertex2i(50 + houseWidth / 2, 50 + houseHeight + roofHeight);

glVertex2i(50 + houseWidth, 50 + houseHeight);

glEnd();

glBegin(GL_QUADS);

glColor3f(1.0, 1.0, 1.0); // Cor branca para a porta

// Porta da casa

glVertex2i(50 + (houseWidth - doorWidth) / 2, 50);

glVertex2i(50 + (houseWidth - doorWidth) / 2, 50 + doorHeight);

glVertex2i(50 + (houseWidth + doorWidth) / 2, 50 + doorHeight);

glVertex2i(50 + (houseWidth + doorWidth) / 2, 50);

glEnd();

// Linha azul do canto inferior direito para o canto inferior esquerdo da porta

glBegin(GL_LINES);

glColor3f(0.0, 0.0, 1.0); // Cor azul para a linha

glVertex2i(50 + (houseWidth + doorWidth) / 2, 50);

glVertex2i(50 + (houseWidth - doorWidth) / 2, 50);


glEnd();

glBegin(GL_QUADS);

glColor3f(1.0, 1.0, 1.0); // Cor branca para a janela

// Janela da casa

glVertex2i(windowLeftX, windowBottomY);

glVertex2i(windowLeftX, windowTopY);

glVertex2i(windowRightX, windowTopY);

glVertex2i(windowRightX, windowBottomY);

glEnd();

glBegin(GL_LINES);

glColor3f(0.0, 0.0, 1.0); // Cor azul para a cruz na janela

// Cruz na janela

int crossCenterX = (windowLeftX + windowRightX) / 2;

int crossCenterY = (windowBottomY + windowTopY) / 2;

glVertex2i(crossCenterX, windowBottomY);

glVertex2i(crossCenterX, windowTopY);
glVertex2i(windowLeftX, crossCenterY);

glVertex2i(windowRightX, crossCenterY);

glEnd();

void display() {

glClear(GL_COLOR_BUFFER_BIT);

drawHouse();

glFlush();

void init() {

glClearColor(1.0, 1.0, 1.0, 0.0); // Cor de fundo branca

gluOrtho2D(0, 250, 0, 350);

int main(int argc, char** argv) {

// Solicitar dimensões da casa ao usuário

std::cout << "Digite a largura da casa: ";

std::cin >> houseWidth;

std::cout << "Digite a altura da casa: ";


std::cin >> houseHeight;

std::cout << "Digite a altura do telhado: ";

std::cin >> roofHeight;

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(250, 350);

glutInitWindowPosition(100, 100);

glutCreateWindow("House Example");

init();

glutDisplayFunc(display);

glutMainLoop();

return 0;

}
10

#include <GL/glut.h>

bool rotationActive = false;

GLfloat rotationAngle = 0.0;

void drawCube() {

glutWireCube(1.0); // Desenha um cubo com aresta de comprimento 1.0

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glTranslatef(0.0, 0.0, -5.0); // Move o cubo para trás da câmera

glRotatef(rotationAngle, 0.0, 1.0, 0.0); // Rotação em torno do eixo Y

glColor3f(1.0, 1.0, 1.0); // Cor branca para o cubo


// Define a largura da linha para torná-la mais grossa

glLineWidth(2.0);

drawCube();

glutSwapBuffers();

void reshape(int width, int height) {

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.0, (double)width / height, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

void mouse(int button, int state, int x, int y) {

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {

rotationActive = !rotationActive; // Inverte o estado de rotação

}
void idle() {

if (rotationActive) {

rotationAngle += 0.5; // Incrementa o ângulo de rotação

if (rotationAngle > 360.0) {

rotationAngle -= 360.0; // Reinicia o ângulo após uma rotação completa

glutPostRedisplay(); // Solicita uma nova renderização

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutCreateWindow("Rotating Cube");

glEnable(GL_DEPTH_TEST);

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMouseFunc(mouse);

glutIdleFunc(idle);
glClearColor(0.0, 0.0, 0.0, 0.0);

glutMainLoop();

return 0;

You might also like