Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 两个立方体在窗口中看不到如何修复_C++_Opengl_Glew_Orthographic - Fatal编程技术网

C++ 两个立方体在窗口中看不到如何修复

C++ 两个立方体在窗口中看不到如何修复,c++,opengl,glew,orthographic,C++,Opengl,Glew,Orthographic,我试着画两个自动旋转的立方体,但我没有看到这个立方体。现在有人知道怎么解决这个问题了吗 另外,我想在这个程序中添加一个摄像头。你能帮我做这个吗 #包括 #包括 #包括 #包括 #定义屏幕宽度1920 #定义屏幕高度1080 void keyCallback(GLFWwindow*窗口、int键、int扫描码、int操作、int mods); void DrawCube(GLfloat centerPosX、GLfloat centerPosY、GLfloat centerPosZ、GLfloa

我试着画两个自动旋转的立方体,但我没有看到这个立方体。现在有人知道怎么解决这个问题了吗

另外,我想在这个程序中添加一个摄像头。你能帮我做这个吗

#包括
#包括
#包括
#包括
#定义屏幕宽度1920
#定义屏幕高度1080
void keyCallback(GLFWwindow*窗口、int键、int扫描码、int操作、int mods);
void DrawCube(GLfloat centerPosX、GLfloat centerPosY、GLfloat centerPosZ、GLfloat edgeLength);
GLfloat旋转X=0.0f;
GLfloat旋转Y=0.0f;
GLRTRI;//三角形的角度(新)
glrquad;
内部主(空)
{ 
GLFWwindow*窗口;
//初始化库
如果(!glfwInit())
{
返回-1;
}
//创建窗口模式窗口及其OpenGL上下文
window=glfwCreateWindow(屏幕宽度,屏幕高度,“Hello World”,NULL,NULL);
glfwSetKeyCallback(窗口,keyCallback);
glfwSetInputMode(窗口,GLFW_粘滞键,1);
int屏幕宽度,屏幕高度;
glfwGetFramebufferSize(窗口、屏幕宽度和屏幕高度);
如果(!窗口)
{
glfwTerminate();
返回-1;
}
//将窗口的上下文设置为当前
glfwMakeContextCurrent(窗口);
glViewport(0.0f,0.0f,screenWidth,screenHeight);//指定OpenGL将绘制到的窗口部分(以像素为单位),从标准化转换为像素
glMatrixMode(GL_PROJECTION);//投影矩阵定义了在世界坐标系中查看对象的摄影机的属性。这里通常设置缩放因子、纵横比以及近剪裁平面和远剪裁平面
glLoadIdentity();//用恒等矩阵替换当前矩阵并重新开始,因为矩阵变换(如glOrpho和glRotate Cumultate)基本上将我们置于(0,0,0)
glOrtho(0,屏幕宽度,0,屏幕高度,0,1000);//基本设置坐标系
glMatrixMode(GL_MODELVIEW);/(默认矩阵模式)MODELVIEW矩阵定义对象在世界中的变换方式(表示平移、旋转和缩放)
glLoadIdentity();//与上面的注释相同
GLfloat半屏幕宽度=屏幕宽度/2;
GLfloat半屏幕高度=屏幕高度/2;
//循环,直到用户关闭窗口
而(!glfwWindowShouldClose(窗口))
{
glShadeModel(GL_平滑);
glClearColor(0.6f、1.0f、1.0f、0.1f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);//启用深度测试
glDepthFunc(GL_LEQUAL);//要进行的深度测试类型
glHint(GL\u透视图\u校正\u提示,GL\u最佳);
//在此处渲染OpenGL
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glPushMatrix();
glLoadIdentity();
GLTRANSTEF(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f);//向左移动1.5个单位,进入屏幕6.0
DrawCube(半屏幕宽度,半屏幕高度,-500,200);
glLoadIdentity();//重置当前模型视图矩阵
glTranslatef(1.5f,0.0f,-7.0f);
glRotatef(rquad,1.0f,1.0f,1.0f);
DrawCube(半屏幕宽度,半屏幕高度,-500,200);
glPopMatrix();
rtri+=0.2f;//增加三角形的旋转变量(新)
rquad-=0.15f;
//交换前后缓冲区
glfwSwapBuffers(窗口);
//轮询并处理事件
glfwPollEvents();
}
glfwTerminate();
返回0;
}
void keyCallback(GLFWwindow*窗口、int键、int扫描码、int操作、int mods)
{

//std::cout首先,我建议将您的原点放在视口的中心:

glOrtho(-SCREEN_WIDTH*0.5,  SCREEN_WIDTH*0.5,
        -SCREEN_HEIGHT*0.5, SCREEN_HEIGHT*0.5,
        0.0, 1000.0);
模型视图矩阵由视图矩阵和模型矩阵组成。 视图矩阵描述了如何查看场景,而模型矩阵描述了几何体在场景中的位置

对于视图矩阵,您选择了标识矩阵

对于模型矩阵,您确实正确设置了旋转。您还计算了模型的平移和比例,但从未使用过。 在绘制之前,您没有平移和缩放模型视图矩阵

GLfloat halfScreenWidth = SCREEN_WIDTH * 0.5;
GLfloat halfScreenHeight = SCREEN_HEIGHT * 0.5;

GLfloat sideLenght = 200.0f;
GLfloat halfSideLenght = sideLenght * 0.5;

while ( ... )
{
    ...

    // backup the view matrix
    glPushMatrix();

    // set up the model matrix
    glTranslatef(-0.5f * halfScreenWidth, 0.0f, -6.0f);         // translate the model   
    glRotatef(rtri, 0.0f, 1.0f, 0.0f);                          // rotate the model
    glScalef( halfSideLenght, halfSideLenght, halfSideLenght ); // scale the model

    // draw the 1. cube
    DrawCube(/* I removed the parameter here, because they are not used */);

    // restor the view matrix
    glPopMatrix();

    // backup the view matrix
    glPushMatrix();

    // set up the model matrix
    glTranslatef(0.5f * halfScreenWidth, 0.0f, -7.0f);          // translate the model
    glRotatef(rquad, 1.0f, 1.0f, 1.0f);                         // rotate the model
    glScalef( halfSideLenght, halfSideLenght, halfSideLenght ); // scale the model

    // draw the 2. cube
    DrawCube(/* I removed the parameter here, because they are not used */);

    // restor the view matrix
    glPopMatrix();

  ...
}

你确定你的立方体不是很小吗?因为你用2的边长绘制它们,所以它们的当前投影应该是2x2像素。第一个立方体也将离开屏幕,因为x坐标范围从-2.5到-0.5。问题的第二部分(关于相机)也太宽了。你在那里试过什么,什么不起作用?哦。谢谢你如何解决这个问题?如何改变坐标范围?如何改变立方体大小。对不起,这是我的第一个程序,我不知道这么多=(这里你最多可以犯10个不同的错误..深度缓冲区:清除和测试,对象定位,背面剔除,…我强烈建议你从一个简单的三角形示例中一步一步地工作。更改立方体中的三角形,然后翻译它,然后使用深度缓冲区…享受旅程。好的,谢谢我尝试了=)
GLfloat halfScreenWidth = SCREEN_WIDTH * 0.5;
GLfloat halfScreenHeight = SCREEN_HEIGHT * 0.5;

GLfloat sideLenght = 200.0f;
GLfloat halfSideLenght = sideLenght * 0.5;

while ( ... )
{
    ...

    // backup the view matrix
    glPushMatrix();

    // set up the model matrix
    glTranslatef(-0.5f * halfScreenWidth, 0.0f, -6.0f);         // translate the model   
    glRotatef(rtri, 0.0f, 1.0f, 0.0f);                          // rotate the model
    glScalef( halfSideLenght, halfSideLenght, halfSideLenght ); // scale the model

    // draw the 1. cube
    DrawCube(/* I removed the parameter here, because they are not used */);

    // restor the view matrix
    glPopMatrix();

    // backup the view matrix
    glPushMatrix();

    // set up the model matrix
    glTranslatef(0.5f * halfScreenWidth, 0.0f, -7.0f);          // translate the model
    glRotatef(rquad, 1.0f, 1.0f, 1.0f);                         // rotate the model
    glScalef( halfSideLenght, halfSideLenght, halfSideLenght ); // scale the model

    // draw the 2. cube
    DrawCube(/* I removed the parameter here, because they are not used */);

    // restor the view matrix
    glPopMatrix();

  ...
}