Last active
October 31, 2017 09:26
-
-
Save kevinmungai/16eb9d38e554f451284c4b6cd5acb1e1 to your computer and use it in GitHub Desktop.
Creating a Curve in OpenGL using the equation y = x ^ 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Drawing the curve y = x ^ 2 in OpenGL | |
* x ranges from (-10) to 10 | |
* It's made to fit the entire Viewport | |
*/ | |
#include <windows.h> // for MS Windows | |
#include <GL/glut.h> // GLUT, include glu.h and gl.h | |
#include <GL/gl.h> | |
/* Initialize OpenGL Graphics */ | |
void initGL() { | |
// Set "clearing" or background color | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque BackGround | |
gluOrtho2D(-10.0, 10.0, 0.0, 100.0); // World View gluOrtho2d(left, right, bottom, top); | |
glViewport(0, 0, 640, 480); // Is the View Port glViewport(left, bottom, width, height); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
} | |
void display() { | |
glClear(GL_COLOR_BUFFER_BIT); | |
GLdouble x; | |
glBegin(GL_LINE_STRIP); | |
for (x = -10.0; x <= 10.0; x+= 0.1 ) { | |
glVertex2d(x, x * x); | |
} | |
glEnd(); | |
glFlush(); | |
} | |
/* Main function: GLUT runs as a console application starting at main() */ | |
int main(int argc, char** argv) { | |
glutInit(&argc, argv); // Initialize GLUT | |
glutCreateWindow(" y = x ^ 2"); // Create window with the given title | |
glutInitWindowSize(320, 320); // Set the window's initial width & height | |
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner | |
glutDisplayFunc(display); // Register callback handler for window re-paint event | |
initGL(); // Our own OpenGL initialization | |
glutMainLoop(); // Enter the event-processing loop | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment