0% found this document useful (0 votes)
80 views

#Include #Include #Pragma Hdrstop Int Int

The document defines functions for rendering a fractal image using OpenGL. It initializes window parameters like size and boundaries. It defines functions for repainting the window which iterates through pixels, calculates the color using a function, and draws points. It also includes functions for handling resize events and keyboard input like toggling fullscreen mode.

Uploaded by

chavachiras
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)
80 views

#Include #Include #Pragma Hdrstop Int Int

The document defines functions for rendering a fractal image using OpenGL. It initializes window parameters like size and boundaries. It defines functions for repainting the window which iterates through pixels, calculates the color using a function, and draws points. It also includes functions for handling resize events and keyboard input like toggling fullscreen mode.

Uploaded by

chavachiras
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/ 6

#include <stdlib.

h>
#include <gl/glut.h> // include GLUT library header
#pragma hdrstop
int width=600, height=600; // window size
int windowID;
GLfloat minX = -2.2f, maxX = 0.8f, minY = -1.5f, maxY = 1.5; // complex plane
boundaries
GLfloat stepX = (maxX - minX)/(GLfloat)width;
GLfloat stepY = (maxY - minY)/(GLfloat)height;
GLfloat black [3] = {0.0f, 0.0f, 0.0f}; // black color
const int paletteSize = 128;
GLfloat palette[paletteSize][3];
const GLfloat radius = 5.0f;
bool fullScreen=false;
//****************************************
GLfloat* calculateColor(GLfloat u, GLfloat v){
GLfloat re = u;
GLfloat im = v;
GLfloat tempRe=0.0;
for(int i=0; i < paletteSize; i++){
tempRe = re*re - im*im + u;
im = re * im * 2 + v;
re = tempRe;
if( (re*re + im*im) > radius ){
return palette[i];
}
}
return black;
}
//****************************************
void repaint() {// function called to repaint the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen buffer
glBegin(GL_POINTS); // start drawing in single pixel mode
for(GLfloat y = maxY; y >= minY; y -= stepY){
for(GLfloat x = minX; x <= maxX; x += stepX){
glColor3fv(calculateColor(x,y)); // set color
glVertex3f(x, y, 0.0f); // put pixel on screen (buffer) - [ 1 ]
}
}
glEnd(); // end drawing
glutSwapBuffers(); // swap the buffers - [ 2 ]
}
//****************************************
void reshape (int w, int h){ // function called when window size is changed
stepX = (maxX-minX)/(GLfloat)w; // calculate new value of step along X axis
stepY = (maxY-minY)/(GLfloat)h; // calculate new value of step along Y axis
glViewport (0, 0, (GLsizei)w, (GLsizei)h); // set new dimension of viewable screen
glutPostRedisplay(); // repaint the window
}
//****************************************

void keyFunction(unsigned char key, int x, int y){ // function to handle key
pressing
switch(key){
case 'F': // pressing F is turning on/off fullscreen mode
case 'f':
if(fullScreen){
glutReshapeWindow(width,height); // sets default window size
GLsizei windowX = (glutGet(GLUT_SCREEN_WIDTH)-width)/2;
GLsizei windowY = (glutGet(GLUT_SCREEN_HEIGHT)-height)/2;
glutPositionWindow(windowX, windowY); // centers window on the screen
fullScreen = false;
}
else{
fullScreen = true;
glutFullScreen(); // go to fullscreen mode
}
glutPostRedisplay();
break;
case 27 : // escape key - close the program
glutDestroyWindow(windowID);
exit(0);
break;
}
}
//****************************************
void createPalette(){
for(int i=0; i < 32; i++){
palette[i][0] = (8*i)/(GLfloat)255;
palette[i][1] = (128-4*i)/(GLfloat)255;
palette[i][2] = (255-8*i)/(GLfloat)255;
}
for(int i=0; i < 32; i++){
palette[32+i][0] = (GLfloat)1;
palette[32+i][1] = (8*i)/(GLfloat)255;
palette[32+i][2] = (GLfloat)0;
}
for(int i=0; i < 32; i++){
palette[64+i][0] = (128-4*i)/(GLfloat)255;
palette[64+i][1] = (GLfloat)1;
palette[64+i][2] = (8*i)/(GLfloat)255;
}
for(int i=0; i < 32; i++){
palette[96+i][0] = (GLfloat)0;
palette[96+i][1] = (255-8*i)/(GLfloat)255;
palette[96+i][2] = (8*i)/(GLfloat)255;
}
}
//****************************************
int main(int argc, char** argv){
glutInit(&argc, argv);
createPalette();
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLsizei windowX = (glutGet(GLUT_SCREEN_WIDTH)-width)/2;
GLsizei windowY = (glutGet(GLUT_SCREEN_HEIGHT)-height)/2;
glutInitWindowPosition(windowX, windowY);

glutInitWindowSize(width, height);
windowID = glutCreateWindow("FRAKTALE");
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glViewport (0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(minX, maxX, minY, maxY, ((GLfloat)-1), (GLfloat)1);
// set the event handling methods
glutDisplayFunc(repaint);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyFunction);
glutMainLoop();
return 0;
}

glBegin(GL_TRIANGLES);
glVertex3f(-1.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();

"$(ProjectDir)include"
"$(ProjectDir)lib"
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen buffer
glPointSize(6);
glColor3f( 0.0, 0.0, 0.7 );
glBegin( GL_TRIANGLES );
glVertex3f(50.0, 30.0, 0.0 );
glVertex3f( 250.0, 30.0, 0.0 );
glVertex3f( 150.0, 150.0, 0.0 );
glEnd( );

glColor3f( 0.0, 1.0, 1.0 );


glBegin( GL_POLYGON );
glVertex3f( 150.0, 200.0, 0.0
glVertex3f( 100.0, 250.0, 0.0
glVertex3f( 200.0, 250.0, 0.0
glVertex3f( 150.0, 300.0, 0.0
glEnd( );

);
);
);
);

glColor3f( 0.7, 0.0, 0.7 );


glBegin( GL_POLYGON );
glVertex3f( 300.0, 400.0, 0.0 );
glVertex3f( 300.0, 350.0, 0.0 );
glVertex3f( 350.0, 350.0, 0.0 );

glVertex3f( 350.0, 400.0, 0.0 );


glEnd( );
glColor3f( 1.0, 0.5, 1.0 );
glBegin( GL_LINES );
glVertex3f(500, 500.0, 0.0 );
glVertex3f( 400.0, 400.0, 0.0
glVertex3f( 400.0, 500.0, 0.0
glVertex3f( 500.0, 400.0, 0.0
glVertex3f( 450.0, 400.0, 0.0
glVertex3f( 450.0, 500.0, 0.0
glVertex3f( 500.0, 450.0, 0.0
glVertex3f( 400.0, 450.0, 0.0
glEnd( );

);
);
);
);
);
);
);

glColor3f( 1.0, 0.5, 1.0 );


glBegin( GL_LINES );
glVertex3f(200.0, 500.0, 0.0 );
glVertex3f( 100.0, 400.0, 0.0 );
glVertex3f( 100.0, 500.0, 0.0 );
glVertex3f( 200.0, 400.0, 0.0 );
glVertex3f( 150.0, 400.0, 0.0 );
glVertex3f( 150.0, 500.0, 0.0 );
glVertex3f( 200.0, 450.0, 0.0 );
glVertex3f( 100.0, 450.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(0.0, 0.0, 0.0 );
glVertex3f( 50.0, 0.0, 0.0 );
glVertex3f( 25.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(50.0, 0.0, 0.0 );
glVertex3f( 100.0, 0.0, 0.0 );
glVertex3f( 75.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(100.0, 0.0, 0.0 );
glVertex3f( 150.0, 0.0, 0.0 );
glVertex3f( 125.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(150.0, 0.0, 0.0 );
glVertex3f( 200.0, 0.0, 0.0 );
glVertex3f( 175.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(200.0, 0.0, 0.0 );

glVertex3f( 250.0, 0.0, 0.0 );


glVertex3f( 225.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(250.0, 0.0, 0.0 );
glVertex3f( 300.0, 0.0, 0.0 );
glVertex3f( 275.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(300.0, 0.0, 0.0 );
glVertex3f( 350.0, 0.0, 0.0 );
glVertex3f( 325.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(350.0, 0.0, 0.0 );
glVertex3f( 400.0, 0.0, 0.0 );
glVertex3f( 375.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(400.0, 0.0, 0.0 );
glVertex3f( 450.0, 0.0, 0.0 );
glVertex3f( 425.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(450.0, 0.0, 0.0 );
glVertex3f( 500.0, 0.0, 0.0 );
glVertex3f( 475.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(500.0, 0.0, 0.0 );
glVertex3f( 550.0, 0.0, 0.0 );
glVertex3f( 525.0, 25.0, 0.0 );
glEnd( );
glColor3f( 0.5, 1.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f(550.0, 0.0, 0.0 );
glVertex3f( 600.0, 0.0, 0.0 );
glVertex3f( 575.0, 25.0, 0.0 );
glEnd( );
glutSwapBuffers(); // swap the buffers - [ 2 ]

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//TRANGULO INFERIOR IZQUIERDO
glColor3f( 0.0, 0.0, 0.7 );
glBegin( GL_TRIANGLES );
glVertex3f(200.0, 0.0, 0.0 );
glVertex3f( 400.0, 0.0, 0.0 );
glVertex3f( 0.0, 300.0, 0.0 );
glEnd( );
//TRANGULO INFERIOR DERECHO
glColor3f( 0.0, 0.0, 0.7 );
glBegin( GL_TRIANGLES );
glVertex3f(200.0, 0.0, 0.0 );
glVertex3f( 400.0, 0.0, 0.0 );
glVertex3f( 600.0, 300.0, 0.0 );
glEnd( );
/*glColor3f( 0.0, 0.0, 0.9 );
glBegin( GL_TRIANGLES );
glVertex3f(200.0, 0.0, 0.0 );
glVertex3f( 400.0, 0.0, 0.0 );
glVertex3f( 0.0, 400.0, 0.0 );
glEnd( );*/

/*glColor3f( 0.0, 0.0, 0.3 );


glBegin( GL_TRIANGLES );
glVertex3f(600.0, 0.0, 0.0 );
glVertex3f( 400.0, 0.0, 0.0 );
glVertex3f( 0.0, 300.0, 0.0 );
glEnd( );*/
//TRIANGULO ARRIBA IZQUIERDO
glColor3f( 0.0, 0.0, 0.7 );
glBegin( GL_TRIANGLES );
glVertex3f(200.0, 600.0, 0.0 );
glVertex3f( 400.0, 600.0, 0.0 );
glVertex3f( 0.0, 300.0, 0.0 );
glEnd( );
//TRIANGULO ARRIBA DERECHA
glColor3f( 0.0, 0.0, 0.7 );
glBegin( GL_TRIANGLES );
glVertex3f(200.0, 600.0, 0.0 );
glVertex3f( 400.0, 600.0, 0.0 );
glVertex3f( 600.0, 300.0, 0.0 );
glEnd( );
glColor3f( 1.0, 0.0, 1.0 );
glBegin( GL_POLYGON );
glVertex3f( 150.0, 200.0, 0.0
glVertex3f( 100.0, 250.0, 0.0
glVertex3f( 200.0, 250.0, 0.0
glVertex3f( 150.0, 300.0, 0.0
glEnd( );

);
);
);
);

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

You might also like