OpenGL kör rajzolás és animáció

OpenGL kör rajzolás és animáció
2022-03-30T20:14:33+02:00
2022-03-30T21:11:02+02:00
2022-10-15T21:21:38+02:00
DavinchiDani
Az lenne a feladat, hogy OpenGL-ben kirajzoljak egy kört, ami x tengelyen mozog az ablak széléig, és vissza.
Háromszöget sikerült kirajzolni és mozgatni, de a kört nem tudom, hogy kellene.

source.cpp

#include <GL\glew.h> #include <GLFW\glfw3.h> #include <string> #include <iostream> #include <fstream> #include <math.h> #include <vector> #define numVAOs 1 float x = 0.0f; float inc = 0.01f; using namespace std; void printShaderLog(GLuint shader) { int len = 0; int chWrittn = 0; char* log; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); if (len > 0) { log = (char*)malloc(len); glGetShaderInfoLog(shader, len, &chWrittn, log); cout << "Shader Info Log: " << log << endl; free(log); } } void printProgramLog(int prog) { int len = 0; int chWrittn = 0; char* log; glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len); if (len > 0) { log = (char*)malloc(len); glGetProgramInfoLog(prog, len, &chWrittn, log); cout << "Program Info Log: " << log << endl; free(log); } } bool checkOpenGLError() { bool foundError = false; int glErr = glGetError(); while (glErr != GL_NO_ERROR) { cout << "glError: " << glErr << endl; foundError = true; glErr = glGetError(); } return foundError; } string readShaderSource(const char* filePath) { string content; ifstream fileStream(filePath, ios::in); string line = ""; while (!fileStream.eof()) { getline(fileStream, line); content.append(line + "\n"); } fileStream.close(); return content; } GLuint renderingProgram; GLuint vao[numVAOs]; GLuint createShaderProgram() { GLint vertCompiled; GLint fragCompiled; GLint linked; string vertShaderStr = readShaderSource("vertShader.glsl"); string fragShaderStr = readShaderSource("fragShader.glsl"); const char* vertShaderSrc = vertShaderStr.c_str(); const char* fragShaderSrc = fragShaderStr.c_str(); GLuint vShader = glCreateShader(GL_VERTEX_SHADER); GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vShader, 1, &vertShaderSrc, NULL); glShaderSource(fShader, 1, &fragShaderSrc, NULL); glCompileShader(vShader); checkOpenGLError(); glGetShaderiv(vShader, GL_COMPILE_STATUS, &vertCompiled); if (vertCompiled != 1) { cout << "vertex compilation failed" << endl; printShaderLog(vShader); } glCompileShader(fShader); checkOpenGLError(); glGetShaderiv(fShader, GL_COMPILE_STATUS, &fragCompiled); if (fragCompiled != 1) { cout << "fragment compilation failed" << endl; printShaderLog(fShader); } GLuint vfProgram = glCreateProgram(); glAttachShader(vfProgram, vShader); glAttachShader(vfProgram, fShader); glLinkProgram(vfProgram); checkOpenGLError(); glGetProgramiv(vfProgram, GL_LINK_STATUS, &linked); if (linked != 1) { cout << "linking failed" << endl; printProgramLog(vfProgram); } return vfProgram; } void init(GLFWwindow* window) { renderingProgram = createShaderProgram(); glGenVertexArrays(numVAOs, vao); glBindVertexArray(vao[0]); } void display(GLFWwindow* window, double currentTime) { glClear(GL_DEPTH_BUFFER_BIT); glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(renderingProgram); x += inc; if (x > 1.0f) inc = -0.01f; if (x < -1.0f) inc = 0.01f; GLuint offsetLoc = glGetUniformLocation(renderingProgram, "offset"); glProgramUniform1f(renderingProgram, offsetLoc, x); glUseProgram(renderingProgram); glDrawArrays(GL_TRIANGLES, 0, 3); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPointSize(30.0f); } int main(void) { if (!glfwInit()) { exit(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); GLFWwindow* window = glfwCreateWindow(600, 600, "Chapter2 - program1", NULL, NULL); glfwMakeContextCurrent(window); if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); } glfwSwapInterval(1); init(window); while (!glfwWindowShouldClose(window)) { display(window, glfwGetTime()); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
vertShader.glsl

#version 430 uniform float offset; void main(void){ if (gl_VertexID == 0) gl_Position = vec4( 0.25 + offset, -0.25, 0.0, 1.0); else if (gl_VertexID == 1) gl_Position = vec4(-0.25 + offset, -0.25, 0.0, 1.0); else gl_Position = vec4( 0.25 + offset, 0.25, 0.0, 1.0); }
fragShader.glsl

#version 430 out vec4 color; void main(void){ color = vec4(1.0, 0.0, 0.0, 1.0); }
Mutasd a teljes hozzászólást!

Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd