Skip to content

Instantly share code, notes, and snippets.

@kevinmungai
Last active October 31, 2017 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmungai/16eb9d38e554f451284c4b6cd5acb1e1 to your computer and use it in GitHub Desktop.
Save kevinmungai/16eb9d38e554f451284c4b6cd5acb1e1 to your computer and use it in GitHub Desktop.
Creating a Curve in OpenGL using the equation y = x ^ 2
/*
* 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