§ main.cc
Примитивный треугольник в пространстве со сгенерированной текстурой.
1#define SDL_MAIN_HANDLED
2
3#include <SDL2/SDL.h>
4#include <GL/gl.h>
5#include <stdlib.h>
6#include <stdio.h>
7
8int main() {
9
10 SDL_Event event;
11 bool running = true;
12
13 SDL_Init(SDL_INIT_EVERYTHING);
14 SDL_Window* window = SDL_CreateWindow("OpenGL Demo",
15 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
16 1024, 768, SDL_WINDOW_OPENGL);
17 SDL_GLContext glcontext = SDL_GL_CreateContext(window);
18
19
20
21 int w = 256,
22 h = 256;
23
24 GLuint bgra[w*h];
25 GLuint texture_id;
26
27
28 for (int i = 0; i < h; i++)
29 for (int j = 0; j < w; j++) {
30 bgra[i*h + j] = (i ^ j) * 256;
31 }
32
33
34 glGenTextures(1, (GLuint *) & texture_id);
35 glBindTexture(GL_TEXTURE_2D, texture_id);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
40 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bgra);
41
42
43 glEnable(GL_TEXTURE_2D);
44 glMatrixMode(GL_PROJECTION);
45 glLoadIdentity();
46 glFrustum(-1, 1, -1, 1, 1, 40);
47
48 while (running)
49 {
50 while (SDL_PollEvent(&event) != 0) {
51 if (event.type == SDL_QUIT) {
52 running = false;
53 }
54 }
55
56 glClearColor(0, 0, 0.5, 1);
57 glClear(GL_COLOR_BUFFER_BIT);
58
59 glMatrixMode(GL_MODELVIEW);
60 glLoadIdentity();
61
62 glBegin(GL_POLYGON);
63 glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.6, 0.75, -1.2);
64 glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.6, 0.75, -1.2);
65 glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.6, -0.75, -1.2);
66 glEnd();
67
68 SDL_GL_SwapWindow(window);
69 }
70
71
72 SDL_GL_DeleteContext(glcontext);
73}
§ makefile
1all:
2 g++ -o main main.cc -lSDL2 -lGL
3 ./main