Introduction – Space cube
In this introduction we’re looking at the basics of OpenGL and how presentation is working in practice.
Different presentation of simple forms is the key to success.
Here is the simplest way to draw a cube:
glDisable (GL_LIGHTING); glColor3f(0,0,0.3); glutSolidCube(1.0); //bib. GLUT, glutSolidCube - fargefull glEnable(GL_LIGHTING);
Here how we draw these gray sides:
void drawSide()
{
glBegin(GL_QUAD_STRIP);
//bottom
glVertex3f(0,0,0);
glVertex3f(0.1,0,0);
glVertex3f(0.1,0.1,0);
glVertex3f(0,0.1,0);
//4 sides
glVertex3f(0,0,0);
glVertex3f(0,0.1,0);
glVertex3f(0.1,0,1.1);
glVertex3f(0,0,1.1);
glVertex3f(0.1,0,0);
glVertex3f(0.1,0.1,0);
glVertex3f(0.1,0.1,1.1);
glVertex3f(0,0,1.1);
glVertex3f(0.1,0.1,0);
glVertex3f(0,0.1,0);
glVertex3f(0,0.1,1.1);
glVertex3f(0.1,0.1,1.1);
glVertex3f(0,0.1,0);
glVertex3f(0,0,0);
glVertex3f(0,0,1.1);
glVertex3f(0,0.1,1.1);
//up
glVertex3f(0,0,1.1);
glVertex3f(0.1,0,1.1);
glVertex3f(0.1,0.1,1.1);
glVertex3f(0,0.1,1.1);
glEnd();
}
Here is how you draw the stars (all stars are the same size, but they are placed in different places of the universe). We use a GLUT-library in order to draw a more complex figure – solid sphere that represents a star:
void stjerner()
{
glPushMatrix();
glTranslatef(-120, -480,-80);
glutSolidSphere(5, 5,5);
glTranslatef(100,-600,80);
glutSolidSphere(5, 5,5);
glTranslatef(600,0,-1000);
glutSolidSphere(5,5,5);
glTranslatef(300,0,-200);
glutSolidSphere(5,5,5);
glTranslatef(500,0,1800);
glutSolidSphere(5,5,5);
glPopMatrix();
glPushMatrix();
glTranslatef(350,580,-280);
glutSolidSphere(5,5,5);
glTranslatef(-550,100,-500);
glutSolidSphere(5,5,5);
glPopMatrix();
}
We also want the cube to rotate (everything is rotating over there you know):
void spinDisplay() {
spin = spin + 0.1;
if(spin>360) spin = spin - 360;
glutPostRedisplay();
}
Complete source for this example.
#include <stdlib.h>
#include <glut.h>
#include <GL/glu.h>
#include <Windows.h>
#include <math.h>
int height, width;
static GLfloat spin = 0.0;
GLfloat mat_specular [] = {0,0,0,0.4};
GLfloat mat_shineness [] = {128};
GLfloat ambient [] = {0,0,0,1};
GLfloat diffuse [] = {1,1,1,1};
GLfloat pos [] = {3,3,0,1};
GLfloat pos1 [] = {120,20,0,1};
void spinDisplay() {
spin = spin + 0.1;
if(spin>360) spin = spin - 360;
glutPostRedisplay();
}
void drawSide()
{
glBegin(GL_QUAD_STRIP);
//bottom
glVertex3f(0,0,0);
glVertex3f(0.1,0,0);
glVertex3f(0.1,0.1,0);
glVertex3f(0,0.1,0);
//4 sides
glVertex3f(0,0,0);
glVertex3f(0,0.1,0);
glVertex3f(0.1,0,1.1);
glVertex3f(0,0,1.1);
glVertex3f(0.1,0,0);
glVertex3f(0.1,0.1,0);
glVertex3f(0.1,0.1,1.1);
glVertex3f(0,0,1.1);
glVertex3f(0.1,0.1,0);
glVertex3f(0,0.1,0);
glVertex3f(0,0.1,1.1);
glVertex3f(0.1,0.1,1.1);
glVertex3f(0,0.1,0);
glVertex3f(0,0,0);
glVertex3f(0,0,1.1);
glVertex3f(0,0.1,1.1);
//up
glVertex3f(0,0,1.1);
glVertex3f(0.1,0,1.1);
glVertex3f(0.1,0.1,1.1);
glVertex3f(0,0.1,1.1);
glEnd();
}
//texture of the Cube (blue with yellow lines)
void kvadratene()
{
glBegin(GL_QUAD_STRIP);
glVertex3f(0,0,0);
glVertex3f(1.02,0,0);
glVertex3f(1.02,1.02,0);
glVertex3f(0,1.02,0);
glEnd();
}
void stjerner()
{
glPushMatrix();
glTranslatef(-120, -480,-80);
glutSolidSphere(5, 5,5);
glTranslatef(100,-600,80);
glutSolidSphere(5, 5,5);
glTranslatef(600,0,-1000);
glutSolidSphere(5,5,5);
glTranslatef(300,0,-200);
glutSolidSphere(5,5,5);
glTranslatef(500,0,1800);
glutSolidSphere(5,5,5);
glPopMatrix();
glPushMatrix();
glTranslatef(350,580,-280);
glutSolidSphere(5,5,5);
glTranslatef(-550,100,-500);
glutSolidSphere(5,5,5);
glPopMatrix();
glPushMatrix();
glTranslatef(750,800,280);
glutSolidSphere(5,5,5);
glTranslatef(-550,200,500);
glutSolidSphere(5,5,5);
glPopMatrix();
glPushMatrix();
glTranslatef(1050,0,-500);
glutSolidSphere(5,5,5);
glTranslatef(250,-100,-800);
glutSolidSphere(5,5,5);
glPopMatrix();
glPushMatrix();
glTranslatef(1500,-200,0);
glutSolidSphere(5,5,5);
glTranslatef(550,-300,-800);
glutSolidSphere(5,5,5);
glPopMatrix();
}
//draw everything on the screen
void display() {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3,3,0,0,0,-1,0,1,-1); //position of the camera
glEnable(GL_LIGHTING);
glRotatef(spin,0,0,1); //rotate the Cube
//the sun
glTranslatef(-120,-20,-50);
glutSolidSphere(100,1600,100);
glTranslatef(120,20,50);
//cube
glDisable (GL_LIGHTING);
glColor3f(0,0,0.3);
glutSolidCube(1.0);
glEnable(GL_LIGHTING);
//sides
glColor3f(0.5,0.5,0.5);
glTranslatef(0.47, 0.47,-0.55);
drawSide();
glTranslatef(-0.47, -0.47, 0.55);
glTranslatef(-0.57, 0.47,-0.55);
drawSide();
glTranslatef(0.57, -0.47, 0.55);
glTranslatef(0.47, -0.57,-0.55);
drawSide();
glTranslatef(-0.47, 0.57, 0.55);
glTranslatef(-0.57, -0.57,-0.55);
drawSide();
glTranslatef(0.57, 0.57, 0.55);
//texture of the cube
glTranslatef(-0.51,-0.51,0.4);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0,0,-0.1);
kvadratene();
glTranslatef(0.51,0.51,0.4);
//antenna
glTranslatef(0,0,-3.5);
gluCylinder(glpQ,0.03,0.03,3,10,10);
glTranslatef(0,0,-0.1);
gluCylinder(glpQ,0.15,0.15,0.4,10,10);
//stars
glDisable(GL_LIGHTING);
stjerner();
glEnable(GL_LIGHTING);
glFlush();
glutSwapBuffers();
}
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shineness);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_EMISSION, diffuse);
glEnable(GL_DEPTH_TEST);
}
void reshape(int w, int h) {
if (h == 0)
h = 1;
height = h;
width = w;
GLdouble ratio = 1.0f * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (GLsizei)w,(GLsizei)h);
gluPerspective(90, ratio, 0.1, 100000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char**argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,400);
glutInitWindowPosition(100,100);
glutCreateWindow("HCube");
init();
glutIdleFunc(spinDisplay);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Popularity: 2% [?]
