我使用VBO编写了一个小的OpengL测试程序。我在(0.0,5.0,0.0)中有一个点,一个相机也在(0.0,5.0,0.0)中,它看着点(0.0,0.0,0.0)。我的飞机位于中心位置(0.0,0.0,0.0)。因此,通常,平面上的所有像素应具有相同的值。但事实并非如此。我尝试了几种代码组合,结果相同。
这是一张场景图片:
正如你所看到的那样,这个位置似乎位于左侧,但它应该被定位为相机。这不符合逻辑。
这是我的代码:
#include <iostream>
#include <SDL/SDL.h>
#include <gl/glew.h>
#define OFFSET_BUFFER(offset) ((char*)NULL + (offset))
static GLfloat position[4] = {0.0f, 5.0f, 0.0f, 1.0f};
static GLfloat diffuse[3] = {0.40f, 0.40f, 0.40f};
static GLfloat specular[3] = {0.80f, 0.80f, 0.80f};
static GLfloat emissive[3] = {0.00f, 0.00f, 0.20f};
const static int WIDTH = 640;
const static int HEIGHT = 480;
struct Vertex
{
float x, y, z;
float nx, ny, nz;
};
static Vertex vertex[24] =
{
-1.000000f, 0.000000f, 1.000000f, //V1
1.000000f, 0.000000f, 1.000000f, //N1
1.000000f, 0.000000f, 1.000000f, //V2
1.000000f, 0.000000f, 1.000000f, //N2
1.000000f, 0.000000f, -1.000000f, //V3
1.000000f, 0.000000f, 1.000000f, //N3
-1.000000f, 0.000000f, -1.000000f, //V4
1.000000f, 0.000000f, 1.000000f //N4
};
static void eventListener(SDL_Event *pEvent, bool *pContinue)
{
while (SDL_PollEvent(pEvent))
{
switch(pEvent->type)
{
case SDL_QUIT:
*pContinue = false;
break;
case SDL_KEYDOWN:
switch (pEvent->key.keysym.sym)
{
case SDLK_ESCAPE:
*pContinue = false;
break;
}
}
}
}
int main(int ac, char **av)
{
bool continuer = true;
SDL_Event event;
GLuint vboID = 0;
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("VBO tests",NULL);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
//Initialize projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, WIDTH, HEIGHT);
gluPerspective(60.0, (float)(WIDTH/HEIGHT), 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Initialize light settings
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emissive);
glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 100);
//Initialize VBO settings
glewInit();
int vertexSize = 4 * sizeof(Vertex);
if (glIsBuffer(vboID) == GL_TRUE)
glDeleteBuffers(1, &vboID);
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vertexSize, vertex, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Loop
while (continuer)
{
eventListener(&event, &continuer);
glClearDepth(1.0f);
glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
//Lock buffer
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), OFFSET_BUFFER(0));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(Vertex), OFFSET_BUFFER(3 * sizeof(GLfloat)));
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
//Unlock buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return (0);
}
光度似乎有效,但渲染不正确。 Vertex结构中的值来自于从Blender导出的简单平面网格文件,因此我认为数据是正确的。有人可以帮助我吗?
答案 0 :(得分:0)
检查你的几何和法向量。你的法线向量在哪里看?你有一个非常奇怪的几何和视图设置,最好不要使用旧的固定功能管道。
答案 1 :(得分:0)
我注意到提供的源代码存在一对问题:
法线用于计算具有光方向的点积(在光方程中除其他之外)。如果你的光线位于(0,5,0)并且法线指向(1,0,1),这将产生阴影颜色,因为它们使这些顶点像面向位置(sqrt(2),0,sqrt(2) )),但你的四边形面向(0,1,0)。