Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4. #include <string>
  5. #include <GL/glew.h>
  6. #include <Cg/cg.h>
  7. #include <Cg/cgGL.h>
  8. #include <SDL2/SDL.h>
  9.  
  10. int main()
  11. {
  12. SDL_Window *mainwindow;
  13. SDL_GLContext maincontext;
  14.  
  15. SDL_Init(SDL_INIT_VIDEO);
  16.  
  17. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  18. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  19. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  20. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  21.  
  22. mainwindow = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  23.  
  24. maincontext = SDL_GL_CreateContext(mainwindow);
  25.  
  26. glewExperimental = GL_TRUE;
  27. glewInit();
  28.  
  29. _CGcontext* cgcontext;
  30. cgcontext = cgCreateContext();
  31. cgGLRegisterStates(cgcontext);
  32.  
  33. CGerror error;
  34. CGeffect effect;
  35. const char* string;
  36. std::string shader;
  37.  
  38. shader =
  39. "struct VS_INPUT"
  40. "{"
  41. " float3 pos : ATTR0;"
  42. "};"
  43.  
  44. "struct FS_INPUT"
  45. "{"
  46. " float4 pos : POSITION;"
  47. " float2 tex : TEXCOORD0;"
  48. "};"
  49.  
  50. "struct FS_OUTPUT"
  51. "{"
  52. " float4 color : COLOR;"
  53. "};"
  54.  
  55. "FS_INPUT VS( VS_INPUT In )"
  56. "{"
  57. " FS_INPUT Out;"
  58. " Out.pos = float4( In.pos, 1.0f );"
  59. " Out.tex = float2( 0.0f, 0.0f );"
  60. " return Out;"
  61. "}"
  62.  
  63. "FS_OUTPUT FS( FS_INPUT In )"
  64. "{"
  65. " FS_OUTPUT Out;"
  66. " Out.color = float4(1.0f, 0.0f, 0.0f, 1.0f);"
  67. " return Out;"
  68. "}"
  69.  
  70. "technique t0"
  71. "{"
  72. " pass p0"
  73. " {"
  74. " VertexProgram = compile gp4vp VS();"
  75. " FragmentProgram = compile gp4fp FS();"
  76. " }"
  77. "}";
  78.  
  79. effect = cgCreateEffect(cgcontext, shader.c_str(), NULL);
  80. error = cgGetError();
  81. if(error)
  82. {
  83. string = cgGetLastListing(cgcontext);
  84. fprintf(stderr, "Shader compiler: %s\n", string);
  85. }
  86.  
  87. glClearColor ( 0.0, 0.0, 1.0, 1.0 );
  88. glClear ( GL_COLOR_BUFFER_BIT );
  89.  
  90. float* vert = new float[9];
  91.  
  92. vert[0] = 0.0; vert[1] = 0.5; vert[2] =-1.0;
  93. vert[3] =-1.0; vert[4] =-0.5; vert[5] =-1.0;
  94. vert[6] = 1.0; vert[7] =-0.5; vert[8]= -1.0;
  95.  
  96. unsigned int m_vaoID;
  97. unsigned int m_vboID;
  98.  
  99. glGenVertexArrays(1, &m_vaoID);
  100. glBindVertexArray(m_vaoID);
  101.  
  102. glGenBuffers(1, &m_vboID);
  103.  
  104. glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
  105. glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vert, GL_STATIC_DRAW);
  106.  
  107. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  108. glEnableVertexAttribArray(0);
  109.  
  110. CGtechnique tech = cgGetFirstTechnique( effect );
  111. CGpass pass = cgGetFirstPass(tech);
  112. while (pass)
  113. {
  114. cgSetPassState(pass);
  115. glDrawArrays(GL_TRIANGLES, 0, 3);
  116. cgResetPassState(pass);
  117. pass = cgGetNextPass(pass);
  118. }
  119.  
  120. glDisableVertexAttribArray( 0 );
  121.  
  122. glBindVertexArray(0);
  123.  
  124. delete[] vert;
  125.  
  126. glBindBuffer(GL_ARRAY_BUFFER, 0);
  127. glDeleteBuffers(1, &m_vboID);
  128. glDeleteVertexArrays(1, &m_vaoID);
  129.  
  130. SDL_GL_SwapWindow(mainwindow);
  131.  
  132. bool state = true;
  133.  
  134. while (state)
  135. {
  136. SDL_Event event;
  137. while (SDL_PollEvent(&event))
  138. {
  139. switch (event.type)
  140. {
  141. case SDL_QUIT:
  142. state = false;
  143. break;
  144. case SDL_KEYDOWN:
  145. state = false;
  146. break;
  147. case SDL_KEYUP:
  148. state = false;
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. }
  155.  
  156. SDL_GL_DeleteContext(maincontext);
  157. SDL_DestroyWindow(mainwindow);
  158. SDL_Quit();
  159.  
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement