我正在尝试制作一个简单的程序,围绕窗口旋转立方体。当我运行它时,看起来立方体面部首先以正确的顺序呈现,但是一旦它们旋转到后面,它们就会通过前面而不是其他面部显示出来。我正在使用glEnable(GL_DEPTH_TEST),glenable(GL_CULL_FACE)和glcullface(GL_BACK),我认为它会照顾深度测试,但它仍然无法正常工作。这是我的剧本,`
int windowwidth = 1920;
int windowheight = 1080;
GLfloat aspectratio = 16 / 9.0;
double horizontalposition = 0;
double verticalposition = 0;
double depthposition = 0;
GLfloat projTop = 1;
GLfloat projBottom = 0;
GLfloat floatOne = 1.0;
GLfloat oneFourth = 1.0 / 4;
GLfloat projLeft = 0;
GLfloat projRight = 1;
GLfloat zNear = .1;
GLfloat zFar = 1;
GLfloat zRange = zNear - zFar;
GLuint puddletexture;
GLuint sampler = 0;
GLuint samplerUniform;
GLuint shaderProgram;
GLuint textureunit = 0;
GLuint positionUniform, colourUniform, rotationViewUniform, projectionUniform,translationUniform, rotationUniform, timeUniform, transformUniform;
GLuint colourAttribute,secondColourAttribute, positionAttribute, textureAttribute, indexAttribute;
GLuint vertexArrayObject,
vertexBuffer;
GLuint vao, vbo[3];
GLuint triangleVao, triangleVbo[2];
GLuint elementbuffer;
GLuint triangleArrayObject, triangleVertexBuffer;
GLfloat diamond[] = {
-1, 1, 1, // vertex[0]
1, 1, 1, // vertex[1]
1, -1, 1, // vertex[2]
-1, -1, 1, // vertex[3]
-1, 1, -1, // vertex[4]
1, 1, -1, // vertex[5]
1, -1, -1, // vertex[6]
-1, -1, -1, // vertex[7]
};
GLuint cubeindicies[] = {
0, 1, 2, 2, 3, 0, // front face
3, 2, 6, 6, 7, 3, // top face
7, 6, 5, 5, 4, 7, // back face
4, 0, 3, 3, 7, 4, // left face
0, 1, 5, 5, 4, 0, // bottom face
1, 5, 6, 6, 2, 1,
};
GLfloat colors[4][4] = {
{ 1.0, 0.0, 0.0, 0.0}, /* Red */
{ 1.0, 1.0, 0.0, 0.0}, /* Green */
{ 0.0, 0.0, 1.0, 0.0}, /* Blue */
{ 1.0, 1.0, 1.0, 0.0},
};
GLfloat picturetexturecoordinates[] = {
0,1,0,
0,0,0,
1,0,0,
1,1,0,
};
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
gluPerspective(45.0f, (float)w / (float)h, 1.0f, 100.0f);
windowwidth = glutGet(GLUT_WINDOW_WIDTH);
windowheight = glutGet(GLUT_WINDOW_HEIGHT);
aspectratio = (double)windowwidth / windowheight;
}
void animate() {
glutPostRedisplay();
}
void visible(int vis) {
if (vis == GLUT_VISIBLE)
glutIdleFunc(animate);
else
glutIdleFunc(0);
}
void loadShader();
void display();
void loadShader() {
shaderProgram = InitShader("beelsebob.vert", "beelsebob.frag", "fragColour");
positionUniform = glGetUniformLocation(shaderProgram, "p");
if (positionUniform < 0) {
std::cerr << "Shader did not contain the 'p' uniform" << std::endl;
}
colourUniform = glGetUniformLocation(shaderProgram, "c");
if (colourUniform < 0) {
std::cerr << "Shader did not contain the 'c' uniform" << std::endl;
}
timeUniform = glGetUniformLocation(shaderProgram, "timer");
if (timeUniform < 0) {
std::cerr << "Shader did not contain the 'timer' uniform" << std::endl;
}
samplerUniform = glGetUniformLocation(shaderProgram, "textSampler");
if (samplerUniform < 0) {
std::cerr << "Shader did not contain the 'textSampler' uniform" << std::endl;
}
projectionUniform = glGetUniformLocation(shaderProgram, "projectionMatrix");
if (projectionUniform < 0) {
std::cerr << "Shader did not contain the 'projectMatrix' uniform" << std::endl;
}
rotationViewUniform = glGetUniformLocation(shaderProgram, "rotationViewMatrix");
if (rotationViewUniform < 0) {
std::cerr << "Shader did not contain the 'rotationViewMatrix' uniform" << std::endl;
}
transformUniform = glGetUniformLocation(shaderProgram, "transformMatrix");
if (transformUniform < 0) {
std::cerr << "Shader did not contain the 'transformMatrix' uniform" << std::endl;
}
translationUniform = glGetUniformLocation(shaderProgram, "translationMatrix");
if (translationUniform < 0) {
std::cerr << "Shader did not contain the 'translationMatrix' uniform" << std::endl;
}
translationUniform = glGetUniformLocation(shaderProgram, "translationMatrix");
if (translationUniform < 0) {
std::cerr << "Shader did not contain the 'translationMatrix' uniform" << std::endl;
}
colourAttribute = glGetAttribLocation(shaderProgram, "colour");
if (colourAttribute < 0) {
std::cerr << "Shader did not contain the 'colour' attribute." << std::endl;
}
secondColourAttribute = glGetAttribLocation(shaderProgram, "secondColour");
if (secondColourAttribute < 0) {
std::cerr << "Shader did not contain the 'secondColour' attribute." << std::endl;
}
positionAttribute = glGetAttribLocation(shaderProgram, "position");
if (positionAttribute < 0) {
std::cerr << "Shader did not contain the 'position' attribute." << std::endl;
}
textureAttribute = glGetAttribLocation(shaderProgram, "textcoord");
if (textureAttribute < 0) {
std::cerr << "Shader did not contain the 'textcoord' attribute." << std::endl;
}
}
void display() {
glClearColor(0.0, 0.0, .2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GLUT_MULTISAMPLE);
glLoadIdentity();
const float timeScale = 0.0008f;
glUseProgram(shaderProgram);
GLfloat timeValue = glutGet(GLUT_ELAPSED_TIME)*timeScale;
vec3 p3(.5f*sin(timeValue) ,0 , 0.5f*cosf(timeValue));
//vec4 c4(.5f*sinf(timeValue), .1f*sinf(timeValue), 9*cosf(timeValue), 1.0);
vec4 c4(.7f, .5f, 0.01, 0.0);
mat4 rotationMat(
{ cos(timeValue), sin(timeValue), 0,0 },
{ 0,1,0,0 },
{ -sin(timeValue), cos(timeValue), 1,0 },
{ 0,0,0,1 }
);
mat4 transMat(
{1,0,0,0},
{0,1,0,0},
{0,0,(GLfloat)(windowheight*1.0/windowwidth),0},
{0,0,0,1}
);
mat4 projMat(
{zFar/aspectratio, 0, 0, 0 },
{ 0, zFar, 0, 0 },
{ 0,0, (zFar+zNear)/(zNear-zFar), (2*zFar*zNear)/(zNear-zFar) },
{ 0,0,0,1}
);
mat4 completeTransformMatrix = projMat*transMat*rotationMat;
glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, projMat);
glUniformMatrix4fv(rotationViewUniform, 1, GL_FALSE, rotationMat);
glUniformMatrix4fv(translationUniform, 1, GL_FALSE, transMat);
glUniformMatrix4fv(transformUniform, 1, GL_FALSE, completeTransformMatrix);
glUniform3fv(positionUniform,1,(const GLfloat*)&p3);
glUniform4fv(colourUniform, 1, (const GLfloat*)&c4);
glUniform1i(samplerUniform, 0);
glUniform1fv(timeUniform, 1, (const GLfloat*)&timeValue);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, squareTexture);
glGenSamplers(1, &sampler);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindSampler(0, sampler);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, sizeof(cubeindicies)/sizeof(GLuint), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glutSwapBuffers();
}
double r = 0;
void loadBufferData() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GLUT_MULTISAMPLE);
glEnable(GL_PERSPECTIVE_CORRECTION_HINT);
Image* squareImage = loadBMP("puddletexture.bmp");
squareTexture = loadTexture(squareImage);
delete squareImage;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(4, vbo);
glEnableVertexAttribArray(positionAttribute);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(diamond), diamond, GL_STATIC_DRAW);
glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(colourAttribute);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glVertexAttribPointer(colourAttribute, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(textureAttribute);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(picturetexturecoordinates), picturetexturecoordinates, GL_STATIC_DRAW);
glVertexAttribPointer(textureAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(indexAttribute);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeindicies), cubeindicies, GL_STATIC_DRAW);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion(3, 2);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("02561");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutVisibilityFunc(visible);
glutIdleFunc(animate);
glutReshapeWindow(600, 400);
glewExperimental = GL_TRUE;
GLint GlewInitResult = glewInit();
if (GlewInitResult != GLEW_OK) {
printf("ERROR: %s\n", glewGetErrorString(GlewInitResult));
}
loadShader();
loadBufferData();
glutMainLoop();
}
顶点着色器:
#version 330
uniform vec3 p;
uniform mat4 transformMatrix;
uniform vec4 c;
in vec4 position;
in vec4 colour;
out vec4 colourV;
in vec4 secondColour;
out vec4 secondColourV;
in vec2 textcoord;
out vec2 textcoordV;
void main (void)
{
colourV = colour;
colourV += c;
textcoordV = textcoord;
gl_Position = transformMatrix*position + vec4(p, 3.0);
}
和片段着色器:
#version 330
uniform sampler2D textSampler;
in vec4 colourV;
in vec4 secondColourV;
out vec4 fragColour;
in vec2 textcoordV;
out vec2 fragTexture;
void main(void)
{
fragColour = colourV*texture(textSampler, textcoordV);
}
这是一个图像,当我运行它时,它会在窗口中显示几个帧,
windowframes 所以现在我被卡住了,有谁知道这可能是什么问题?
答案 0 :(得分:1)
你所有的矩阵似乎都错了。
function build($file_name = null, $gzip = NULL)
{
$map = $this->CI->config->item('sitemaps_header') . "\n";
$xml=simplexml_load_file($file_name);
foreach($xml->children() as $books) {
$books['loc'] = htmlentities($books['loc'], ENT_QUOTES);
$map .= "\t<url>\n\t\t<loc>" . $books->loc . "</loc>\n";
$map .= "\t\t<lastmod>" . $books->lastmod . "</lastmod>\n";
$map .= "\t\t<changefreq>" . $books->changefreq . "</changefreq>\n";
$map .= "\t\t<priority>" . $books->priority . "</priority>\n";
$map .= "\t</url>\n\n";
}
foreach ($this->items as $item)
{
$item['loc'] = htmlentities($item['loc'], ENT_QUOTES);
$map .= "\t<url>\n\t\t<loc>" . $item['loc'] . "</loc>\n";
$attributes = array("lastmod", "changefreq", "priority");
foreach ($attributes AS $attr)
{
if (isset($item[$attr]))
{
$map .= "\t\t<$attr>" . $item[$attr] . "</$attr>\n";
}
}
$map .= "\t</url>\n\n";
}
//unset($this->items);
$map .= $this->CI->config->item('sitemaps_footer');
if (!is_null($file_name))
{
$fh = fopen($file_name, 'w');
if (!$fh)
{
$this->set_error('Could not open sitemaps file for writing: ' . $file_name);
return FALSE;
}
if (!fwrite($fh, $map))
{
$this->set_error('Error writing to sitemaps file: ' . $file_name);
return FALSE;
}
fclose($fh);
if ($this->CI->config->item('sitemaps_filesize_error') && filesize($file_name) > 1024 * 1024 * 10)
{
$this->set_error('Your sitemap is bigger than 10MB, most search engines will not accept it.');
return FALSE;
}
return $file_name;
}
else
{
return $map;
}
}
不是有效的旋转矩阵。所有这些都具有共同点,即与旋转轴对应的行和列不受影响。 (x-rotation的第一行和第一列设置为rotationMat
,y-rotation的第二行和第二列设置为[1,0,0,0]
)。在矩阵中,第二行和第三列未设置。 Source
[0,1,0,0]
并不一定是翻译矩阵,尽管用法看起来应该是一个。目前,执行沿z轴的缩放。
transMat
完全错了。目前这是一种奇怪的正交投影,但不知何故根据远平面值投影x和y轴?如果您想编写透视投影矩阵,则前两行中的projMat
应为zFar
,第四行应为cotan(field_of_view / 2)
。 Source
由于您在编写正确的矩阵时似乎遇到了很多问题,我强烈建议您使用库(例如glm)。
此外,您正在设置着色器中不存在的制服(例如[0, 0, -1, 0]
),您在设置代码中检查这些制服是否存在,但是然后尝试在{{1无论这种检查如何,都应该导致OpenGL错误(检查translationMatrix
)。