我正在尝试提供http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
的教程这里是完整的代码:
//============================================================================
// Name : TestOpenGLGLFW.cpp
// Author : Manohar Kuse
// Version :
// Copyright : GNU Licence
// Description : http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/
//============================================================================
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include "LoadShader.h"
int main( int argc, char ** argv )
{
// --1-- Initialise GLFW
if( glfwInit() == false )
{
std::cerr << "Failed to initialize GLFW3\nQuiting....\n";
exit(1);
}
// --2-- Create Window
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
GLFWwindow * window=NULL;
window = glfwCreateWindow( 400, 400, "hello", NULL, NULL );
if( window == NULL )
{
std::cerr << "Failed to create glfw window\nQuiting....\n";
glfwTerminate();
exit(1);
}
// --3-- Make current context
glfwMakeContextCurrent( window );
// --4-- GLEW (Wrangler lib)
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW\n";
glfwTerminate();
exit(1);
}
// --4.1-- Create a VAO
GLuint vertexArrayId;
glGenVertexArrays( 1, &vertexArrayId ); // generate just 1 vertex array
glBindVertexArray( vertexArrayId );
// --4.2-- Define a float array, denoting 3 cords of a triangle
GLfloat vertexData[] = {
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0
};
// --4.3-- Create a VBO (vertex buffer)
GLuint vertexBufferId;
glGenBuffers( 1, &vertexBufferId );
glBindBuffer( GL_ARRAY_BUFFER, vertexBufferId );
//give our vertices to OpenGL
glBufferData( GL_ARRAY_BUFFER, sizeof( vertexData), vertexData, GL_STATIC_DRAW );
// --4.4-- Load vertex & fragment shaders
GLuint programID = LoadShaders( "src/shaders/vertex.shader", "src/shaders/fragment.shader" );
// --5-- Main loop
glfwSetInputMode( window, GLFW_STICKY_KEYS, GL_TRUE );
glClearColor(0,0,.4,0);
glm::mat4 Projection = glm::perspective(45.0f, 4.0f/3.0f, .1f, 100.0f );
//glm::mat4 Projection = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f );
glm::mat4 View = glm::lookAt( glm::vec3(4,3,3), glm::vec3(0,0,0), glm::vec3(0,1,0) );
glm::mat4 Model = glm::mat4( 1.0 );
glm::mat4 MVP = Projection * View * Model;
GLuint matrixId = glGetUniformLocation( programID, "MVP" );
glUniformMatrix4fv(matrixId, 1, GL_FALSE, &MVP[0][0]);
do
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram( programID );
// draw
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
// swap buffers
glfwSwapBuffers( window );
glfwPollEvents();
}
while( glfwWindowShouldClose(window)==false );
}
顶点着色器:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP ;
void main(){
vec4 v = vec4(vertexPosition_modelspace,1); // Transform an homogeneous 4D vector, remember ?
gl_Position = MVP *v;
}
Fragment Shader:
#version 330 core
out vec3 color;
void main(){
color = vec3(1,0,0);
}
我看不到我的三角形是画画,而是看到背景颜色。但是,如果我删除转换代码(Model&lt; View,Projection),我会看到三角形。可能是什么问题?
答案 0 :(得分:2)
glUniform*()
函数为当前绑定程序设置制服。因此,您需要在glUseProgram()
来电之前移动glUniformMatrix4fv()
来电:
glUseProgram(programID);
glUniformMatrix4fv(matrixId, 1, GL_FALSE, &MVP[0][0]);
每当某些内容在您的OpenGL代码中无法正常工作时,您应该调用glGetError()
,并查看它是否报告任何错误。在这种情况下,您将从glUniformMatrix4fv()
收到错误。来自man page的错误列表:
如果没有当前的程序对象,则生成GL_INVALID_OPERATION。
OpenGL 4.1引入了一组入口点,用于设置统一值而不绑定程序。在这种情况下,将应用以下调用:
glProgramUniformMatrix4fv(programID, matrixId, 1, GL_FALSE, &MVP[0][0]);