Skip to content

Instantly share code, notes, and snippets.

@stfairy
Last active Dec 25, 2015
Embed
What would you like to do?
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
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_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void drawBox(float x, float y, float z, float dx, float dy, float dz)
{
glBegin(GL_QUADS);
// Front
glNormal3f(0, 0, dz > 0 ? -1 : 1);
glVertex3f(x, y, z);
glVertex3f(x + dx, y, z);
glVertex3f(x + dx, y + dy, z);
glVertex3f(x, y + dy, z);
// Back
glNormal3f(0, 0, dz > 0 ? 1 : -1);
glVertex3f(x + dx, y, z + dz);
glVertex3f(x, y, z + dz);
glVertex3f(x, y + dy, z + dz);
glVertex3f(x + dx, y + dy, z + dz);
// Left
glNormal3f(dx > 0 ? -1 : 1, 0, 0);
glVertex3f(x, y, z + dz);
glVertex3f(x, y, z);
glVertex3f(x, y + dy, z);
glVertex3f(x, y + dy, z + dz);
// Right
glNormal3f(dx > 0 ? 1 : -1, 0, 0);
glVertex3f(x + dx, y, z);
glVertex3f(x + dx, y, z + dz);
glVertex3f(x + dx, y + dy, z + dz);
glVertex3f(x + dx, y + dy, z);
// Top
glNormal3f(0, dy > 0 ? 1 : -1, 0);
glVertex3f(x, y + dy, z);
glVertex3f(x + dx, y + dy, z);
glVertex3f(x + dx, y + dy, z + dz);
glVertex3f(x, y + dy, z + dz);
// Bottom
glNormal3f(0, dy > 0 ? -1 : 1, 0);
glVertex3f(x, y, z);
glVertex3f(x, y, z + dz);
glVertex3f(x + dx, y, z + dz);
glVertex3f(x + dx, y, z);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox(-0.5, -0.5, -0.5, 1, 1, 1);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
import static org.lwjgl.opengl.GL11.*;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class LightExample {
private int width = 500;
private int height = 500;
private FloatBuffer matSpecular = floatBuffer(1, 1, 1, 1);
private FloatBuffer matShininess = floatBuffer(50);
private FloatBuffer lightPosition = floatBuffer(1, 1, 1, 0);
private FloatBuffer floatBuffer(float... values) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(values.length + 4);
buffer.put(values);
return buffer;
}
private void initGL() {
glClearColor(0, 0, 0, 0);
glShadeModel(GL_SMOOTH);
glMaterial(GL_FRONT, GL_SPECULAR, matSpecular);
glMaterial(GL_FRONT, GL_SHININESS, matShininess);
glLight(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5 * width / height, 1.5 * width / height, -1.5, 1.5, -10, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
private void drawBox(float x, float y, float z, float dx, float dy, float dz) {
glBegin(GL_QUADS);
// Front
glNormal3f(0, 0, dz > 0 ? -1 : 1);
glVertex3f(x, y, z);
glVertex3f(x + dx, y, z);
glVertex3f(x + dx, y + dy, z);
glVertex3f(x, y + dy, z);
// Back
glNormal3f(0, 0, dz > 0 ? 1 : -1);
glVertex3f(x + dx, y, z + dz);
glVertex3f(x, y, z + dz);
glVertex3f(x, y + dy, z + dz);
glVertex3f(x + dx, y + dy, z + dz);
// Left
glNormal3f(dx > 0 ? -1 : 1, 0, 0);
glVertex3f(x, y, z + dz);
glVertex3f(x, y, z);
glVertex3f(x, y + dy, z);
glVertex3f(x, y + dy, z + dz);
// Right
glNormal3f(dx > 0 ? 1 : -1, 0, 0);
glVertex3f(x + dx, y, z);
glVertex3f(x + dx, y, z + dz);
glVertex3f(x + dx, y + dy, z + dz);
glVertex3f(x + dx, y + dy, z);
// Top
glNormal3f(0, dy > 0 ? 1 : -1, 0);
glVertex3f(x, y + dy, z);
glVertex3f(x + dx, y + dy, z);
glVertex3f(x + dx, y + dy, z + dz);
glVertex3f(x, y + dy, z + dz);
// Bottom
glNormal3f(0, dy > 0 ? -1 : 1, 0);
glVertex3f(x, y, z);
glVertex3f(x, y, z + dz);
glVertex3f(x + dx, y, z + dz);
glVertex3f(x + dx, y, z);
glEnd();
}
private void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox(-0.5f, -0.5f, -0.5f, 1, 1, 1);
glFlush();
}
public void start() {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
initGL();
while (!Display.isCloseRequested()) {
drawScene();
Display.update();
}
Display.destroy();
}
public static void main(String[] args) {
LightExample example = new LightExample();
example.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment