|
glMaterialfv(GL_FRONT, GL_AMBIENT, mat.ambient.rgba);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat.diffuse.rgba);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat.specular.rgba);
glMaterialf(GL_FRONT, GL_SHININESS, mat.shininess);
#ifdef __APPLE__
#include <gl.h>
#include <glu.h>
#include <glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include <assert.h>
static char *TITLE = "test_materials";
void init() {
GLfloat LP[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 10,
(GLfloat) 1};
GLfloat A[] = {(GLfloat) 0.1,
(GLfloat) 0.1,
(GLfloat) 0.1};
GLfloat S[] = {(GLfloat) 0.5,
(GLfloat) 0.5,
(GLfloat) 0.5};
GLfloat D[] = {(GLfloat) 0.5,
(GLfloat) 0.5,
(GLfloat) 0.5};
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_FRONT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, LP);
glLightfv(GL_LIGHT0, GL_AMBIENT, A);
glLightfv(GL_LIGHT0, GL_SPECULAR, S);
glLightfv(GL_LIGHT0, GL_DIFFUSE, D);
glClearColor(0.f, 0.f, 0.f, 1.f);
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float) width / (float) height, 1.0, 100);
}
void display() {
static GLfloat RA[] = {(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1};
static GLfloat RS[] = {(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1};
static GLfloat RD[] = {(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1};
static GLfloat GA[] = {(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 1};
static GLfloat GS[] = {(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 1};
static GLfloat GD[] = {(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 1};
static GLfloat BA[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 1};
static GLfloat BS[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 1};
static GLfloat BD[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 1};
static GLfloat Y = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5,
0, 0, 0,
0, 1, 0);
glBegin(GL_QUADS);
glNormal3d(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, RA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, RD);
glMaterialfv(GL_FRONT, GL_SPECULAR, RS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glVertex3d(0, 0, 0);
glVertex3d(1, 0, 0);
glVertex3d(1, 1, 0);
glVertex3d(0, 1, 0);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3d(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, GA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, GD);
glMaterialfv(GL_FRONT, GL_SPECULAR, GS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glVertex3d(1, 0, 0);
glVertex3d(2, 0, 0);
glVertex3d(2, 1, 0);
glEnd();
glBegin(GL_TRIANGLES);
glNormal3d(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, BA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, BD);
glMaterialfv(GL_FRONT, GL_SPECULAR, BS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glVertex3d(0, 0, 0);
glVertex3d(-1, 1, 0);
glVertex3d(-1, 0, 0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char **argv) {
int window = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
window = glutCreateWindow(TITLE);
assert(window);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
glutDestroyWindow(window);
return 0;
}
#ifdef __APPLE__
#include <gl.h>
#include <glu.h>
#include <glut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
static char *TITLE = "test_materials";
static GLint quadId = 0;
static GLint triangleAId = 0;
static GLint triangleBId = 0;
static void objects() {
GLfloat RA[] = {(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1};
GLfloat RS[] = {(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1};
GLfloat RD[] = {(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1};
GLfloat GA[] = {(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 1};
GLfloat GS[] = {(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 1};
GLfloat GD[] = {(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 0,
(GLfloat) 1};
GLfloat BA[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 1};
GLfloat BS[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 1};
GLfloat BD[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 1,
(GLfloat) 1};
GLfloat Y = 0;
quadId = glGenLists(1);
glNewList(quadId, GL_COMPILE);
glNormal3d(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, RA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, RD);
glMaterialfv(GL_FRONT, GL_SPECULAR, RS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glBegin(GL_QUADS);
glVertex3d(0, 0, 0);
glVertex3d(1, 0, 0);
glVertex3d(1, 1, 0);
glVertex3d(0, 1, 0);
glEnd();
glEndList();
triangleAId = glGenLists(1);
glNewList(triangleAId, GL_COMPILE);
glNormal3d(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, GA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, GD);
glMaterialfv(GL_FRONT, GL_SPECULAR, GS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glBegin(GL_TRIANGLES);
glVertex3d(1, 0, 0);
glVertex3d(2, 0, 0);
glVertex3d(2, 1, 0);
glEnd();
glEndList();
triangleBId = glGenLists(1);
glNewList(triangleBId, GL_COMPILE);
glNormal3d(0, 0, 1);
glMaterialfv(GL_FRONT, GL_AMBIENT, BA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, BD);
glMaterialfv(GL_FRONT, GL_SPECULAR, BS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glBegin(GL_TRIANGLES);
glVertex3d(0, 0, 0);
glVertex3d(-1, 1, 0);
glVertex3d(-1, 0, 0);
glEnd();
glEndList();
}
static void light() {
GLfloat LP[] = {(GLfloat) 0,
(GLfloat) 0,
(GLfloat) 10,
(GLfloat) 1};
GLfloat A[] = {(GLfloat) 0.1,
(GLfloat) 0.1,
(GLfloat) 0.1};
GLfloat S[] = {(GLfloat) 0.5,
(GLfloat) 0.5,
(GLfloat) 0.5};
GLfloat D[] = {(GLfloat) 0.5,
(GLfloat) 0.5,
(GLfloat) 0.5};
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, LP);
glLightfv(GL_LIGHT0, GL_AMBIENT, A);
glLightfv(GL_LIGHT0, GL_SPECULAR, S);
glLightfv(GL_LIGHT0, GL_DIFFUSE, D);
}
void init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_FRONT);
glEnable(GL_LIGHTING);
glClearColor(0.f, 0.f, 0.f, 1.f);
light();
objects();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float) width / (float) height, 1.0, 100);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5,
0, 0, 0,
0, 1, 0);
glCallList(quadId);
glCallList(triangleAId);
glCallList(triangleBId);
glutSwapBuffers();
}
int main(int argc, char **argv) {
int window = 0;
/* init glut */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
/* create window */
window = glutCreateWindow(TITLE);
/* set callbacks */
glutDisplayFunc(display);
glutReshapeFunc(reshape);
/* init gl, light and objects */
init();
/* enter main loop */
glutMainLoop();
/* destroy window */
glutDestroyWindow(window);
return 0;
}
// in method init objects
// vertices
glGenBuffers(1, &o.vertexId);
glBindBuffer(GL_ARRAY_BUFFER, o.vertexId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.vertices.size(), o.vertices.data(), GL_STATIC_DRAW);
// normals
glGenBuffers(1, &o.normalId);
glBindBuffer(GL_ARRAY_BUFFER, o.normalId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.normals.size(), o.normals.data(), GL_STATIC_DRAW);
// in method render objects
// set material
glMaterialfv(GL_FRONT, GL_AMBIENT, m.ambient.rgba);
glMaterialfv(GL_FRONT, GL_DIFFUSE, m.diffuse.rgba);
glMaterialfv(GL_FRONT, GL_SPECULAR, m.specular.rgba);
glMaterialf(GL_FRONT, GL_SHININESS, m.shininess);
glBindBuffer(GL_ARRAY_BUFFER, o.vertexId);
glVertexPointer(3, GL_DOUBLE, sizeof(Vertex), NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, o.normalId);
glNormalPointer(GL_DOUBLE, 0, NULL);
glEnableClientState(GL_NORMAL_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, o.vertices.size());
#ifdef __APPLE__
#include <gl.h>
#include <glu.h>
#include <glut.h>
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
static char *TITLE = "test_materials_vbos";
static GLuint verticesId = 0;
static GLuint normalsId = 0;
static GLfloat vertices[] = {0, 0, 0, 1, 0, 0, 1, 1, 0};
static GLfloat normals[] = {0, 0, 1, 0, 0, 1, 0, 0, 1};
static GLfloat RA[] = {1, 0, 0, 1};
static GLfloat RS[] = {1, 0, 0, 1};
static GLfloat RD[] = {1, 0, 0, 1};
static GLfloat GA[] = {0, 1, 0, 1};
static GLfloat GS[] = {0, 1, 0, 1};
static GLfloat GD[] = {0, 1, 0, 1};
static GLfloat Y = 0;
static void objects() {
/* vertices */
glGenBuffers(1, &verticesId);
glBindBuffer(GL_ARRAY_BUFFER, verticesId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/* normals */
glGenBuffers(1, &normalsId);
glBindBuffer(GL_ARRAY_BUFFER, normalsId);
glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
}
static void light() {
GLfloat LP[] = {0, 0, 10, 1};
GLfloat A[] = {0.1, 0.1, 0.1};
GLfloat S[] = {0.5, 0.5, 0.5};
GLfloat D[] = {0.5, 0.5, 0.5};
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, LP);
glLightfv(GL_LIGHT0, GL_AMBIENT, A);
glLightfv(GL_LIGHT0, GL_SPECULAR, S);
glLightfv(GL_LIGHT0, GL_DIFFUSE, D);
}
void init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_FRONT);
glEnable(GL_LIGHTING);
glClearColor(0.f, 0.f, 0.f, 1.f);
light();
objects();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (float) width / (float) height, 1.0, 100);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5,
0, 0, 0,
0, 1, 0);
/* red triangle */
{
glPushMatrix();
glTranslated(1, 0, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, RA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, RD);
glMaterialfv(GL_FRONT, GL_SPECULAR, RS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glBindBuffer(GL_ARRAY_BUFFER, verticesId);
glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 3, NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, normalsId);
glNormalPointer(GL_FLOAT, 0, NULL);
glEnableClientState(GL_NORMAL_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);
glPopMatrix();
}
/* green triangle */
{
glPushMatrix();
glTranslated(-1, 0, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, GA);
glMaterialfv(GL_FRONT, GL_DIFFUSE, GD);
glMaterialfv(GL_FRONT, GL_SPECULAR, GS);
glMaterialf(GL_FRONT, GL_SHININESS, Y);
glBindBuffer(GL_ARRAY_BUFFER, verticesId);
glVertexPointer(3, GL_FLOAT, sizeof(GLfloat) * 3, NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, normalsId);
glNormalPointer(GL_FLOAT, 0, NULL);
glEnableClientState(GL_NORMAL_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 3);
glPopMatrix();
}
glutSwapBuffers();
}
int main(int argc, char **argv) {
int window = 0;
/* init glut */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
/* create window */
window = glutCreateWindow(TITLE);
/* set callbacks */
glutDisplayFunc(display);
glutReshapeFunc(reshape);
/* init gl, light and objects */
init();
/* enter main loop */
glutMainLoop();
/* destroy window */
glutDestroyWindow(window);
return 0;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.normals.size(), o.normals.data(), GL_STATIC_DRAW);
// vertices
glGenBuffers(1, &o.vertexId);
glBindBuffer(GL_ARRAY_BUFFER, o.vertexId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.vertices.size(), o.vertices.data(), GL_STATIC_DRAW);
// normals
glGenBuffers(1, &o.normalId);
glBindBuffer(GL_ARRAY_BUFFER, o.normalId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * o.normals.size(), o.normals.data(), GL_STATIC_DRAW);
glPushMatrix();
glTranslated(t.translation.x, t.translation.y, t.translation.z);
glRotated(t.rotation.x, 1, 0, 0);
glRotated(t.rotation.y, 0, 1, 0);
glRotated(t.rotation.z, 0, 0, 1);
setMaterial(m);
glBindBuffer(GL_ARRAY_BUFFER, o.vertexId);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, o.normalId);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, NULL);
glDrawArrays(GL_TRIANGLES, 0, o.vertices.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glPopMatrix();
|