Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Created April 8, 2018 10:56
Show Gist options
  • Save SohanChy/26365cc887f4585eeefdabffb637a7d6 to your computer and use it in GitHub Desktop.
Save SohanChy/26365cc887f4585eeefdabffb637a7d6 to your computer and use it in GitHub Desktop.
// house.cc
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
float camPosX = 5;
float camPosy = 13.0f;
float camPosZ = 5;
float upVx = .1;
float upVy = .1;
float upVz = .1;
float angle=20;
/*GLfloat ctrlpoints[3][3] = {
{ 0.0, 0.0, 0.0}, { .50, .50, 0.0},
{1.0, 1.0, 0.0}};*/
GLuint LoadTexture( const char * filename )
{
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 1024;
height = 512;
data = (unsigned char *)malloc( width * height * 3 );
//int size = fseek(file,);
fread( data, width * height * 3, 1, file );
fclose( file );
for(int i = 0; i < width * height ; ++i)
{
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];
data[index] = R;
data[index+2] = B;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
}
void makeCylinder(float x,float y,float z,float rad,float h){
//cylinder
GLuint texture = LoadTexture( "tower.bmp" );
glPushMatrix();
glTranslatef(x,y,z);
glRotatef(90,0.0f,1.0f,0);
glColor3ub(255,0,56);
glBegin(GL_POLYGON);
GLUquadricObj *obj = gluNewQuadric();
gluCylinder(obj, rad, rad, h, 30, 30);
glEnd();
glPopMatrix();
}
void display () {
/* clear window */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* future matrix manipulations should affect the modelview matrix */
glMatrixMode(GL_MODELVIEW);
/* draw scene */
glPushMatrix();
//glScalef(0.33,0.33,0.33);
// house
glPushMatrix();
//angle+=20;
glRotatef(angle, 1.0, 0.0,0.0);
//glColor3f(1.0,0.0,0.0);
//glTranslatef(0,0,0);
//glutWireCube(1); // building
glLineWidth(3.0f);
//X is RED
//Y IS PINK
//Z is BLUE
glBegin(GL_LINES);
//X-Axis
glColor3ub(255,0,0);
glVertex3f(1,2,0);
glVertex3f(2,2,0);
//Z-Axis
glColor3ub(0,0,255);
glVertex3f(1,2,0);
glVertex3f(1,2,1);
//Y-Axis
glColor3ub(255,0,255);
glVertex3f(1,2,0);
glVertex3f(1,3,0);
glEnd();
//JOMI
glBegin(GL_QUADS);
//X-Axis
glColor3ub(96,128,56);
glVertex3f(0,0,-5);
glColor3ub(96,160,28);
glVertex3f(0,0,5);
glColor3ub(124,252,0);
glVertex3f(0,5,5);
glColor3ub(100,255,0);
glVertex3f(0,5,-5);
glEnd();
glPopMatrix();
makeCylinder(0.0,1.0,0.5,0.5,6);
/* flush drawing routines to the window */
//glFlush();
glutSwapBuffers();
}
void reshape ( int width, int height ) {
/* define the viewport transformation */
glViewport(0,0,width,height);
}
void myInit()
{
/* set up depth-buffering */
glEnable(GL_DEPTH_TEST);
/* turn on default lighting */
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
/* define the projection transformation */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90,1.14,5,25);
/* define the viewing transformation */
}
void update(int value)
{
//gluLookAt(5.0,camPosy,5.0,0.0,0.0,0.0,0.0,1.0,0.0glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camPosX,camPosy,camPosZ,8.0,0.0,0.0,1.0,0.0,0.0);
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
camPosy+=0.5f;
std::cout<<camPosy<<std::endl;
update(0);
break;
case GLUT_KEY_DOWN:
camPosy-=0.5f;
update(0);
break;
case GLUT_KEY_RIGHT:
camPosX+=0.5f;
update(0);
break;
case GLUT_KEY_LEFT:
camPosX-=0.5f;
update(0);
break;
}
}
int main ( int argc, char * argv[] ) {
/* initialize GLUT, using any commandline parameters passed to the
program */
glutInit(&argc,argv);
/* setup the size, position, and display mode for new windows */
glutInitWindowSize(1280,720);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
/* create and set up a window */
glutCreateWindow("hello, teapot!");
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutReshapeFunc(reshape);
myInit();
glutTimerFunc(25,update,0);
/* tell GLUT to wait for events */
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment