我的代码没有正确打开窗口,它只是向我显示空白页
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/matrix_interpolation.hpp>
#include <glm/gtc/constants.hpp>
#include "shader.h"
// Function to create a shader program
GLuint createShaderProgram(const char* vertexShaderPath, const char* fragmentShaderPath) {
return initShader(vertexShaderPath, fragmentShaderPath);
}
// Function to create a VAO and VBO for a shape
GLuint createShapeVAO(const GLfloat* vertices, GLsizei vertexCount) {
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertexCount, vertices, GL_STATIC_DRAW);
// Set up vertex attributes
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(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
return VAO;
}
// Function to render a shape
void renderShape(GLuint shaderProgram, GLuint VAO, GLsizei vertexCount, const glm::mat4& transform) {
glUseProgram(shaderProgram);
GLint transformLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glBindVertexArray(0);
}
int main(void) {
//++++create a glfw window+++++++++++++++++++++++++++++++++++++++
GLFWwindow* window;
if (!glfwInit()) //Initialize the library
return -1;
window = glfwCreateWindow(640, 640, "OpenGL Window", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);//Make the window's context current
//++++Initialize GLEW to setup the OpenGL Function pointers+++++++
glewExperimental = GL_TRUE;
glewInit();
//++++Define the viewport dimensions++++++++++++++++++++++++++++
glViewport(0, 0, 640, 640);
GLuint shaderProgram = createShaderProgram("vert.glsl", "frag.glsl");
// Define vertices for the triangle and square
GLfloat vertices_DarkPinktriangle[] = {
-0.4f, 0.4f, 0.0f, 170.0f / 255.0f, 51.0f / 255.0f, 106.0f / 255.0f, // Left
0.4f, 0.4f, 0.0f, 170.0f / 255.0f, 51.0f / 255.0f, 106.0f / 255.0f, // Right
0.0f, 0.0f, 0.0f, 170.0f / 255.0f, 51.0f / 255.0f, 106.0f / 255.0f, // Top
};
// Create VAOs for the shapes
GLuint VAO_DarkPinktriangle = createShapeVAO(vertices_DarkPinktriangle, 3);
//GLuint VAO_Greensquare = createShapeVAO(vertices_Greensquare, 4);
// Initialize transformation matrices
glm::mat4 transform_DarkPinktriangle = glm::mat4(1.0f);
//glm::mat4 transform_Greensquare = glm::mat4(1.0f);
// Main rendering loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Render the triangle
transform_DarkPinktriangle = glm::translate(transform_DarkPinktriangle, glm::vec3(0.0f, 0.0f, 0.0f));
renderShape(shaderProgram, VAO_DarkPinktriangle, 3, transform_DarkPinktriangle);
}
// Cleanup
glDeleteVertexArrays(1, &VAO_DarkPinktriangle);
//glDeleteVertexArrays(1, &VAO_Greensquare);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}
字符串
我期待一个红色三角形将显示在屏幕上,但我看到的是一个空白的白色屏幕,Visual Studio显示没有错误,问题不在于我的着色器文件或任何其他文件,因为它与类似的代码,它只是这种格式,它使我头痛
1条答案
按热度按时间2wnc66cl1#
永远不要交换帧缓冲区,在
renderShape
调用之后添加glfwSwapBuffers(window);
。