C++ OpenGL透视照相机
我不知道为什么代码不显示旋转立方体。我正在寻找原因。我认为这与视角或观点有关,但我不确定。我只需要理解为什么我不能在屏幕上显示一个简单的立方体C++ OpenGL透视照相机,c++,opengl,glsl,glfw,glm-math,C++,Opengl,Glsl,Glfw,Glm Math,我不知道为什么代码不显示旋转立方体。我正在寻找原因。我认为这与视角或观点有关,但我不确定。我只需要理解为什么我不能在屏幕上显示一个简单的立方体 #include <iostream> #include <GL/glew.h> // GLFW #include <GLFW/glfw3.h> #include "LoadShader.hpp" // just a function in a header that returns a GLuint for a
#include <iostream>
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
#include "LoadShader.hpp" // just a function in a header that returns a GLuint for a shaderProgram
#define FS1 "shader1.frag"
#define VS1 "shader1.vert"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
// Init GLFW
glfwInit( );
// Set all the required options for GLFW
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow *window = glfwCreateWindow( WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr );
int screenWidth, screenHeight;
glfwGetFramebufferSize( window, &screenWidth, &screenHeight );
if ( nullptr == window )
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate( );
return EXIT_FAILURE;
}
glfwMakeContextCurrent( window );
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if ( GLEW_OK != glewInit( ) )
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}
glEnable( GL_DEPTH_TEST ) ;
//glDepthFunc( GL_LESS ) ;
// Define the viewport dimensions
glViewport( 0, 0, screenWidth, screenHeight );
//glMatrixMode(GL_PROJECTION);
// Set up vertex data (and buffer(s)) and attribute pointers
GLfloat vertices[ ] =
{ // verticies a0 // colors a1
// front
-1.0, -1.0, 1.0, .70f , 0.32f , 0.31f ,
1.0, -1.0, 1.0, .20f , 0.4f , 0.21f ,
1.0, 1.0, 1.0, .50f , 0.32f , 0.71f ,
-1.0, 1.0, 1.0, .60f , 0.62f , 0.11f ,
// back
-1.0, -1.0, -1.0, .30f , 0.87f , 0.60f ,
1.0, -1.0, -1.0, .30f , 0.87f , 0.f ,
1.0, 1.0, -1.0, .10f , 0.f , 0.60f ,
-1.0, 1.0, -1.0, .20f , 0.87f , 0.60f ,
// side Right
1.0f , 1.0 , 1.0 , .45f , .3f , .4f ,
1.0 , -1.0 , 1.0 , .5f , .8f, .1f ,
1.0 , -1.0 , -1.0 , .3f , .6f , .0f ,
1.0 , 1.0 , -1.0 , .45f , .87f , .53f ,
// left Side
-1.0f , 1.0 , 1.0 , .45f , .23f , .54f ,
-1.0 , -1.0 , 1.0 , .51f , .84f, .81f ,
-1.0 , -1.0 , -1.0 , .6f , .4f , .78f ,
-1.0 , 1.0 , -1.0 , .45f , .87f , .53f ,
//top
-1.f , 1.f , 1.f , .35f , .87f , .54f ,
-1.f , 1.f , -1.f , .45f , .98f, .92f ,
1.f , 1.f , 1.f , .50f , 0.f , .32f ,
1.f , 1.f , -1.f , 1.f , .72f , .87f ,
// bottom
-1.f , -1.f , 1.f , .35f , .87f , .54f ,
-1.f , -1.f , -1.f , .45f , .98f, .92f ,
1.f , -1.f , 1.f , .50f , .89f , .32f ,
1.f , -1.f , -1.f , 0.f , .72f , .87f
};
GLuint indices[ ] =
{
0 , 1 , 2 , 0 , 2 , 3 ,
4 , 5 , 6 , 4 , 6 , 7 ,
8 , 9 , 10 , 8 , 10 , 11 ,
12 , 13 , 14 , 12 , 14 , 15 ,
16 , 19 , 18 , 16 , 17 , 19 ,
20 , 23 , 22 , 20 , 21 , 23
} ;
int numOfIndices = sizeof( indices ) / sizeof ( GLuint ) ;
std::cout << numOfIndices << std::endl ;
GLuint VAO , VBO , EBO ;
glGenVertexArrays( 1 , &VAO ) ;
glBindVertexArray( VAO ) ;
glGenBuffers( 1 , &VBO ) ;
glBindBuffer( GL_ARRAY_BUFFER , VBO ) ;
glBufferData( GL_ARRAY_BUFFER , sizeof( vertices ) , vertices , GL_STATIC_DRAW ) ;
glVertexAttribPointer( 0 , 3 , GL_FLOAT , GL_FALSE , 6 * sizeof(GLfloat) , ( GLvoid * ) 0 ) ;
glEnableVertexAttribArray( 0 ) ;
glVertexAttribPointer( 1 , 3 , GL_FLOAT , GL_FALSE , 6 * sizeof(GLfloat) , (GLvoid*)(3 * sizeof(GL_FLOAT)) ) ;
glEnableVertexAttribArray( 1 ) ;
glGenBuffers( 1 , &EBO ) ;
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , EBO ) ;
glBufferData( GL_ELEMENT_ARRAY_BUFFER , sizeof( indices ) , indices , GL_STATIC_DRAW ) ;
GLuint program = LoadShaders( VS1 , FS1 ) ;
glUseProgram( program ) ;
float rotationDegrees = 1.f ;
float fov = 1.0f ;
glm::mat4 view;
view = glm::lookAt(glm::vec3( 0.0f, 1.0f, -3.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projectionMatrix = glm::perspective( fov , ( ( float ) HEIGHT ) / WIDTH , 1.f , 10.0f ) ;
glm::mat4 modelTransform = glm::translate( glm::mat4( 1.f ) , glm::vec3( .0f , -3.0f , -3.0f ) ) ;
glm::mat4 rotation = glm::rotate( glm::mat4(1.f) , rotationDegrees , glm::vec3( 1.f , 1.f , 0.f ) ) ;
//glm::mat4 rotation = glm::rotate( rotation1 , 30.f , glm::vec3( 0.f , 2.f , 0.f ) ) ;
glm::mat4 fullTransform = projectionMatrix * modelTransform * rotation ;
GLuint transformLocation = glGetUniformLocation( program , "uModelTranslate" ) ;
GLuint projectionLocation = glGetUniformLocation( program , "uProjectionMatrix" ) ;
//modelTransform = modelTransform * rotation ;
glUniformMatrix4fv( transformLocation , 1 , GL_FALSE , &modelTransform[0][0] ) ;
glUniformMatrix4fv( projectionLocation , 1 , GL_FALSE , &fullTransform[0][0] ) ;
bool canUp , canDown , canL , canR , canW , canS ,canA , canD , canQ , canE ;
canQ = true ;
canE = true ;
canD= true ;
canA = true ;
canUp = true ;
canS = true ;
canDown = true ;
canR = true ;
canL = true ;
canW = true ;
float nearF = 0.1f ;
float zZ = -3.0 ;
float yY = 1.0f ;
float xX = 0.f ;
while ( !glfwWindowShouldClose( window ) )
{
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents( );
rotationDegrees += .0001f ;
if( rotationDegrees >= 360 )
{
rotationDegrees =0 ;
}
view = glm::lookAt(glm::vec3( yY , xX, zZ),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 rotation = glm::rotate( glm::mat4(1.f) , rotationDegrees , glm::vec3( 1.f , 1.f , 0.f ) ) ;
glm::mat4 projectionMatrix = glm::perspective(glm::radians(fov), (float) WIDTH / (float)HEIGHT , nearF, 10.0f);
glm::mat4 fullTransform = projectionMatrix * view * rotation ;
glUniformMatrix4fv( projectionLocation , 1 , GL_FALSE , &fullTransform[0][0] ) ;
// Render
// Clear the colorbuffer
glClearColor( 0.f, 0.f, 0.f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS )
{
glfwSetWindowShouldClose(window,true);
}
if (glfwGetKey(window,GLFW_KEY_UP) == GLFW_PRESS && canUp )
{
canUp = false ;
fov += 1.f ;
std::cout << fov << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_UP) != GLFW_PRESS && !canUp )
{
canUp = true ;
}
if (glfwGetKey(window,GLFW_KEY_DOWN) == GLFW_PRESS && canDown)
{
canDown = false ;
fov += -1.f ;
std::cout << fov << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_DOWN) != GLFW_PRESS && !canDown)
{
canDown = true ;
}
if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS )
{
glfwSetWindowShouldClose(window,true);
}
if (glfwGetKey(window,GLFW_KEY_LEFT) == GLFW_PRESS && canL )
{
canL = false ;
nearF += 1.f ;
std::cout << nearF << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_LEFT) != GLFW_PRESS && !canL )
{
canL = true ;
}
if (glfwGetKey(window,GLFW_KEY_RIGHT) == GLFW_PRESS && canR)
{
canR = false ;
nearF += -1.f ;
std::cout << nearF << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_RIGHT) != GLFW_PRESS && !canR)
{
canR = true ;
}
if (glfwGetKey(window,GLFW_KEY_W) == GLFW_PRESS && canW)
{
canW = false ;
zZ += -1.f ;
std::cout << zZ << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_W) != GLFW_PRESS && !canW)
{
canW = true ;
}
if (glfwGetKey(window,GLFW_KEY_S) == GLFW_PRESS && canS)
{
canS = false ;
zZ += +1.f ;
std::cout << zZ << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_S) != GLFW_PRESS && !canS)
{
canS = true ;
}
if (glfwGetKey(window,GLFW_KEY_A) == GLFW_PRESS && canA)
{
canA = false ;
xX += +1.f ;
std::cout << xX << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_A) != GLFW_PRESS && !canA)
{
canA = true ;
}
if (glfwGetKey(window,GLFW_KEY_D) == GLFW_PRESS && canD)
{
canD = false ;
xX += -1.f ;
std::cout << xX << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_D) != GLFW_PRESS && !canD)
{
canD = true ;
}
if (glfwGetKey(window,GLFW_KEY_Q) == GLFW_PRESS && canQ)
{
canQ = false ;
yY += -1.f ;
std::cout << yY << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_Q) != GLFW_PRESS && !canQ)
{
canQ = true ;
}
if (glfwGetKey(window,GLFW_KEY_E) == GLFW_PRESS && canE)
{
canE = false ;
yY += 1.f ;
std::cout << yY << std::endl ;
}
if (glfwGetKey(window,GLFW_KEY_E) != GLFW_PRESS && !canE)
{
canE = true ;
}
//glUseProgram( program ) ;
glDrawElements( GL_TRIANGLES , numOfIndices , GL_UNSIGNED_INT , 0 ) ;
// Swap the screen buffers
glfwSwapBuffers( window );
}
glfwTerminate( );
return EXIT_SUCCESS;
}
#包括
#包括
//GLFW
#包括
#包括“LoadShader.hpp”//仅在标头中包含一个函数,该函数返回着色器程序的GLuint
#定义FS1“shader1.frag”
#定义VS1“shader1.vert”
#定义GLM_启用_
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//窗口尺寸
常数胶接宽度=800,高度=600;
//主要功能是,从这里开始启动应用程序并运行游戏循环
int main()
{
//初始GLFW
glfwInit();
//为GLFW设置所有必需的选项
glfwWindowHint(GLFW_上下文_版本_专业,3);
glfwWindowHint(GLFW_上下文_版本_小调,3);
glfwWindowHint(GLFW_OPENGL_配置文件、GLFW_OPENGL_核心配置文件);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwWindowHint(GLFW_可调整大小,GL_为FALSE);
//创建可用于GLFW函数的GLFWwindow对象
GLFWwindow*window=glfwCreateWindow(宽度、高度、“学习英语”、nullptr、nullptr);
int屏幕宽度,屏幕高度;
glfwGetFramebufferSize(窗口、屏幕宽度和屏幕高度);
if(nullptr==窗口)
{
标准::cout和的角度单位是弧度。1°的视野太小了。
modelTransform
矩阵的平移组件将对象移出,并且必须进行缩减。
此外,我建议启用
e、 g:
float rotationDegrees=0.0f;
浮动视野=90.0f;
在F附近浮动=0.1f;
浮动zZ=-3.0;
浮动yY=1.0f;
浮动xX=0.0f;
glEnable(GLU深度试验);
而(!GLFWWindowsShouldClose(wnd))
{
glm::mat4旋转=glm::旋转(glm::mat4(1.0f)、glm::弧度(旋转度)、glm::vec3(1.0f、1.0f、0.0f));
glm::mat4 modelTransform=glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,0.5f,0.0f));
glm::mat4 view=glm::lookAt(glm::vec3(yY,xX,zZ),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));
glm::mat4模型视图=视图*模型变换*旋转;
glm::mat4 projectionMatrix=glm::perspective(glm::弧度(fov),(浮动)宽度/(浮动)高度,近f,10.0f);
旋转度+=1.0f;
glUniformMatrix4fv(transformLocation,1,GL_FALSE,glm::value_ptr(modelview));
glUniformMatrix4fv(projectionLocation,1,GL_FALSE,glm::value_ptr(projectionMatrix));
// [...]
}
顶点着色器:
#版本330核心
vec3 INPO中的布局(位置=0);
vec3彩色中的布局(位置=1);
输出vec3-vertCol;
均匀mat4映射矩阵;
均匀mat4 uModelTranslate;
void main()
{
vertCol=不着色;
gl_Position=uProjectionMatrix*uModelTranslate*vec4(inPos.xyz,1.0);
}
和的角度单位是弧度。1°的视野太小了。
modelTransform
矩阵的平移组件将对象移出,并且必须进行缩减。
此外,我建议启用
e、 g:
float rotationDegrees=0.0f;
浮动视野=90.0f;
在F附近浮动=0.1f;
浮动zZ=-3.0;
浮动yY=1.0f;
浮动xX=0.0f;
glEnable(GLU深度试验);
而(!GLFWWindowsShouldClose(wnd))
{
glm::mat4旋转=glm::旋转(glm::mat4(1.0f)、glm::弧度(旋转度)、glm::vec3(1.0f、1.0f、0.0f));
glm::mat4 modelTransform=glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,0.5f,0.0f));
glm::mat4 view=glm::lookAt(glm::vec3(yY,xX,zZ),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));
glm::mat4模型视图=视图*模型变换*旋转;
glm::mat4 projectionMatrix=glm::perspective(glm::弧度(fov),(浮动)宽度/(浮动)高度,近f,10.0f);
旋转度+=1.0f;
glUniformMatrix4fv(transformLocation,1,GL_FALSE,glm::value_ptr(modelview));
glUniformMatrix4fv(projectionLocation,1,GL_FALSE,glm::value_ptr(projectionMatrix));
// [...]
}
顶点着色器:
#版本330核心
vec3 INPO中的布局(位置=0);
vec3彩色中的布局(位置=1);
输出vec3-vertCol;
均匀mat4映射矩阵;
均匀mat4 uModelTranslate;
void main()
{
vertCol=不着色;
gl_Position=uProjectionMatrix*uModelTranslate*vec4(inPos.xyz,1.0);
}
据我们所知,您的着色器已损坏。请在中编辑。可以随意用作基础。为什么要在一个glm::perspective()中使用glm::radians()
call,但不是另一个?我不确定它是否与glm弧度有区别。我这里的大部分内容都来自一个示例。我的目标是在3d空间中创建一个立方体,并使其旋转,它实际上看起来像一个立方体,而不是一个倾斜的矩形。如果运行我的代码,你将能够使用键盘调整m中的变量我把它们放在那里是为了尝试为立方体获得正确的设置,但我一直没能做到。据我们所知,你的着色器已经损坏了。在a中编辑。可以随意用作基础。为什么你要在一个glm::perspective()中使用glm::radians()
call,但不是另一个?我不确定它是否与glm弧度有区别。我这里的大部分内容都来自一个示例。我的目标是在3d空间中创建一个立方体,并使其旋转,它实际上看起来像一个立方体,而不是一个倾斜的矩形。如果运行我的代码,你将能够使用键盘调整m中的变量我把它们放在那里是为了尝试为多维数据集获得正确的设置,但我一直无法做到。你能确认编译时所做的更改对你有效吗?你能确认编译时所做的更改对你有效吗?