-
Notifications
You must be signed in to change notification settings - Fork 1
/
grass.c
104 lines (72 loc) · 2.49 KB
/
grass.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <SDL.h>
#include <GL/gl.h>
// gcc cube.c $(sdl2-config --cflags --libs ) -lGL
/*
* Grass and texture example
*/
int main() {
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
window = SDL_CreateWindow(
"Move the Cube with your arrow keys", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
0, // width, in pixels
0, // height, in pixels
SDL_WINDOW_OPENGL|SDL_WINDOW_FULLSCREEN_DESKTOP|SDL_WINDOW_RESIZABLE // flags - see below
);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
int w,h;
SDL_GetWindowSize(window, &w, &h);
glViewport(0,0,w,h);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glFrustum(1,-1,1,-1,0.5,100);
glTranslatef(0,0,-1.5);
glScalef((float)(w)/h,2,1);
glMatrixMode(GL_MODELVIEW);
// Enable Lighting
float light_position[] = {0,-1,0,1};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
SDL_GL_SetSwapInterval(1);
SDL_Surface *bmp = SDL_LoadBMP("img/grass.bmp");
int texture = 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp->w, bmp->h, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
float c_x = 0, c_z = 0;
while(1){
//Draw green plane
float color[] = {0.0,0.5,0,1};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glNormal3f(0,-1,0);
glVertex3f(1,0.5,1);
glTexCoord2f(1,0);
glVertex3f(1,0.5,-1);
glTexCoord2f(1,1);
glVertex3f(-1,0.5,-1);
glTexCoord2f(0,1);
glVertex3f(-1,0.5,1);
glEnd();
SDL_Event e;
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT)
return 0;
}
SDL_GL_SwapWindow(window);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}