opengl的問題

這是是我作業二的程式 想請問是要加什麼 才能就是我老師所要的

謝謝

==========================================================

修改你作業二的程式,使得螢幕視窗中顯示任意三個不同形狀的實體模型物件,請設定這些物件的 materials 屬性,使其顯示不同的顏色。

=========================================================

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <GL/glut.h>

void MyTranslate(GLfloat x, GLfloat y, GLfloat z)

{

GLfloat m[] = {1.0f, 0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f, 0.0f,

0.0f, 0.0f, 1.0f, 0.0f,

0.0f, 0.0f, 0.0f, 1.0f };

glRotatef(85.0,1.0,0.0,0.0);

#define M(row, col) m[col*4+row]

M(0, 3) = x;

M(1, 3) = y;

M(2, 3) = z;

#undef M

glMultMatrixf(m);

}

void OnSize(int w, int h)

{

GLfloat fAspect;

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

fAspect = (GLfloat)w /(GLfloat)h;

gluPerspective(45.0f, fAspect, 1.0, 425.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

}

void OnDraw(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0.0f, 15.0f, 40.0f,

0.0f, 0.0f, 0.0f,

0.0f, 2.0f, 0.0f);

glColor3f(0.0, 1.0, 0.0);

glPushMatrix();

MyTranslate(-10, 0, 0);

glutWireCube (5.0f);

MyTranslate(20, 0, 0);

glutWireSphere (5.0f,10,10);

MyTranslate(-10, 1, -15);

glutWireCone (5.0f,8.0f,10,10);

glPopMatrix();

glFlush();

}

Update:

int main(int argc, char **argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500,500);

glutCreateWindow("123456");

glutReshapeFunc(OnSize);

glutDisplayFunc(OnDraw);

glClearColor(0.0, 0.0, 0.0, 0.0 );

glutMainLoop();

return 0;

}

Update 2:

我全部的原始碼都PO上來了= =

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    我對 OpenGL 知道的也不是很多。但就我所知,要改的東西不少耶!而且,要改的部份,你都沒 post 上來!

    在 Global Variable 裡,你要有

    float ambient[] = {0.1, 0.1, 0.1, 1.0}; // 不一定是這些數值

    float diffuse[] = {1.0, 1.0, 1.0, 0.5};

    float light_pos[] = {0.0, 3.0, 0.0, 1.0};

    float specular[] = {1.0, 1.0, 1.0, 0.5};

    在 GL init 裡,你(可能)要有:(至少,position一定要)

    glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);

    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);

    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

    在 display 裡(或GL init 裡),你要有

    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); // GL_FRONT也可,比較快

    而你在畫每個 Obj 前,要對OpenGL說這東東的 diffuse, ambient, specular和 shineness。如:

    glMaterialfv(GL_FRONT, GL_AMBIENT, Obj[i].getAmbient());

    glMaterialfv(GL_FRONT, GL_DIFFUSE, Obj[i].getDiffuse());

    glMaterialfv(GL_FRONT, GL_SPECULAR, Obj[i].getSpecular());

    glMaterialfv(GL_FRONT, GL_SHININESS, mat);

    加油!!^_^

    2007-01-14 11:14:24 補充:

    那你就把該加的加上吧!!

    你沒設 Material,GL/ glut 都不可能會知道你的 material 要做啥處理

  • 1 decade ago

    老師的ppt 中都有

    by 同班

Still have questions? Get your answers by asking now.