-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c.2.bak
137 lines (134 loc) · 4.86 KB
/
main.c.2.bak
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#define STB_IMAGE_IMPLEMENTATION
#include<stdio.h>
#include<stdlib.h>
#include<GLFW/glfw3.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<stb/stb_image.h>
GLuint left = 0;
GLuint right = 0;
void key_callback();
int main(){
if(!glfwInit()){
//printf("Failed to initialize GLFW!\n");
system("notify-send 'Failed to initialize GLFW'");
return 1;
}
GLFWwindow* window = glfwCreateWindow(1054, 1057, "Title", NULL, NULL);
if(window == NULL){
//printf("Window creation failed!\n");
system("notify-send 'Window creation failed!'");
return 1;
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMatrixMode(GL_MODELVIEW);
float translatetri = 0.0f;
float translatequad = 0.0f;
float rotationx = 0.0f;
float rotationy = 0.0f;
float ambientlight[] = {0.5f, 0.5f, 0.5f, 1.0f};
float diffuselight[] = {1.0f, 1.0f, 1.0f, 1.0f};
float lightposition[] = {1.0f, 0.0f, -3.0f, 1.0f};
float currentframe = 0.0f;
float lastframe = 0.0f;
float deltatime = 0.0f;
int texwidth, texheight, texnum;
unsigned char* bytes = stbi_load("wood.jpg", &texwidth, &texheight, &texnum, 0);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texwidth, texheight, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes);
glLightfv(GL_LIGHT1, GL_AMBIENT, ambientlight);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuselight);
glLightfv(GL_LIGHT1, GL_POSITION, lightposition);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
glfwSwapBuffers(window);
currentframe = glfwGetTime();
deltatime = currentframe - lastframe;
lastframe = currentframe;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(translatetri, 0.0f, 0.0f);
glRotatef(rotationx, 0.0f, 1.0f, 0.0f);
//glRotatef(rotationy, 1.0f, 0.0f, 0.0f);
/*
if(left == 1){
translatetri += 1.0f * deltatime;
}
if(right == 1){
translatetri += -1.0f * deltatime;
}
*/
glBegin(GL_TRIANGLES);
//glColor3f(1.0f, 0.0f, 0.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2f(0.5f, 1.0f); glVertex3f( 0.0f, 0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f);
//glColor3f(0.0f, 1.0f, 0.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f);
glTexCoord2f(0.5f, 1.0f); glVertex3f( 0.0f, 0.5f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.5f);
//glColor3f(0.0f, 0.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2f(0.5f, 1.0f); glVertex3f( 0.0f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, 0.5f);
//glColor3f(0.0f, 1.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2f(0.5f, 1.0f); glVertex3f( 0.0f, 0.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
//glColor3f(1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, 0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
glEnd();
rotationx += 100.0f * deltatime;
rotationy += 25.0f * deltatime;
if(rotationx > 360.0f){
rotationx = rotationx - 360.0f;
}
if(rotationx < 0.0f){
rotationx = 360.0f + rotationx;
}
if(rotationy > 360.0f){
rotationy = rotationy - 360.0f;
}
if(rotationy < 0.0f){
rotationy = 360.0f + rotationy;
}
}
return 0;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
if(key == GLFW_KEY_A && action == GLFW_PRESS){
left = 1;
}else if(key == GLFW_KEY_A && action == GLFW_RELEASE){
left = 0;
}else if(key == GLFW_KEY_D && action == GLFW_PRESS){
right = 1;
}else if(key == GLFW_KEY_D && action == GLFW_RELEASE){
right = 0;
}
}