2017年10月19日 星期四

week06 - shading

position陣列
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };

createwindow之後
放入
 glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
mainloop之前


mouse:
  button哪個鍵 state 0按下1起來

motion:
  滑鼠按下任何一個鍵

-------------------------------------------------------------------------------------
#include <GL/glut.h>
#include "glm.h"
#include <stdio.h>
GLMmodel* pmodel = NULL;
int rotatex=0, rotatey=0, rotatez=0;
int oldX = 0, oldY = 0;

void keyboard(unsigned char key, int x, int y)
{
    printf("now: %c (%d %d)\n", key, x, y);
    if (key == 'z') { rotatex++; }
    if (key == 'x') { rotatey++; }
    if (key == 'c') { rotatez++; }
    glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
    printf("button:%d state:%d (%d %d)\n", button, state, x, y);
    if (state == 0)
    {
        oldX=x;
        oldY=y;
    }
}

void motion(int x, int y)
{
    rotatey += -(x-oldX);
    rotatex += -(y-oldY);
    oldX=x; oldY=y;
    glutPostRedisplay();
}

void drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }

    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(rotatex, 1, 0, 0);
        glRotatef(rotatey, 0, 1, 0);
        glRotatef(rotatez, 0, 0, 1);
        drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}

GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week05");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);

    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
}

沒有留言:

張貼留言