All pastes #511500 Raw Edit

mcnasty

public text v1 · immutable
#511500 ·published 2007-05-26 14:00 UTC
rendered paste body
/*Skeleton code for opengl software development on any linux 
machine, Creates a blank window.*/

#include <GL/gl.h>
#include <GL/glut.h>
#include <cstdlib>
#include <stdio.h>
#include <cmath>

//variable declaration

//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle = 0.0;

//positions of the cubes
float positionz[10];
float positionx[10];



GLuint texture;
GLfloat whiteSpecularMaterial[] = {1.0,1.0,1.0};
GLfloat whiteSpecularLight[] = {1.0,1.0,1.0};
GLfloat redDiffuseMaterial[] = {0.0,0.0,1.0};
GLfloat whiteDiffuseLight[] = {1.0,1.0,1.0};
GLfloat blankMaterial[] = {0.0, 0.0,0.0};
GLfloat mShininess[] = {120}; //set the shininess of the material
bool specular = false; //whether or not specular lighting mode is enabled

//end variable declaration

void cubepositions (void)//set the positions of the cubes
{
	for(int i = 0; i < 10; i++)
	{
	positionz[i] = rand()% 5;
	positionx[i] = rand()% 5;
	}
}


//function to load raw file
GLuint LoadTexture(const char * filename, int width, int height)
{
	GLuint texture; 	//create GLuint data type variable texture
	unsigned char * data;
	FILE * file; 	//Create our pointer for our  filehandle
	
	//the following code will read IN our RAW file
	file = fopen("texture.raw", "rb"); 
	if(file == NULL) return 0;
	data = (unsigned char *)malloc(width * height * 3);
	fread(data, width * height * 3, 1, file);
	fclose(file);
	
	glGenTextures(1, &texture); //generate the texture with the loaded data
	glBindTexture(GL_TEXTURE_2D, texture); //bind the texture to its array
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); //set texture env parameters
	
	//here we set what textures to use and when, the MIN filter is which quality to show
	//when the texture is near the view, and the MAG filter is which quality to show
	//when the texture is far from view

	//the qualities are
	//GL_NEAREST , GL_LINEAR , GL_LINEAR_MIPMAP_NEAREST , GL_LINEAR_MIPMAP_LINEAR
	//and if you use extensions you can use anisotropic filtering
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	//here set the parameter to repeat the texture instead of clamping it 
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	//generate the texture
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	free(data); //free the data, and memory...
	return texture; //return whether it was successfull or not
}

void FreeTexture(GLuint texture)
{
	glDeleteTextures(1, &texture);
}

void cube()
{
	for(int i=0; i < 10; i++)
	{
	glTranslated(-positionx[i+1] * 3, 0 , -positionz[i + 1] * 3); //translate cube
	glPushMatrix();
	glRotatef(angle,1.0,0.0,1.0);
	/*glRotatef(angle,0.0,1.0,0.0);
	//glRotatef(angle,0.0,0.0,1.0);*/
	glColor3f(1.0,1.0,1.0);

	glutWireCube(2);
	glPopMatrix();
	}
}
	
void square()
{
	glPushMatrix(); //where to start object transformations
	glTranslatef(1.0,1.0,0.0);
	//glRotatef(angle,1.0,0.0,0.0);
	glRotatef(angle,2.0,0.0,0.0);
	glRotatef(angle,0.0,1.0,0.0);
	glRotatef(angle,0.0,0.0,1.0);

	glBindTexture(GL_TEXTURE_2D, texture); //bind texture to our shape
	glBegin(GL_QUADS);
	glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
	glTexCoord2d(1.0,0.0); glVertex2d(+1.0, -1.0);
	glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
	glTexCoord2d(0.0,1.0); glVertex2d(-1.0, +1.0);
	glPopMatrix(); //where to end object transformations
	glEnd();
	
	/*glPushMatrix();
	glTranslatef(1.0,1.0,0.0);
	glTranslatef(-2.0, -1.0, 0.0);
	glBegin(GL_QUADS);
	glVertex3f(-1.0,-1.0, 0.0);
	glVertex3f(1.0, -1.0,0.0);
	glVertex3f(1.0, 1.0,0.0);
	glVertex3f(-1.0, 1.0,0.0);
	glPopMatrix();
	glEnd();*/
}


void init()
{
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT1);
	glShadeModel(GL_SMOOTH);
	cubepositions();
}


void light()
{

	glLightfv(GL_LIGHT0, GL_SPECULAR, whiteSpecularLight);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, whiteSpecularMaterial);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, redDiffuseMaterial);
}


void keyboard(unsigned char key, int x, int y)
{
	if(key == 27) 
	{
		exit(0);
	}
	

	if(key =='q')
	{
		xrot +=1;
		if(xrot > 360) xrot -= 360; //reset view back to 0 if we go past 360
	}
	
	if(key == 'z')
	{
		xrot -=1;
		if(xrot < -360) xrot += 360;
	}
	
	if(key == 'w')
	{
		float xrotrad, yrotrad; //x and y rotation radius
		yrotrad = (yrot / 180 * 3.141592654f);
		xrotrad = (xrot / 180 * 3.141592654f);
		xpos +=float(sin(yrotrad));
		zpos -= float(cos(yrotrad));
		ypos -= float(sin(xrotrad));
	}
	
	if(key == 's')
	{
		float xrotrad, yrotrad;
		yrotrad = (yrot / 180 * 3.141592654f);
		xrotrad = (xrot / 180 * 3.141592654f);
		xpos -= float(sin(yrotrad));
		zpos += float(cos(yrotrad));
		ypos += float(sin(xrotrad));
	}

}

void camera()
{
glRotatef(xrot,1.0,0.0,0.0); //rotate camera on the x axis
glRotatef(yrot,0.0,1.0,0.0);
glTranslated(-xpos,-ypos,-zpos);
}
//our display function, sets color, clears, color buffer, load identity matrix,// , set view and finally render the output to the screen.
void display(void)
{
        glClearColor(0.0,0.0,0.0,1.0); //clear the color of the window
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer
	glLoadIdentity(); //load the identity matrix
	glEnable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_2D);
	light();
        glTranslatef(0,0,0);
        //glRotatef(angle,1,1,1);
	gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); //set the view
	camera();
	//texture = LoadTexture("texture.raw", 256,256);
	//glEnable(GL_TEXTURE_GEN_T);
	//square();
	cube();
	glutSwapBuffers();
	angle++;
}

//our reshape function scales our screen, and images, to the correct size format
void reshape(int w, int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h); //set viewport
	glMatrixMode(GL_PROJECTION); //select matrix mode
	glLoadIdentity(); //now load the matrix
	gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0); //angle, aspect of ration, min view distance, max view distance
	glMatrixMode(GL_MODELVIEW);
}


//Main function 
int main(int argc, char **argv)
{
        glutInit(&argc, argv); //initialize the program
        glutInitDisplayMode(GLUT_DOUBLE); //set up a basic single  display buffer
	glutInitWindowSize(500,500); //set the width and height of the window
        glutInitWindowPosition(100,100); //set the position of the window
        glutCreateWindow("Best RPG on earth"); //set the caption for the window
        glutDisplayFunc(display);//call the display function to draw our world
	glutIdleFunc(display);
        glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	//load texture
	init();
	glutMainLoop(); //initialize the Opengl loop cycle
	//FreeTexture(texture);

        return 0;
}