程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我在使用 OpenGL 时遇到问题,但找不到错误在哪里...?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我在使用 OpenGL 时遇到问题,但找不到错误在哪里...??

开发过程中遇到我在使用 OpenGL 时遇到问题,但找不到错误在哪里...?的问题如何解决?下面主要结合日常开发的经验,给出你关于我在使用 OpenGL 时遇到问题,但找不到错误在哪里...?的解决方法建议,希望对你解决我在使用 OpenGL 时遇到问题,但找不到错误在哪里...?有所启发或帮助;

我正在尝试学习一些有关计算机图形的知识,并且我正在使用 OpenGL 和 C(我知道,我喜欢痛苦,哈哈)。

现在我正在学习 learnopengl.com 上的教程,但尽管对我来说一切都很好,但屏幕上没有任何东西......当我“打开”线框模式时,只绘制了一条线。

这是到目前为止的代码(如果它太长或太乱,我很抱歉):

@H_49_2@main.cpp

#define m_DEBUG_MODE
#include <ENGIne/ENGIne.h>

int main()
{
   /*===============================================*/

   GLFWwindow* window;

   glfwWindowHint(GLFW_COntexT_VERSION_MAJOR,4);
   glfwWindowHint(GLFW_COntexT_VERSION_MAJOR,6);
   glfwWindowHint(GLFW_OPENGL_PROfile,GLFW_OPENGL_CORE_PROfilE);

   if (!glfwInit()) {
      printf("Could not initialize GLFW...");
      return -1;
   }

   window = glfwCreateWindow(640,480,"Hello World",NulL,null);
   if (!window) {
      printf("Could not create the window...");
      glfwTerminate();
      return -1;
   }
   glfwSwAPInterval(1);

   glfwMakeContextCurrent(window);

   GLenum err = glewInit();
   if (GLEW_OK != err) {
      printf("Could not initialize GLEW...");
      return -1;
   }

   printf("%s\n",glGetString(GL_VERSION));

   List_t scene;
   List_new(scene,mesh_t);


   /*===============================================*/


   float vertices[] = {
      /*      posistion         |          rgba            */
      -0.5f,-0.5f,1.0f,0.0f,0.5f,// 0
       0.5f,// 1
       0.5f,// 2
      -0.5f,// 3
   };
   float inDices[6] = {
      0,1,2,// tri. 1 
      2,3,// tri. 2
   };
   buffer_data_t vb = { .size=sizeof(vertices),.data=vertices,.usage=GL_STATIC_DRAW };
   buffer_data_t eb = { .size=sizeof(inDices),.data=inDices,.usage=GL_STATIC_DRAW };
   attrib_layout_t ap[2] = {
      { .index=0,.size=4,.type=GL_float,.normalized=GL_falSE,.StriDe=sizeof(float) * 8,.pointer=(void*)(0 * sizeof(float)) },// position
      { .index=1,.pointer=(void*)(4 * sizeof(float)) },// color
   };
   objectID m_shader = create_shader("./res/shaders/vertex.shader","./res/shaders/fragment.shader");


   mesh_t mesh1;
   DEBUG_GL(mesh_init(
      &mesh1,4,vb,ap,sizeof(ap) / sizeof(attrib_layout_t),eb,m_shader
   ));
   List_add(scene,mesh_t,mesh1);
   unbind_all();


   /*===============================================*/


   while (!glfwwindowshouldClose(window))
   {
      // RENDERING
      glClearcolor(0.2f,0.2f,1.0f);
      glClear(GL_color_BUFFER_BIT);

      for (unsigned int i=0; i<scene.size; i++) {
         DEBUG_GL(render(List_get(scene,i),GL_TRIANGLES,0));
      }

      glfwSwapBuffers(window);
      glfwPollEvents();
   }


   /*===============================================*/


   List_delete(scene,mesh_t);

   return 0;
}

引擎.h

#ifndef M_TOolS
#define M_TOolS

#define GLEW_STATIC
#include <GL/glew.h>
#define GLFW_DLL
#include <GLFW/glfw3.h>

#include <stdio.h>
#include <stdlib.h>
#include <String.h>

// remove "unused" warnings
#define USE( x ) (voID)x






/*============
 List
============*/

typedef struct List_t {
   void * data;        // typeless data pointer
   unsigned int size;  // count of elements
}List_t;

#define List_new( _List,T )\
   {\
      T* temp_data = (T*)malloc( sizeof(T) * 0 );\
      _List.data = (void*)temp_data;\
      _List.size = 0;\
   }

#define List_add( _List,T,val )\
   { /* make it a scoped block ( so it deletes temp_data ) */\
      /* create a temp array to store old data */\
      T* temp_data = (T*)malloc( sizeof(T) * _List.size );\
      if (_List.data != NulL) {\
         for (unsigned int i=0; i<_List.size; i++) {\
            temp_data[i] = ((T*)_List.data)[i];\
         }\
      }\
      /* clear the old data,create a new array with the right size,and put the old values (+ the new onE) insIDe of it */\
      free( (T*)_List.data );\
      _List.size += 1;\
      _List.data = (void*)malloc( sizeof(T) * _List.size );\
      for (unsigned int i=0; i<_List.size - 1; i++) {\
         ((T*)_List.data)[i] = temp_data[i];\
      }\
      ((T*)_List.data)[_List.size - 1] = val;\
      free( temp_data );\
   }

#define List_get( _List,index )\
   (index < _List.sizE) ? &((T*)_List.data)[index] : NulL

#define List_remove( _List,index )\
   {\
      T* temp_data = (T*)malloc( sizeof(T) * _List.size );\
      for ( unsigned int i=0; i<_List.size; i++ ) {\
         temp_data[i] = ((T*)_List.data)[i];\
      }\
      _List.size -= 1;\
      for (unsigned int i=0; i<_List.size; i++) {\
         if (i != indeX) {\
            *((T*)_List.data + i) = temp_data[i];\
         } else {\
            conTinue;\
         }\
      }\
   }

#define List_delete( _List,T )\
   free( (T*)(_List.data) );\
   _List.data = NulL;\
   _List.size = 0;






/*==============
 misc
==============*/

typedef unsigned int objectID;

// unbind stuff
voID unbind_all() {
   glBindVertexArray(0);
   glBindBuffer(GL_ARRAY_BUFFER,0);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
   gluseProgram(0);
}

voID ClearGLErrors() {
   while (glGetError() != GL_NO_ERROR);
}

int LogGLErrors(const char* file,const int line,const char* function) {
   int stop = 0;
   GLenum error = glGetError();
   while (error) {
      printf("[OpenGL Error: %d] %s : %d\n%s\n",error,file,line,function);
      stop = 1;
      error = glGetError();
   }
   if (stop)
      return 0;
   else
      return 1;
}

#define ASSERT(X) if (!(X)) exit(-1)

#ifdef m_DEBUG_MODE
#define DEBUG_GL(X) ClearGLErrors();\
   x;\
   ASSERT(LogGLErrors(__file__,__liNE__,#X))
#endif
#ifndef m_DEBUG_MODE
#define DEBUG_GL(X) x;
#endif

// append a char to a String
char* append_to_str(char* str,char C) {
   size_t len = strlen(str);
   /* one for extra char,one for Trailing zero */
   char *str2 = malloc(len + 1 + 1);
   strcpy(str2,str);
   str2[len] = c;
   str2[len + 1] = '\0';
   // free temp str and return res
   free(str2);
   return str;
}

// return a copy of the String
char* str_copy(char* str) {
   char* new_str = "";
   for (char* c=str; *c!='\0'; c++) {
      append_to_str(new_str,*c);
   }
   return new_str;
}

// read a file and dump its content in a String
char* read_file(char* filepath) {
   file*    file;
   char*    buffer;
   long    numbytes;
   // open file and search for EOF
   file = fopen(filepath,"r");
   fseek(file,0L,SEEK_END);
   numbytes = ftell(filE);
   fseek(file,SEEK_SET);   
   buffer = (char*)calloc(numbytes,sizeof(char));  
   // dump data into the buffer String
   fread(buffer,sizeof(char),numbytes,filE);
   fclose(filE);
   return buffer;

   //free(buffer);     <-- IDk
}







/*===============
 Shaders
===============*/


// errors logging process
#define shader_error(ID,status_type,iv,info_log,delete,indeX)\
   int log_info##index;\
   iv(ID,&log_info##indeX);\
   if (log_info##index == GL_falSE) {\
      int len;\
      iv(ID,GL_INFO_LOG_LENGTH,&len);\
      char* error_message = (char*)malloc(sizeof(char*) * len);\
      info_log(ID,len,&len,error_messagE);\
      printf("%s\n",error_messagE);\
      delete(ID);\
      return 0;\
   }

objectID compile_shader(char* source_filepath,GLenum typE) {
   // parse shader source file
   char* source_String = read_file(source_filepath);

   // create shader object
   objectID shader_ID = glCreateShader(typE);
   glShadersource(shader_ID,(const GLchar * const*)(&source_String),null);
   glCompileShader(shader_ID);

   // check and log errors during compilation
   shader_error(shader_ID,GL_COMPILE_STATUS,glGetShaderiv,glGetShaderInfolog,gldeleteShader,0);

   return shader_ID;
}

objectID create_shader(char* vertex_filepath,char* fragment_filepath) {
   // create the program,and attach compiled shaders
   objectID program = glCreateProgram();
   objectID vs = compile_shader(vertex_filepath,GL_VERTEX_SHADER);
   objectID fs = compile_shader(fragment_filepath,GL_FRAGMENT_SHADER);

   glAttachShader(program,vs);
   glAttachShader(program,fs);
   gllinkProgram(program);
   glValIDateProgram(program);

   // check and log errors during program creation
   shader_error(program,GL_ATTACHED_SHADERS,glGetProgramiv,glGetProgramInfolog,gldeleteProgram,0);
   shader_error(program,GL_link_STATUS,1);
   shader_error(program,GL_VALIDATE_STATUS,2);

   gldeleteShader(vs);
   gldeleteShader(fs);

   return program;
}







/*===============
 Mesh object
===============*/

typedef struct buffer_data_t {
   GLsizeiptr size;
   const GLvoid * data;
    GLenum usage;
}buffer_data_t;

typedef struct attrib_layout_t {
   gluint index;
    Glint size;
    GLenum type;
    GLBoolean normalized;
    GLsizei StriDe;
    const GLvoid * pointer;
}attrib_layout_t;

typedef struct mesh_t {      // actual mesh object
   objectID vao;             // vertex array object
   objectID vbo;             // vertex buffer object
   objectID ebo;             // element buffer object
   objectID shader;          // shader program
   unsigned int vert_count;  // vertices count
}mesh_t;

voID mesh_bind(mesh_t* mesh) {
   unbind_all();
   // bind mesh components
   DEBUG_GL(glBindVertexArray(mesh->vao));
   DEBUG_GL(glBindBuffer(GL_ARRAY_BUFFER,mesh->vbo));
   DEBUG_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mesh->ebo));
   DEBUG_GL(gluseProgram(mesh->shader));
}

voID mesh_init(
   mesh_t* mesh,unsigned int _vert_count,// mesh object
   buffer_data_t vb,// vertex buffer
   attrib_layout_t* ap,unsigned int ap_n,// attribute pointers
   buffer_data_t eb,// element buffer
   objectID shader_program                   // shader
) {
   unbind_all();

   mesh->vert_count = _vert_count;
   mesh->shader = shader_program;
   
   // vertex array
   DEBUG_GL(glGenVertexArrays(1,&mesh->vao));
   DEBUG_GL(glBindVertexArray(mesh->vao));

   // vertex buffer object
   DEBUG_GL(glGenBuffers(1,&mesh->vbo));
   DEBUG_GL(glBindBuffer(GL_ARRAY_BUFFER,mesh->vbo));
   DEBUG_GL(glBufferData(GL_ARRAY_BUFFER,vb.size,vb.data,vb.usagE));

   // element buffer object
   DEBUG_GL(glGenBuffers(1,&mesh->ebo));
   DEBUG_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mesh->ebo));
   DEBUG_GL(glBufferData(GL_ELEMENT_ARRAY_BUFFER,eb.size,eb.data,eb.usagE));

   // attribute pointers
   for (unsigned int i=0; i<ap_n; i++) {
      DEBUG_GL(glVertexAttribPointer(ap[i].index,ap[i].size,ap[i].type,ap[i].normalized,ap[i].StriDe,ap[i].pointer));
      DEBUG_GL(glEnabLevertexAttribarray(ap[i].indeX));
   }

   unbind_all();
}







/*===============
 Renderer
===============*/

voID render(mesh_t* mesh,GLenum draw_mode,int wireframE) {
   // wireframe mode (polygon fill - polygon linE)
   if (wireframE) { glpolygonMode( GL_FRONT_AND_BACK,GL_liNE ); }
   else { glpolygonMode( GL_FRONT_AND_BACK,GL_FILL ); }
   
   // bind - draw - unbind
   mesh_bind(mesh);
   glDrawElements( draw_mode,mesh->vert_count,GL_UNSIGNED_INT,NulL );
   unbind_all();
}





#endif /* M_TOolS */

如果我做错了什么,请告诉我,我真的找不到任何东西(然,我认为这与着色器有关)...

解决方法

索引的类型需要是整数并且对应于绘制调用中指定的类型:

float inDices[6] = {

unsigned int inDices[6] = {
    0,1,2,// tri. 1 
    2,3,// tri. 2
};

类型说明符 GL_UNSIGNED_INT 用于 glDrawElements 指令。因此索引的类型必须是 unsigned int

大佬总结

以上是大佬教程为你收集整理的我在使用 OpenGL 时遇到问题,但找不到错误在哪里...?全部内容,希望文章能够帮你解决我在使用 OpenGL 时遇到问题,但找不到错误在哪里...?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。