Modern OpenGL MacOS Only Black Screen(现代OpenGL MacOS纯黑屏)
本文介绍了现代OpenGL MacOS纯黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习C++中OpenGL的教程(YouTube上的Cherno)。我有以下代码,但无论我尝试什么,我都无法画出一个三角形。创建了一个窗口,我没有得到任何错误等,我甚至可以用glClearColor更改背景颜色。但没有三角形!
仅供参考,我是通过Homebrew安装GLFW/Glew的,我使用的是Clion。
CMakeList.txt
cmake_minimum_required(VERSION 3.3)
project(Lib_Test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")
set(SOURCE_FILES src/main.cpp CMakeLists.txt)
# add extra include directories
include_directories(/usr/local/include)
# add extra lib directories
link_directories(/usr/local/lib)
add_executable(Lib_Test main.cpp)
target_link_libraries(Lib_Test glfw)
target_link_libraries(Lib_Test glew)
find_package (GLM REQUIRED)
include_directories(include)
main.cpp
#include <stdio.h>
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
static unsigned int CompileShader(unsigned int type, const std::string& source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile " <<
(type == GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
int main(){
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW
" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( 640, 480, "My App", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window.
" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW
");
return -1;
}
float positions[6] = {
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, 0);
std::string vertexShader =
"#version 330 core
";
"
";
"layout(location = 0) in vec4 position;
";
"
";
"void main()
";
"{
";
" gl_Position = position
";
"}
";
std::string fragmentShader =
"#version 330 core
";
"
";
"layout(location = 0) out vec4 color;
";
"
";
"void main()
";
"{
";
" color = vec4(1.0, 0.0, 0.0, 1.0)
";
"}
";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
while( !glfwWindowShouldClose(window) )
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0 ,3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
推荐答案
您必须在核心上下文中创建和绑定VAO,它不像在兼容性上下文中那样是可选的。
这篇关于现代OpenGL MacOS纯黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:现代OpenGL MacOS纯黑屏
猜你喜欢
- 将 hdc 内容复制到位图 2022-09-04
- XML Schema 到 C++ 类 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- GDB 不显示函数名 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01