Skip to content

Instantly share code, notes, and snippets.

@vakili73
Last active November 1, 2015 13:06
Show Gist options
  • Select an option

  • Save vakili73/3ab2c3ebc584ef91d3d0 to your computer and use it in GitHub Desktop.

Select an option

Save vakili73/3ab2c3ebc584ef91d3d0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
using namespace std;
void drawfunc(void);
void myglinit(void);
char *easel;
int width, height;
int X1 = 0;
int X2 = 0;
int Y1 = 0;
int Y2 = 0;
int main(int argc, char **argv) {
// width and height of the window
width = height = 500;
// input variable
cout << "X1: ";
cin >> X1;
cout << "Y1: ";
cin >> Y1;
cout << "X2: ";
cin >> X2;
cout << "Y2: ";
cin >> Y2;
// --------------
// create a char buffer, 3 bytes per pixel of the window
easel = new char[width*height*3];
// initialize the glut system and create a window
glutInitWindowSize(width, height);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("example");
// initialize some OpenGL state
myglinit();
// set callback functions. drawfunc is the function that gets
// called automatically as fast as possible. keyfunc only is
// invoked if the user hits a keyboard key.
glutDisplayFunc(drawfunc);
// start the main glut loop, no code runs after this
glutMainLoop();
}
void setpixel(char *buf, int x, int y, int r, int g, int b) {
buf[(y*width+x)*3+0] = r;
buf[(y*width+x)*3+1] = g;
buf[(y*width+x)*3+2] = b;
}
// main draw function, gets called over and over, as fast as possible
void drawfunc(void) {
int** ary = new int*[10];
for(int i = 0; i < 10; ++i)
ary[i] = new int[3];
for (int i = 0 ; i < 5 ; i++){
for(int j = 0 ; j < 3 ; j++){
ary[i][j] = 255;
}
}
for (int i = 5 ; i < 10 ; i++){
for(int j = 0 ; j < 3 ; j++){
ary[i][j] = 0;
}
}
float m = (Y2 - Y1) / (X2 - X1);
for (int i = X1; i < X2; i++){
setpixel(easel, i, (int) (m*i + (X1 - Y1)), ary[i%10][0], ary[i%10][1], ary[i%10][2]);
}
// drawpixels draws the rgb data stored in 'easel' to the screen
glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, easel);
// in double buffer mode so we swap to avoid a flicker
glutSwapBuffers();
// instruct event system to call 'drawfunc' again
glutPostRedisplay();
}
// set some OpenGL state variables
void myglinit() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment